Skip to content

Commit 8abcc5f

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 8abcc5f

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

private/dependency_tree_parser.bzl

Lines changed: 24 additions & 23 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,
@@ -87,7 +94,7 @@ def _generate_target(
8794
# (jvm|aar)_import(
8895
#
8996
packaging = artifact_path.split(".").pop()
90-
if packaging == "jar":
97+
if packaging == "jar" or artifact_path.endswith("//file"):
9198
# Regular `java_import` invokes ijar on all JARs, causing some Scala and
9299
# Kotlin compile interface JARs to be incorrect. We replace java_import
93100
# with a simple jvm_import Starlark rule that skips ijar.
@@ -120,7 +127,7 @@ def _generate_target(
120127
# srcjar = "https/repo1.maven.org/maven2/org/hamcrest/hamcrest-library/1.3/hamcrest-library-1.3-sources.jar",
121128
#
122129
is_dylib = False
123-
if packaging == "jar":
130+
if packaging == "jar" or artifact_path.endswith("//file"):
124131
target_import_string.append("\tjars = [\"%s\"]," % artifact_path)
125132
if srcjar_paths != None and target_label in srcjar_paths:
126133
target_import_string.append("\tsrcjar = \"%s\"," % srcjar_paths[target_label])
@@ -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,23 +404,18 @@ 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(
419-
"alias(\n\tname = \"%s\",\n\tactual = \"%s\",\n\tvisibility = [\"//visibility:public\"],)" % (target_label, labels_to_override.get(target_label)),
412+
"alias(\n\tname = \"%s\",\n\tactual = \"%s\",\n\tvisibility = [\"//visibility:public\"],\n)" % (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)
425415
raw_artifact["coordinates"] = "original_" + artifact["coordinates"]
426416
raw_artifact["maven_coordinates"] = artifact["coordinates"]
427417
raw_artifact["out"] = "original_" + artifact["file"]
428-
418+
raw_artifact["file"] = "@%s//file" % escape(artifact["coordinates"])
429419
all_imports.extend(_generate_target(
430420
repository_ctx,
431421
jar_versionless_target_labels,
@@ -439,7 +429,18 @@ def _generate_imports(repository_ctx, dependencies, explicit_artifacts, neverlin
439429
raw_artifact,
440430
))
441431

432+
442433
elif artifact_path != None:
434+
if artifact["file"] not in added_aliases:
435+
added_aliases[artifact["file"]] = True
436+
repo = escape(artifact["coordinates"])
437+
all_imports.append(
438+
_ARTIFACT_JAR.format(
439+
artifact_path = artifact["file"],
440+
source = "@%s//file" % repo
441+
)
442+
)
443+
443444
all_imports.extend(_generate_target(
444445
repository_ctx,
445446
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)