From da7c264e4a1ad03fa373dabfcc08251ed1426819 Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Fri, 7 Jun 2024 15:15:44 -0700 Subject: [PATCH] perf(npm): avoid large array concatenation --- js/private/js_helpers.bzl | 24 +++++++++++++++++------- npm/private/npm_package.bzl | 15 ++++++++++----- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/js/private/js_helpers.bzl b/js/private/js_helpers.bzl index c3a70ef8c4..359e6f8ef8 100644 --- a/js/private/js_helpers.bzl +++ b/js/private/js_helpers.bzl @@ -49,11 +49,18 @@ def gather_npm_sources(srcs, deps): Depset of npm sources """ - return depset(transitive = [ - target[JsInfo].npm_sources - for target in srcs + deps - if JsInfo in target - ]) + transitive = [] + + # Duplicate loops to avoid src + deps concat + for target in srcs: + if JsInfo in target: + transitive.append(target[JsInfo].npm_sources) + + for target in deps: + if JsInfo in target: + transitive.append(target[JsInfo].npm_sources) + + return depset([], transitive = transitive) def gather_npm_package_store_infos(targets): """Gathers NpmPackageStoreInfo providers from the list of targets @@ -182,9 +189,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 +219,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_package.bzl b/npm/private/npm_package.bzl index 4620478b4b..419959fbc0 100644 --- a/npm/private/npm_package.bzl +++ b/npm/private/npm_package.bzl @@ -16,6 +16,7 @@ load("@bazel_skylib//lib:dicts.bzl", "dicts") load("@bazel_skylib//lib:versions.bzl", "versions") load("//js:defs.bzl", "js_binary") load("//js:libs.bzl", "js_lib_helpers") +load("//js:providers.bzl", "JsInfo") load(":npm_package_info.bzl", "NpmPackageInfo") # Pull in all copy_to_directory attributes except for exclude_prefixes @@ -68,10 +69,14 @@ def _npm_package_files_impl(ctx): def _npm_package_impl(ctx): dst = ctx.actions.declare_directory(ctx.attr.out if ctx.attr.out else ctx.attr.name) - # forward all npm_package_store_infos - npm_package_store_infos = js_lib_helpers.gather_npm_package_store_infos( - targets = ctx.attr.srcs + ctx.attr.data, - ) + # forward all src+data npm_package_store_infos + npm_package_store_infos = [] + for target in ctx.attr.srcs: + if JsInfo in target: + npm_package_store_infos.append(target[JsInfo].npm_package_store_infos) + for target in ctx.attr.data: + if JsInfo in target: + npm_package_store_infos.append(target[JsInfo].npm_package_store_infos) copy_to_directory_bin_action( ctx, @@ -103,7 +108,7 @@ def _npm_package_impl(ctx): package = ctx.attr.package, version = ctx.attr.version, src = dst, - npm_package_store_infos = npm_package_store_infos, + npm_package_store_infos = depset(transitive = npm_package_store_infos), ), ]