Skip to content

Commit 3c1a089

Browse files
committed
fix: stop copying targets, consume directly
Since we have downloaded_file_path that points to the actual jar name in our http_file invocations we can directly consume from those targets instead of copying, this has an impact not just in IO as we reduce copying, but also reduces the amount of targets bazel is aware as we don't need an extra node, reducing the amount of RAM needed for rules_jvm_external slightly. In large maven repositories we use at Booking.com we saw a decrease in the disk cache from 2.4GB to 300MB, which aligns with a repository_cache of roughly 2.1GB. The time it takes to do a `bazel build @maven//...` went from 120 seconds to 50 seconds on a i9-13900H using 75% of the cores.
1 parent b29be75 commit 3c1a089

File tree

2 files changed

+22
-36
lines changed

2 files changed

+22
-36
lines changed

private/dependency_tree_parser.bzl

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ def _find_repository_url(artifact_url, repositories):
6666
longest_match = repository
6767
return longest_match
6868

69+
_ARTIFACT_JAR = """
70+
alias(
71+
name = "{artifact_path}",
72+
actual = "{source}",
73+
visibility = ["//visibility:public"],
74+
)"""
75+
6976
def _generate_target(
7077
repository_ctx,
7178
jar_versionless_target_labels,
@@ -320,18 +327,6 @@ genrule(
320327
to_return.append("alias(\n\tname = \"%s\",\n\tactual = \"%s\",\n%s)" %
321328
(versioned_target_alias_label, target_label, alias_visibility))
322329

323-
# 11. If using maven_install.json, use a genrule to copy the file from the http_file
324-
# repository into this repository.
325-
#
326-
# genrule(
327-
# name = "org_hamcrest_hamcrest_library_1_3_extension",
328-
# srcs = ["@org_hamcrest_hamcrest_library_1_3//file"],
329-
# outs = ["@maven//:v1/https/repo1.maven.org/maven2/org/hamcrest/hamcrest-library/1.3/hamcrest-library-1.3.jar"],
330-
# cmd = "cp $< $@",
331-
# )
332-
if repository_ctx.attr.maven_install_json:
333-
to_return.append(_genrule_copy_artifact_from_http_file(artifact, default_visibilities))
334-
335330
return to_return
336331

337332
# Generate BUILD file with jvm_import and aar_import for each artifact in
@@ -351,6 +346,8 @@ def _generate_imports(repository_ctx, dependencies, explicit_artifacts, neverlin
351346
# seen_imports :: string -> bool
352347
seen_imports = {}
353348

349+
added_aliases = {}
350+
354351
# A list of versionless target labels for jar artifacts. This is used for
355352
# generating a compatibility layer for repositories. For example, if we generate
356353
# @maven//:junit_junit, we also generate @junit_junit//jar as an alias to it.
@@ -378,8 +375,6 @@ def _generate_imports(repository_ctx, dependencies, explicit_artifacts, neverlin
378375
seen_imports[artifact_path] = True
379376
target_label = escape(strip_packaging_and_classifier_and_version(artifact["coordinates"]))
380377
srcjar_paths[target_label] = artifact_path
381-
if repository_ctx.attr.maven_install_json:
382-
all_imports.append(_genrule_copy_artifact_from_http_file(artifact, default_visibilities))
383378

384379
# Iterate through the list of artifacts, and generate the target declaration strings.
385380
for artifact in dependencies:
@@ -409,37 +404,28 @@ def _generate_imports(repository_ctx, dependencies, explicit_artifacts, neverlin
409404
all_imports.append(
410405
"alias(\n\tname = \"%s\",\n\tactual = \"%s\",\n\tvisibility = [\"//visibility:public\"],\n)" % (target_label, versioned_target_alias_label),
411406
)
412-
if repository_ctx.attr.maven_install_json:
413-
all_imports.append(_genrule_copy_artifact_from_http_file(artifact, default_visibilities))
414407
elif target_label in labels_to_override:
415408
# Override target labels with the user provided mapping, instead of generating
416409
# a jvm_import/aar_import based on information in dep_tree.
417410
seen_imports[target_label] = True
418411
all_imports.append(
419412
"alias(\n\tname = \"%s\",\n\tactual = \"%s\",\n\tvisibility = [\"//visibility:public\"],)" % (target_label, labels_to_override.get(target_label)),
420413
)
421-
if repository_ctx.attr.maven_install_json:
422-
# Provide the downloaded artifact as a file target.
423-
all_imports.append(_genrule_copy_artifact_from_http_file(artifact, default_visibilities))
424414
raw_artifact = dict(artifact)
425-
raw_artifact["coordinates"] = "original_" + artifact["coordinates"]
426415
raw_artifact["maven_coordinates"] = artifact["coordinates"]
427-
raw_artifact["out"] = "original_" + artifact["file"]
428-
429-
all_imports.extend(_generate_target(
430-
repository_ctx,
431-
jar_versionless_target_labels,
432-
explicit_artifacts,
433-
srcjar_paths,
434-
labels_to_override,
435-
repository_urls,
436-
neverlink_artifacts,
437-
testonly_artifacts,
438-
default_visibilities,
439-
raw_artifact,
440-
))
416+
raw_artifact["out"] = "@%s//file" % escape(artifact["coordinates"])
441417

442418
elif artifact_path != None:
419+
if artifact["file"] not in added_aliases:
420+
added_aliases[artifact["file"]] = True
421+
repo = escape(artifact["coordinates"])
422+
all_imports.append(
423+
_ARTIFACT_JAR.format(
424+
artifact_path = artifact["file"],
425+
source = "@%s//file" % repo
426+
)
427+
)
428+
443429
all_imports.extend(_generate_target(
444430
repository_ctx,
445431
jar_versionless_target_labels,

private/rules/jvm_import.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def _jvm_import_impl(ctx):
2525

2626
injar = ctx.files.jars[0]
2727
if ctx.attr._stamp_manifest[StampManifestProvider].stamp_enabled:
28-
outjar = ctx.actions.declare_file("processed_" + injar.basename, sibling = injar)
28+
outjar = ctx.actions.declare_file("processed_" + injar.basename)
2929
args = ctx.actions.args()
3030
args.add_all(["--source", injar, "--output", outjar])
3131
args.add_all(["--manifest-entry", "Target-Label:{target_label}".format(target_label = label)])
@@ -40,7 +40,7 @@ def _jvm_import_impl(ctx):
4040
else:
4141
outjar = injar
4242

43-
compilejar = ctx.actions.declare_file("header_" + injar.basename, sibling = injar)
43+
compilejar = ctx.actions.declare_file("header_" + injar.basename)
4444
args = ctx.actions.args()
4545
args.add_all(["--source", outjar, "--output", compilejar])
4646

0 commit comments

Comments
 (0)