Skip to content

Commit

Permalink
perf(npm): avoid large array concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Aug 9, 2024
1 parent acb0f7e commit da7c264
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
24 changes: 17 additions & 7 deletions js/private/js_helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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 = {
Expand Down
15 changes: 10 additions & 5 deletions npm/private/npm_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
),
]

Expand Down

0 comments on commit da7c264

Please sign in to comment.