Use depsets
Whenever you are rolling up information from rule dependencies you should use depsets. Only use plain lists or dicts to publish information local to the current rule. A depset represents information as a nested graph which enables sharing. Consider the following graph:'a' is mentioned four times! With larger graphs this
problem will only get worse.
Here is an example of a rule implementation that uses depsets correctly to
publish transitive information. Note that it is OK to publish rule-local
information using lists if you want since this is not O(N^2).
Avoid calling depset.to_list()
You can coerce a depset to a flat list using
to_list(), but doing so usually results in O(N^2)
cost. If at all possible, avoid any flattening of depsets except for debugging
purposes.
A common misconception is that you can freely flatten depsets if you only do it
at top-level targets, such as an <xx>_binary rule, since then the cost is not
accumulated over each level of the build graph. But this is still O(N^2) when
you build a set of targets with overlapping dependencies. This happens when
building your tests //foo/tests/..., or when importing an IDE project.
Reduce the number of calls to depset
Calling depset inside a loop is often a mistake. It can lead to depsets with
very deep nesting, which perform poorly. For example:
Use ctx.actions.args() for command lines
When building command lines you should use ctx.actions.args(). This defers expansion of any depsets to the execution phase. Apart from being strictly faster, this will reduce the memory consumption of your rules — sometimes by 90% or more. Here are some tricks:-
Pass depsets and lists directly as arguments, instead of flattening them
yourself. They will get expanded by
ctx.actions.args()for you. If you need any transformations on the depset contents, look at ctx.actions.args#add to see if anything fits the bill. -
Are you passing
File#pathas arguments? No need. Any File is automatically turned into its path, deferred to expansion time. - Avoid constructing strings by concatenating them together. The best string argument is a constant as its memory will be shared between all instances of your rule.
-
If the args are too long for the command line an
ctx.actions.args()object can be conditionally or unconditionally written to a param file usingctx.actions.args#use_param_file. This is done behind the scenes when the action is executed. If you need to explicitly control the params file you can write it manually usingctx.actions.write.
Transitive action inputs should be depsets
When building an action using ctx.actions.run, do not forget that theinputs field accepts a depset. Use this whenever inputs are
collected from dependencies transitively.
Hanging
If Bazel appears to be hung, you can hit Ctrl-\ or send Bazel aSIGQUIT signal (kill -3 $(bazel info server_pid)) to get a thread
dump in the file $(bazel info output_base)/server/jvm.out.
Since you may not be able to run bazel info if bazel is hung, the
output_base directory is usually the parent of the bazel-<workspace>
symlink in your workspace directory.
Performance profiling
The JSON trace profile can be very useful to quickly understand what Bazel spent time on during the invocation. The--experimental_command_profile
flag may be used to capture Java Flight Recorder profiles of various kinds
(cpu time, wall time, memory allocations and lock contention).
The --starlark_cpu_profile
flag may be used to write a pprof profile of CPU usage by all Starlark threads.
Memory profiling
Bazel comes with a built-in memory profiler that can help you check your rule’s memory use. If there is a problem you can dump the heap to find the exact line of code that is causing the problem.Enabling memory tracking
You must pass these two startup flags to every Bazel invocation:Using the Memory Tracker
As an example, look at the targetfoo and see what it does. To only
run the analysis and not run the build execution phase, add the
--nobuild flag.
bazel dump --rules:
pprof file
using bazel dump --skylark_memory:
pprof tool to investigate the heap. A good starting point is
getting a flame graph by using pprof -flame $HOME/prof.gz.
Get pprof from https://github.com/google/pprof.
Get a text dump of the hottest call sites annotated with lines: