From 19cd2423916fa100f96b7795ad3bc22a3835e27a Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Thu, 26 Sep 2024 15:56:36 +0200 Subject: [PATCH] Weakly cache runfiles mappings of tools Benchmarks showed that this can reduce eden space garbage by 1-4%. --- .../devtools/build/lib/analysis/RunfilesSupport.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/analysis/RunfilesSupport.java b/src/main/java/com/google/devtools/build/lib/analysis/RunfilesSupport.java index ee159b6bb25605..ce5085d2664243 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/RunfilesSupport.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/RunfilesSupport.java @@ -236,13 +236,17 @@ public String getWorkspaceName() { private final CommandLine args; private final ActionEnvironment actionEnvironment; - // Only cache runfiles if there is more than one test runner action. Otherwise, there is no chance - // for reusing the runfiles within a single build, so don't pay the overhead of a weak reference. + // Only cache mappings if there is a chance that more than one action will use it within a single + // build. This helps reduce peak memory usage, especially when the value of --jobs is high, but + // avoids the additional overhead of a weak reference when it is not needed. private static boolean cacheRunfilesMappings(RuleContext ruleContext) { if (!TargetUtils.isTestRule(ruleContext.getTarget())) { - return false; + // Runfiles trees of non-test rules are tools and can thus be used by multiple actions. + return true; } + // Test runfiles are only used by a single test runner action unless there are multiple runs or + // shards. if (TestActionBuilder.getRunsPerTest(ruleContext) > 1) { return true; }