Skip to content

Commit 542e7e0

Browse files
manekinekkoalexeagle
authored andcommitted
fix: reduce the use of to_list()
1 parent 338a43c commit 542e7e0

7 files changed

Lines changed: 35 additions & 18 deletions

File tree

‎internal/common/collect_es6_sources.bzl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def collect_es6_sources(ctx):
3333
non_rerooted_files += ctx.files.srcs
3434
for dep in ctx.attr.deps:
3535
if hasattr(dep, "typescript"):
36-
non_rerooted_files += [f.path for f in dep.typescript.transitive_es6_sources]
36+
non_rerooted_files += dep.typescript.transitive_es6_sources.to_list()
3737

3838
rerooted_files = []
3939
for file in non_rerooted_files:

‎internal/common/devmode_js_sources.bzl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def _devmode_js_sources_impl(ctx):
3939

4040
ctx.actions.write(ctx.outputs.manifest, "".join([
4141
expand_path_into_runfiles(ctx, f.path) + "\n"
42-
for f in files
42+
for f in files.to_list()
4343
]))
4444
return [DefaultInfo(files = files)]
4545

‎internal/common/sources_aspect.bzl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _sources_aspect_impl(target, ctx):
4242
result = depset(transitive = [result, target.typescript.es5_sources])
4343
elif hasattr(target, "files"):
4444
result = depset(
45-
[f for f in target.files if f.path.endswith(".js")],
45+
[f for f in target.files.to_list() if f.path.endswith(".js")],
4646
transitive = [result],
4747
)
4848
return struct(node_sources = result)

‎internal/common/typescript_mock_lib.bzl‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ def _mock_typescript_lib(ctx):
2323
es5_sources = depset()
2424
transitive_decls = depset()
2525
for s in ctx.attr.srcs:
26-
es5_sources = depset([f for f in s.files if f.path.endswith(".js")], transitive = [es5_sources])
27-
transitive_decls = depset([f for f in s.files if f.path.endswith(".d.ts")], transitive = [transitive_decls])
26+
files_list = s.files.to_list()
27+
es5_sources = depset([f for f in files_list if f.path.endswith(".js")], transitive = [es5_sources])
28+
transitive_decls = depset([f for f in files_list if f.path.endswith(".d.ts")], transitive = [transitive_decls])
2829
return struct(
2930
runfiles = ctx.runfiles(collect_default = True, collect_data = True),
3031
typescript = struct(

‎internal/node/node.bzl‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,17 @@ def _short_path_to_manifest_path(ctx, short_path):
102102
def _nodejs_binary_impl(ctx):
103103
node = ctx.file.node
104104
node_modules = ctx.files.node_modules
105-
sources = []
105+
106+
# Using a depset will allow us to avoid flattening files and sources
107+
# inside this loop. This should reduce the performances hits,
108+
# since we don't need to call .to_list()
109+
sources = depset()
110+
106111
for d in ctx.attr.data:
107112
if hasattr(d, "node_sources"):
108-
# sources += d.node_sources.to_list()
109-
sources += [f for f in d.node_sources]
113+
sources = depset( transitive = [sources, d.node_sources] )
110114
if hasattr(d, "files"):
111-
# sources += d.files.to_list()
112-
sources += [f for f in d.files]
113-
114-
# print([f for f in d.files])
115-
# print(d.files.to_list())
116-
# print(sources)
115+
sources = depset( transitive = [sources, d.files] )
117116

118117
_write_loader_script(ctx)
119118

@@ -152,13 +151,24 @@ def _nodejs_binary_impl(ctx):
152151
is_executable = True,
153152
)
154153

155-
runfiles = depset(sources + [node, ctx.outputs.loader, ctx.file._repository_args] + node_modules + ctx.files._node_runfiles)
154+
runfiles = depset(
155+
transitive = [sources + [node, ctx.outputs.loader, ctx.file._repository_args] + node_modules + ctx.files._node_runfiles]
156+
)
156157

157158
return [DefaultInfo(
158159
executable = ctx.outputs.script,
159160
runfiles = ctx.runfiles(
160161
transitive_files = runfiles,
161-
files = [node, ctx.outputs.loader] + ctx.files._source_map_support_files + node_modules + sources,
162+
files = [
163+
node,
164+
ctx.outputs.loader
165+
] + ctx.files._source_map_support_files + node_modules +
166+
167+
# We need this call to the list of Files.
168+
# Calling the .to_list() method may have some perfs hits,
169+
# so we should be running this method only once per rule.
170+
# see: https://docs.bazel.build/versions/master/skylark/depsets.html#performance
171+
sources.to_list(),
162172
collect_data = True,
163173
),
164174
)]

‎internal/npm_package/npm_package.bzl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ def _npm_package(ctx):
9797

9898
deps_sources = depset(transitive = transitive)
9999

100-
package_dir = create_package(ctx, [f for f in deps_sources], ctx.files.packages)
100+
# Note: to_list() should be called once per rule!
101+
package_dir = create_package(ctx, deps_sources.to_list(), ctx.files.packages)
101102

102103
return [DefaultInfo(
103104
files = depset([package_dir]),

‎internal/rollup/rollup_bundle.bzl‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ def run_rollup(ctx, sources, config, output):
151151
return map_output
152152

153153
def _filter_js_inputs(all_inputs):
154+
155+
# Note: make sure that "all_inputs" is not a depset.
156+
# Iterating over a depset is deprecated!
154157
return [
155158
f
156159
for f in all_inputs
@@ -184,7 +187,9 @@ def _run_rollup(ctx, sources, config, output, map_output = None):
184187
# These deps are identified by the NodeModuleInfo provider.
185188
for d in ctx.attr.deps:
186189
if NodeModuleInfo in d:
187-
direct_inputs += _filter_js_inputs(d.files)
190+
191+
# Note: we can't avoid calling .to_list() on files
192+
direct_inputs += _filter_js_inputs(d.files.to_list())
188193

189194
if ctx.file.license_banner:
190195
direct_inputs += [ctx.file.license_banner]

0 commit comments

Comments
 (0)