Skip to content

Commit 519b5fd

Browse files
fmeumjustinhorvitz
andauthored
[7.6.0] Performance improvements for builds with many top-level targets (#25492)
* 3e115b9 * e12fdd9 --------- Co-authored-by: Googler <[email protected]>
1 parent b46d382 commit 519b5fd

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/main/java/com/google/devtools/build/lib/analysis/BuildView.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
// limitations under the License.
1414
package com.google.devtools.build.lib.analysis;
1515

16+
import static com.google.common.base.Preconditions.checkNotNull;
17+
import static com.google.common.base.Preconditions.checkState;
1618
import static com.google.common.collect.ImmutableList.toImmutableList;
1719

1820
import com.google.common.annotations.VisibleForTesting;
19-
import com.google.common.base.Preconditions;
2021
import com.google.common.collect.ImmutableList;
2122
import com.google.common.collect.ImmutableMap;
2223
import com.google.common.collect.ImmutableSet;
@@ -63,6 +64,7 @@
6364
import com.google.devtools.build.lib.packages.NativeAspectClass;
6465
import com.google.devtools.build.lib.packages.NoSuchPackageException;
6566
import com.google.devtools.build.lib.packages.NoSuchTargetException;
67+
import com.google.devtools.build.lib.packages.Package;
6668
import com.google.devtools.build.lib.packages.Rule;
6769
import com.google.devtools.build.lib.packages.StarlarkAspectClass;
6870
import com.google.devtools.build.lib.packages.Target;
@@ -166,7 +168,7 @@ public BuildView(
166168
this.directories = directories;
167169
this.coverageReportActionFactory = coverageReportActionFactory;
168170
this.ruleClassProvider = ruleClassProvider;
169-
this.skyframeExecutor = Preconditions.checkNotNull(skyframeExecutor);
171+
this.skyframeExecutor = checkNotNull(skyframeExecutor);
170172
this.skyframeBuildView = skyframeExecutor.getSkyframeBuildView();
171173
}
172174

@@ -232,13 +234,7 @@ public AnalysisResult update(
232234
skyframeBuildView.resetProgressReceiver();
233235
skyframeExecutor.setBaselineConfiguration(targetOptions);
234236

235-
ImmutableMap.Builder<Label, Target> labelToTargetsMapBuilder =
236-
ImmutableMap.builderWithExpectedSize(loadingResult.getTargetLabels().size());
237-
loadingResult
238-
.getTargets(eventHandler, skyframeExecutor.getPackageManager())
239-
.forEach(target -> labelToTargetsMapBuilder.put(target.getLabel(), target));
240-
ImmutableMap<Label, Target> labelToTargetMap = labelToTargetsMapBuilder.buildOrThrow();
241-
237+
ImmutableMap<Label, Target> labelToTargetMap = constructLabelToTargetMap(loadingResult);
242238
eventBus.post(new AnalysisPhaseStartedEvent(labelToTargetMap.values()));
243239

244240
// Prepare the analysis phase
@@ -291,7 +287,7 @@ public AnalysisResult update(
291287
(RuleContextConstraintSemantics) ruleClassProvider.getConstraintSemantics());
292288
// We wait until now to setup for execution, in case the artifact factory was reset
293289
// due to a config change.
294-
Preconditions.checkNotNull(executionSetupCallback).prepareForExecution();
290+
checkNotNull(executionSetupCallback).prepareForExecution();
295291
skyframeAnalysisResult =
296292
skyframeBuildView.analyzeAndExecuteTargets(
297293
eventHandler,
@@ -303,8 +299,8 @@ public AnalysisResult update(
303299
explicitTargetPatterns,
304300
eventBus,
305301
bugReporter,
306-
Preconditions.checkNotNull(resourceManager), // non-null for skymeld.
307-
Preconditions.checkNotNull(buildResultListener), // non-null for skymeld.
302+
checkNotNull(resourceManager), // non-null for skymeld.
303+
checkNotNull(buildResultListener), // non-null for skymeld.
308304
(configuredTargets, allTargetsToTest) ->
309305
memoizedGetCoverageArtifactsHelper(
310306
configuredTargets, allTargetsToTest, eventHandler, eventBus, loadingResult),
@@ -414,6 +410,20 @@ public AnalysisResult update(
414410
return result;
415411
}
416412

413+
private ImmutableMap<Label, Target> constructLabelToTargetMap(
414+
TargetPatternPhaseValue loadingResult) throws InterruptedException {
415+
ImmutableSet<Label> labels = loadingResult.getTargetLabels();
416+
ImmutableMap.Builder<Label, Target> builder =
417+
ImmutableMap.builderWithExpectedSize(labels.size());
418+
for (Label label : labels) {
419+
Package pkg =
420+
checkNotNull(skyframeExecutor.getExistingPackage(label.getPackageIdentifier()), label);
421+
Target target = checkNotNull(pkg.getTargets().get(label.getName()), label);
422+
builder.put(label, target);
423+
}
424+
return builder.buildOrThrow();
425+
}
426+
417427
private ImmutableList<TopLevelAspectsKey> createTopLevelAspectKeys(
418428
List<String> aspects,
419429
ImmutableMap<String, String> aspectsParameters,
@@ -533,7 +543,7 @@ private AnalysisResult createResult(
533543
// build-info and build-changelist.
534544
ImmutableList<Artifact> buildInfoArtifacts =
535545
skyframeExecutor.getWorkspaceStatusArtifacts(eventHandler);
536-
Preconditions.checkState(buildInfoArtifacts.size() == 2, buildInfoArtifacts);
546+
checkState(buildInfoArtifacts.size() == 2, buildInfoArtifacts);
537547

538548
// Extra actions
539549
addExtraActionsIfRequested(

src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2913,13 +2913,14 @@ public void evaluated(
29132913
skyKey, newValue, newError, evaluationSuccessState, state, directDeps);
29142914
}
29152915

2916-
// After a PACKAGE node is evaluated, all targets and the labels associated with this package
2917-
// should have been added to the InMemoryGraph. So it is safe to remove relevant labels from
2918-
// weak interner.
2916+
// After a PACKAGE node is freshly computed, all targets and the labels associated with this
2917+
// package should have been added to the InMemoryGraph. So it is safe to remove relevant
2918+
// labels from weak interner.
29192919
LabelInterner labelInterner = Label.getLabelInterner();
29202920
if (labelInterner.enabled()
29212921
&& skyKey.functionName().equals(SkyFunctions.PACKAGE)
2922-
&& newValue != null) {
2922+
&& newValue != null
2923+
&& directDeps != null) {
29232924
checkState(newValue instanceof PackageValue, newValue);
29242925

29252926
Package pkg = ((PackageValue) newValue).getPackage();
@@ -3654,7 +3655,7 @@ default ThreadStateReceiver makeThreadStateReceiver(SkyKey key) {
36543655
}
36553656

36563657
@Nullable
3657-
private Package getExistingPackage(PackageIdentifier id) throws InterruptedException {
3658+
public Package getExistingPackage(PackageIdentifier id) throws InterruptedException {
36583659
var value = (PackageValue) memoizingEvaluator.getExistingValue(id);
36593660
if (value == null) {
36603661
return null;

0 commit comments

Comments
 (0)