From c343d690a915a0ad8b94cee4d56a958c9a79c251 Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Wed, 21 Aug 2024 12:40:12 -0700 Subject: [PATCH] perf: reduce concatenation, use of .extends --- e2e/pnpm_lockfiles/v54/snapshots/defs.bzl | 4 ++-- e2e/pnpm_lockfiles/v60/snapshots/defs.bzl | 4 ++-- e2e/pnpm_lockfiles/v61/snapshots/defs.bzl | 4 ++-- e2e/pnpm_lockfiles/v90/snapshots/defs.bzl | 4 ++-- js/private/js_binary.bzl | 10 +++++----- js/private/js_helpers.bzl | 7 +++++-- npm/private/npm_translate_lock_generate.bzl | 12 ++++++------ npm/private/npm_translate_lock_helpers.bzl | 4 ++-- npm/private/test/snapshots/bzlmod/npm_defs.bzl | 4 ++-- npm/private/test/snapshots/wksp/npm_defs.bzl | 4 ++-- 10 files changed, 30 insertions(+), 27 deletions(-) diff --git a/e2e/pnpm_lockfiles/v54/snapshots/defs.bzl b/e2e/pnpm_lockfiles/v54/snapshots/defs.bzl index 42d95fcaa1..ab13c22787 100644 --- a/e2e/pnpm_lockfiles/v54/snapshots/defs.bzl +++ b/e2e/pnpm_lockfiles/v54/snapshots/defs.bzl @@ -86,11 +86,11 @@ def npm_link_all_packages(name = "node_modules", imported_links = []): for link_fn in imported_links: new_link_targets, new_scope_targets = link_fn(name) - link_targets.extend(new_link_targets) + link_targets += new_link_targets for _scope, _targets in new_scope_targets.items(): if _scope not in scope_targets: scope_targets[_scope] = [] - scope_targets[_scope].extend(_targets) + scope_targets[_scope] += _targets if is_root: store_0(name = "{}/ansi-regex".format(name)) diff --git a/e2e/pnpm_lockfiles/v60/snapshots/defs.bzl b/e2e/pnpm_lockfiles/v60/snapshots/defs.bzl index 070836a188..284e6530b9 100644 --- a/e2e/pnpm_lockfiles/v60/snapshots/defs.bzl +++ b/e2e/pnpm_lockfiles/v60/snapshots/defs.bzl @@ -87,11 +87,11 @@ def npm_link_all_packages(name = "node_modules", imported_links = []): for link_fn in imported_links: new_link_targets, new_scope_targets = link_fn(name) - link_targets.extend(new_link_targets) + link_targets += new_link_targets for _scope, _targets in new_scope_targets.items(): if _scope not in scope_targets: scope_targets[_scope] = [] - scope_targets[_scope].extend(_targets) + scope_targets[_scope] += _targets if is_root: store_0(name = "{}/ansi-regex".format(name)) diff --git a/e2e/pnpm_lockfiles/v61/snapshots/defs.bzl b/e2e/pnpm_lockfiles/v61/snapshots/defs.bzl index 070836a188..284e6530b9 100644 --- a/e2e/pnpm_lockfiles/v61/snapshots/defs.bzl +++ b/e2e/pnpm_lockfiles/v61/snapshots/defs.bzl @@ -87,11 +87,11 @@ def npm_link_all_packages(name = "node_modules", imported_links = []): for link_fn in imported_links: new_link_targets, new_scope_targets = link_fn(name) - link_targets.extend(new_link_targets) + link_targets += new_link_targets for _scope, _targets in new_scope_targets.items(): if _scope not in scope_targets: scope_targets[_scope] = [] - scope_targets[_scope].extend(_targets) + scope_targets[_scope] += _targets if is_root: store_0(name = "{}/ansi-regex".format(name)) diff --git a/e2e/pnpm_lockfiles/v90/snapshots/defs.bzl b/e2e/pnpm_lockfiles/v90/snapshots/defs.bzl index 612e83c615..7f580626c6 100644 --- a/e2e/pnpm_lockfiles/v90/snapshots/defs.bzl +++ b/e2e/pnpm_lockfiles/v90/snapshots/defs.bzl @@ -87,11 +87,11 @@ def npm_link_all_packages(name = "node_modules", imported_links = []): for link_fn in imported_links: new_link_targets, new_scope_targets = link_fn(name) - link_targets.extend(new_link_targets) + link_targets += new_link_targets for _scope, _targets in new_scope_targets.items(): if _scope not in scope_targets: scope_targets[_scope] = [] - scope_targets[_scope].extend(_targets) + scope_targets[_scope] += _targets if is_root: store_0(name = "{}/ansi-regex".format(name)) diff --git a/js/private/js_binary.bzl b/js/private/js_binary.bzl index 33e71d92ad..c9050bdd83 100644 --- a/js/private/js_binary.bzl +++ b/js/private/js_binary.bzl @@ -522,16 +522,16 @@ def _create_launcher(ctx, log_prefix_rule_set, log_prefix_rule, fixed_args = [], bash_launcher, toolchain_files = _bash_launcher(ctx, nodeinfo, entry_point_path, log_prefix_rule_set, log_prefix_rule, fixed_args, fixed_env, is_windows) launcher = create_windows_native_launcher_script(ctx, bash_launcher) if is_windows else bash_launcher - launcher_files = [bash_launcher] - launcher_files.extend(toolchain_files) + launcher_files = [bash_launcher] + toolchain_files if hasattr(nodeinfo, "node"): if nodeinfo.node: launcher_files.append(nodeinfo.node) else: # TODO(3.0): drop support for deprecated toolchain attributes - launcher_files.extend(nodeinfo.tool_files) + launcher_files += nodeinfo.tool_files - launcher_files.extend(ctx.files._node_patches_files + [ctx.file._node_patches]) + launcher_files += ctx.files._node_patches_files + launcher_files.append(ctx.file._node_patches) transitive_launcher_files = None if ctx.attr.include_npm: if hasattr(nodeinfo, "npm_sources"): @@ -540,7 +540,7 @@ def _create_launcher(ctx, log_prefix_rule_set, log_prefix_rule, fixed_args = [], # TODO(3.0): drop support for deprecated toolchain attributes if not hasattr(nodeinfo, "npm_files"): fail("include_npm requires a minimum @rules_nodejs version of 5.7.0") - launcher_files.extend(nodeinfo.npm_files) + launcher_files += nodeinfo.npm_files runfiles = gather_runfiles( ctx = ctx, diff --git a/js/private/js_helpers.bzl b/js/private/js_helpers.bzl index c3a70ef8c4..3725c3e244 100644 --- a/js/private/js_helpers.bzl +++ b/js/private/js_helpers.bzl @@ -182,9 +182,12 @@ def gather_runfiles( for target in data: transitive_files_depsets.append(target[DefaultInfo].files) + # Avoid concatenating more then once + data_deps = data + deps + # Gather files from JsInfo providers of data & deps transitive_files_depsets.append(gather_files_from_js_infos( - targets = data + deps, + targets = data_deps, include_sources = include_sources, include_types = include_types, include_transitive_sources = include_transitive_sources, @@ -209,7 +212,7 @@ def gather_runfiles( transitive_files = depset(transitive = transitive_files_depsets), ).merge_all([ target[DefaultInfo].default_runfiles - for target in data + deps + for target in data_deps ]) LOG_LEVELS = { diff --git a/npm/private/npm_translate_lock_generate.bzl b/npm/private/npm_translate_lock_generate.bzl index b85445ae14..66aacac917 100644 --- a/npm/private/npm_translate_lock_generate.bzl +++ b/npm/private/npm_translate_lock_generate.bzl @@ -232,11 +232,11 @@ def npm_link_all_packages(name = "node_modules", imported_links = []): for link_fn in imported_links: new_link_targets, new_scope_targets = link_fn(name) - link_targets.extend(new_link_targets) + link_targets += new_link_targets for _scope, _targets in new_scope_targets.items(): if _scope not in scope_targets: scope_targets[_scope] = [] - scope_targets[_scope].extend(_targets) + scope_targets[_scope] += _targets """.format( defs_bzl_file = "@{}//:{}".format(rctx.name, rctx.attr.defs_bzl_filename), link_packages_comma_separated = "\"'\" + \"', '\".join(_LINK_PACKAGES) + \"'\"" if len(link_packages) else "\"\"", @@ -346,7 +346,7 @@ def npm_link_all_packages(name = "node_modules", imported_links = []): if len(stores_bzl) > 0: npm_link_all_packages_bzl.append(""" if is_root:""") - npm_link_all_packages_bzl.extend(stores_bzl) + npm_link_all_packages_bzl += stores_bzl if len(links_bzl) > 0: npm_link_all_packages_bzl.append(""" if link:""") @@ -356,7 +356,7 @@ def npm_link_all_packages(name = "node_modules", imported_links = []): els = "" if first_link else "el", pkg = link_package, )) - npm_link_all_packages_bzl.extend(bzl) + npm_link_all_packages_bzl += bzl first_link = False if len(links_targets_bzl) > 0: @@ -367,7 +367,7 @@ def npm_link_all_packages(name = "node_modules", imported_links = []): els = "" if first_link else "el", pkg = link_package, )) - npm_link_targets_bzl.extend(bzl) + npm_link_targets_bzl += bzl first_link = False for fp_link in fp_links.values(): @@ -470,7 +470,7 @@ def npm_link_all_packages(name = "node_modules", imported_links = []): other_statements.append(content) rctx_files[filename] = load_statements + rctx_files[filename] + other_statements else: - rctx_files[filename].extend(contents) + rctx_files[filename] += contents if not rctx.attr.bzlmod: rctx_files[rctx.attr.repositories_bzl_filename] = _generate_repositories( diff --git a/npm/private/npm_translate_lock_helpers.bzl b/npm/private/npm_translate_lock_helpers.bzl index 6a73176f46..5581f21283 100644 --- a/npm/private/npm_translate_lock_helpers.bzl +++ b/npm/private/npm_translate_lock_helpers.bzl @@ -57,7 +57,7 @@ def _gather_values_from_matching_names(additive, keyed_lists, *names): v = keyed_lists[name] if additive: if type(v) == "list": - result.extend(v) + result += v elif type(v) == "string": result.append(v) else: @@ -352,7 +352,7 @@ ERROR: can not apply both `pnpm.patchedDependencies` and `npm_translate_lock(pat elif len(translate_patches) > 0: patches = translate_patches patch_args, _ = _gather_values_from_matching_names(False, attr.patch_args, "*", name, friendly_name, unfriendly_name) - patches_used.extend(patches_keys) + patches_used += patches_keys # Resolve string patch labels relative to the root respository rather than relative to rules_js. # https://docs.google.com/document/d/1N81qfCa8oskCk5LqTW-LNthy6EBrDot7bdUsjz6JFC4/ diff --git a/npm/private/test/snapshots/bzlmod/npm_defs.bzl b/npm/private/test/snapshots/bzlmod/npm_defs.bzl index bd118d2fa8..86abd4b1ca 100644 --- a/npm/private/test/snapshots/bzlmod/npm_defs.bzl +++ b/npm/private/test/snapshots/bzlmod/npm_defs.bzl @@ -1007,11 +1007,11 @@ def npm_link_all_packages(name = "node_modules", imported_links = []): for link_fn in imported_links: new_link_targets, new_scope_targets = link_fn(name) - link_targets.extend(new_link_targets) + link_targets += new_link_targets for _scope, _targets in new_scope_targets.items(): if _scope not in scope_targets: scope_targets[_scope] = [] - scope_targets[_scope].extend(_targets) + scope_targets[_scope] += _targets if is_root: store_0(name = "{}/abbrev".format(name)) diff --git a/npm/private/test/snapshots/wksp/npm_defs.bzl b/npm/private/test/snapshots/wksp/npm_defs.bzl index 07a74a96d3..a52be9e612 100644 --- a/npm/private/test/snapshots/wksp/npm_defs.bzl +++ b/npm/private/test/snapshots/wksp/npm_defs.bzl @@ -1007,11 +1007,11 @@ def npm_link_all_packages(name = "node_modules", imported_links = []): for link_fn in imported_links: new_link_targets, new_scope_targets = link_fn(name) - link_targets.extend(new_link_targets) + link_targets += new_link_targets for _scope, _targets in new_scope_targets.items(): if _scope not in scope_targets: scope_targets[_scope] = [] - scope_targets[_scope].extend(_targets) + scope_targets[_scope] += _targets if is_root: store_0(name = "{}/abbrev".format(name))