编写规则时,最常见的性能误区是遍历或复制从依赖项累积的数据。如果对整个 build 进行汇总,这些操作会很容易占用 O(N^2) 时间或空间。为避免这种情况,您必须了解如何有效地使用依赖项。
这可能很难做出正确选择,因此 Bazel 还提供内存分析器,帮助您查找可能犯错的位置。注意:在大范围使用之前,编写低效规则的代价可能并不明显。
使用依赖项
每当您滚动来自规则依赖项的信息时,都应使用 depsets。仅使用普通列表或字典来发布当前规则本地的信息。
预设将信息表示为可启用共享的嵌套图。
请参考以下图表:
C -> B -> A
D ---^
每个节点都会发布一个字符串。使用 dessets,数据如下所示:
a = depset(direct=['a'])
b = depset(direct=['b'], transitive=[a])
c = depset(direct=['c'], transitive=[b])
d = depset(direct=['d'], transitive=[b])
请注意,每项只被提及一次。使用列表,您会得到:
a = ['a']
b = ['b', 'a']
c = ['c', 'b', 'a']
d = ['d', 'b', 'a']
请注意,在本例中,'a'
被提及了四次!在使用更大的图表时,这个问题只会变得更加严重。
下面是一个规则实现示例,该实现正确使用依赖项来发布传递信息。请注意,如果您愿意,可以使用列表发布规则本地信息,因为这不是 O(N^2)。
MyProvider = provider()
def _impl(ctx):
my_things = ctx.attr.things
all_things = depset(
direct=my_things,
transitive=[dep[MyProvider].all_things for dep in ctx.attr.deps]
)
...
return [MyProvider(
my_things=my_things, # OK, a flat list of rule-local things only
all_things=all_things, # OK, a depset containing dependencies
)]
如需了解详情,请参阅预设概览页面。
避免调用 depset.to_list()
您可以使用 to_list()
将偏移量强制转换为平面列表,但通常会产生 O(N^2) 费用。请尽可能避免除调试之外的任何扁平化偏移。
一种常见的误解是,如果您只在顶级目标(如 <xx>_binary
规则)中这么做,就可以自由地拆分偏移,因为费用不会累计在构建图的每个级别上。但当您构建一组具有重叠依赖项的目标时,这仍是 O(N^2)。构建测试 //foo/tests/...
或导入 IDE 项目时,会发生这种情况。
减少对 depset
的调用次数
在循环内调用 depset
通常是一个错误。这会导致出现非常深的嵌套,进而导致性能下降。例如:
x = depset()
for i in inputs:
# Do not do that.
x = depset(transitive = [x, i.deps])
此代码可轻松替换。首先,收集传递依赖项并一次性将它们全部合并:
transitive = []
for i in inputs:
transitive.append(i.deps)
x = depset(transitive = transitive)
有时,可通过运用理解来实现这一点:
x = depset(transitive = [i.deps for i in inputs])
为命令行使用 ctx.actions.args()
构建命令行时,您应使用 ctx.actions.args()。这会将任何依赖项推迟到执行阶段。
除了严格加快速度之外,还可以将规则的内存消耗量减少 90% 或更多。
以下是一些提示:
请直接将参数和列表作为参数传递,而不是自行拆分它们。它们会为您展开
ctx.actions.args()
。 如果您需要对偏移内容进行任何转换,请查看 ctx.actions.args#add 以了解是否有任何内容符合帐单。避免通过将字符串串联在一起来构造字符串。最佳字符串参数是一个常量,因为其内存将在规则的所有实例之间共享。
如果参数对于命令行来说过长,可以使用
ctx.actions.args#use_param_file
有条件地或无条件地写入参数文件。系统会在执行操作时在后台完成该操作。如果您需要明确控制参数文件,可以使用ctx.actions.write
手动编写。
例如:
def _impl(ctx):
...
args = ctx.actions.args()
file = ctx.declare_file(...)
files = depset(...)
# Bad, constructs a full string "--foo=<file path>" for each rule instance
args.add("--foo=" + file.path)
# Good, shares "--foo" among all rule instances, and defers file.path to later
# It will however pass ["--foo", <file path>] to the action command line,
# instead of ["--foo=<file_path>"]
args.add("--foo", file)
# Use format if you prefer ["--foo=<file path>"] to ["--foo", <file path>]
args.add(format="--foo=%s", value=file)
# Bad, makes a giant string of a whole depset
args.add(" ".join(["-I%s" % file.short_path for file in files])
# Good, only stores a reference to the depset
args.add_all(files, format_each="-I%s", map_each=_to_short_path)
# Function passed to map_each above
def _to_short_path(f):
return f.short_path
传递的操作输入应为 depset
使用 ctx.actions.run 构建操作时,不要忘记 inputs
字段接受预设。每当以传递方式从依赖项收集输入时,都要使用此选项。
inputs = depset(...)
ctx.actions.run(
inputs = inputs, # Do *not* turn inputs into a list
...
)
悬挂式
如果 Bazel 似乎挂起,您可以按 Ctrl-\ 键或向 Bazel 发送 SIGQUIT
信号 (kill -3 $(bazel info server_pid)
),以在文件 $(bazel info output_base)/server/jvm.out
中获取线程转储。
由于如果 bazel 挂起,您可能无法运行 bazel info
,因此 output_base
目录通常是工作区目录中 bazel-<workspace>
符号链接的父级。
性能剖析
JSON 跟踪记录配置文件非常有用,可让您快速了解 Bazel 在调用期间花费的时间。
内存性能分析
Bazel 附带内置内存分析器,可以帮助您检查规则的内存使用情况。如果出现问题,您可以转储堆,以查找导致问题的确切代码行。
启用内存跟踪
您必须将以下两个启动标志传递给每个 Bazel 调用:
STARTUP_FLAGS=\
--host_jvm_args=-javaagent:$(BAZEL)/third_party/allocation_instrumenter/java-allocation-instrumenter-3.3.0.jar \
--host_jvm_args=-DRULE_MEMORY_TRACKER=1
这些命令以内存跟踪模式启动服务器。即使您忘记了一次 Bazel 调用,服务器也会重启,而您必须重新开始。
使用内存跟踪器
例如,查看目标 foo
并了解它的功能。若要仅运行分析而不运行构建执行阶段,请添加 --nobuild
标志。
$ bazel $(STARTUP_FLAGS) build --nobuild //foo:foo
接下来,查看整个 Bazel 实例使用了多少内存:
$ bazel $(STARTUP_FLAGS) info used-heap-size-after-gc
> 2594MB
使用 bazel dump --rules
按规则类对其进行细分:
$ bazel $(STARTUP_FLAGS) dump --rules
>
RULE COUNT ACTIONS BYTES EACH
genrule 33,762 33,801 291,538,824 8,635
config_setting 25,374 0 24,897,336 981
filegroup 25,369 25,369 97,496,272 3,843
cc_library 5,372 73,235 182,214,456 33,919
proto_library 4,140 110,409 186,776,864 45,115
android_library 2,621 36,921 218,504,848 83,366
java_library 2,371 12,459 38,841,000 16,381
_gen_source 719 2,157 9,195,312 12,789
_check_proto_library_deps 719 668 1,835,288 2,552
... (more output)
使用 bazel dump --skylark_memory
生成 pprof
文件,查看内存的去向:
$ bazel $(STARTUP_FLAGS) dump --skylark_memory=$HOME/prof.gz
> Dumping Starlark heap to: /usr/local/google/home/$USER/prof.gz
使用 pprof
工具调查堆。不妨先使用 pprof -flame $HOME/prof.gz
获取火焰图。
从 https://github.com/google/pprof 获取 pprof
。
获取用行注释的最热门的调用网站的文本转储:
$ pprof -text -lines $HOME/prof.gz
>
flat flat% sum% cum cum%
146.11MB 19.64% 19.64% 146.11MB 19.64% android_library <native>:-1
113.02MB 15.19% 34.83% 113.02MB 15.19% genrule <native>:-1
74.11MB 9.96% 44.80% 74.11MB 9.96% glob <native>:-1
55.98MB 7.53% 52.32% 55.98MB 7.53% filegroup <native>:-1
53.44MB 7.18% 59.51% 53.44MB 7.18% sh_test <native>:-1
26.55MB 3.57% 63.07% 26.55MB 3.57% _generate_foo_files /foo/tc/tc.bzl:491
26.01MB 3.50% 66.57% 26.01MB 3.50% _build_foo_impl /foo/build_test.bzl:78
22.01MB 2.96% 69.53% 22.01MB 2.96% _build_foo_impl /foo/build_test.bzl:73
... (more output)