Skip to content

Commit 4a2e51b

Browse files
justinhorvitzcopybara-github
authored andcommitted
Make getPrimaryOutput() always return the first artifact in getOutputs().
This is already the case everywhere except `CppCompileAction` but was not documented, leading to `SpawnAction` unnecessarily storing a field for the primary output when it's already just the first element in its outputs. This change saves 8 bytes per `SpawnAction` and `GenRuleAction`, and moves other `SpawnAction` subclasses closer to an 8-byte threshold. `CppCompileAction` had been using the coverage artifact (if present) as the first output but not the primary output. This is easy to change by storing the coverage artifact in a field instead of the primary output. It also removes a boolean field since we can just check the nullness of the coverage artifact field, although this does not change the shallow heap of `CppCompileAction`. PiperOrigin-RevId: 523196083 Change-Id: Id6fe4ef126334a2b4cd2d2dc63409e6668e5c8bd
1 parent ef9ccaa commit 4a2e51b

13 files changed

+78
-149
lines changed

src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,8 @@ public Artifact getPrimaryInput() {
309309
}
310310

311311
@Override
312-
public Artifact getPrimaryOutput() {
313-
// Default behavior is to return the first output artifact.
314-
// Use the method rather than field in case of overriding in subclasses.
315-
return Iterables.getFirst(getOutputs(), null);
312+
public final Artifact getPrimaryOutput() {
313+
return Iterables.get(outputs, 0);
316314
}
317315

318316
@Override

src/main/java/com/google/devtools/build/lib/actions/ActionAnalysisMetadata.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ NestedSet<Artifact> getInputFilesForExtraAction(ActionExecutionContext actionExe
186186
Artifact getPrimaryInput();
187187

188188
/**
189-
* Returns the "primary" output of this action.
189+
* Returns the "primary" output of this action, which is the same as the first artifact in {@link
190+
* #getOutputs}.
190191
*
191192
* <p>For example, the linked library would be the primary output of a LinkAction.
192193
*

src/main/java/com/google/devtools/build/lib/actions/ActionExecutionMetadata.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ default String getProgressMessage(RepositoryMapping mainRepositoryMapping) {
4848
}
4949

5050
/**
51-
* Returns a human-readable description of the inputs to {@link #getKey(ActionKeyContext)}. Used
52-
* in the output from '--explain', and in error messages for '--check_up_to_date' and
53-
* '--check_tests_up_to_date'. May return null, meaning no extra information is available.
51+
* Returns a human-readable description of the inputs to {@link #getKey}. Used in the output from
52+
* '--explain', and in error messages for '--check_up_to_date' and '--check_tests_up_to_date'. May
53+
* return null, meaning no extra information is available.
5454
*
5555
* <p>If the return value is non-null, for consistency it should be a multiline message of the
5656
* form:

src/main/java/com/google/devtools/build/lib/analysis/actions/SpawnAction.java

+8-31
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.devtools.build.lib.analysis.actions;
1616

17+
import static com.google.common.collect.ImmutableSet.toImmutableSet;
1718
import static com.google.devtools.build.lib.actions.ActionAnalysisMetadata.mergeMaps;
1819
import static com.google.devtools.build.lib.packages.ExecGroup.DEFAULT_EXEC_GROUP_NAME;
1920

@@ -103,7 +104,7 @@ public interface ExtraActionInfoSupplier {
103104

104105
private static final String GUID = "ebd6fce3-093e-45ee-adb6-bf513b602f0d";
105106

106-
public static final Interner<ImmutableSortedMap<String, String>> executionInfoInterner =
107+
private static final Interner<ImmutableSortedMap<String, String>> executionInfoInterner =
107108
BlazeInterners.newWeakInterner();
108109

109110
private final CommandLines commandLines;
@@ -118,7 +119,6 @@ public interface ExtraActionInfoSupplier {
118119
private final ImmutableMap<String, String> executionInfo;
119120

120121
private final ExtraActionInfoSupplier extraActionInfoSupplier;
121-
private final Artifact primaryOutput;
122122
private final Consumer<Pair<ActionExecutionContext, List<SpawnResult>>> resultConsumer;
123123
private final boolean stripOutputPaths;
124124

@@ -132,7 +132,6 @@ public interface ExtraActionInfoSupplier {
132132
* @param inputs the set of all files potentially read by this action; must not be subsequently
133133
* modified.
134134
* @param outputs the set of all files written by this action; must not be subsequently modified.
135-
* @param primaryOutput the primary output of this action
136135
* @param resourceSetOrBuilder the resources consumed by executing this Action.
137136
* @param env the action environment
138137
* @param commandLines the command lines to execute. This includes the main argv vector and any
@@ -148,7 +147,6 @@ public SpawnAction(
148147
NestedSet<Artifact> tools,
149148
NestedSet<Artifact> inputs,
150149
Iterable<Artifact> outputs,
151-
Artifact primaryOutput,
152150
ResourceSetOrBuilder resourceSetOrBuilder,
153151
CommandLines commandLines,
154152
CommandLineLimits commandLineLimits,
@@ -161,7 +159,6 @@ public SpawnAction(
161159
tools,
162160
inputs,
163161
outputs,
164-
primaryOutput,
165162
resourceSetOrBuilder,
166163
commandLines,
167164
commandLineLimits,
@@ -188,7 +185,6 @@ public SpawnAction(
188185
* @param inputs the set of all files potentially read by this action; must not be subsequently
189186
* modified
190187
* @param outputs the set of all files written by this action; must not be subsequently modified.
191-
* @param primaryOutput the primary output of this action
192188
* @param resourceSetOrBuilder the resources consumed by executing this Action.
193189
* @param env the action's environment
194190
* @param executionInfo out-of-band information for scheduling the spawn
@@ -206,7 +202,6 @@ public SpawnAction(
206202
NestedSet<Artifact> tools,
207203
NestedSet<Artifact> inputs,
208204
Iterable<? extends Artifact> outputs,
209-
Artifact primaryOutput,
210205
ResourceSetOrBuilder resourceSetOrBuilder,
211206
CommandLines commandLines,
212207
CommandLineLimits commandLineLimits,
@@ -221,7 +216,6 @@ public SpawnAction(
221216
Consumer<Pair<ActionExecutionContext, List<SpawnResult>>> resultConsumer,
222217
boolean stripOutputPaths) {
223218
super(owner, tools, inputs, runfilesSupplier, outputs, env);
224-
this.primaryOutput = primaryOutput;
225219
this.resourceSetOrBuilder = resourceSetOrBuilder;
226220
this.executionInfo =
227221
executionInfo.isEmpty()
@@ -238,11 +232,6 @@ public SpawnAction(
238232
this.stripOutputPaths = stripOutputPaths;
239233
}
240234

241-
@Override
242-
public Artifact getPrimaryOutput() {
243-
return primaryOutput;
244-
}
245-
246235
@VisibleForTesting
247236
public CommandLines getCommandLines() {
248237
return commandLines;
@@ -261,12 +250,10 @@ public List<String> getArguments() throws CommandLineExpansionException, Interru
261250
}
262251

263252
@Override
264-
public Sequence<CommandLineArgsApi> getStarlarkArgs() throws EvalException {
253+
public Sequence<CommandLineArgsApi> getStarlarkArgs() {
265254
ImmutableList.Builder<CommandLineArgsApi> result = ImmutableList.builder();
266255
ImmutableSet<Artifact> directoryInputs =
267-
getInputs().toList().stream()
268-
.filter(artifact -> artifact.isDirectory())
269-
.collect(ImmutableSet.toImmutableSet());
256+
getInputs().toList().stream().filter(Artifact::isDirectory).collect(toImmutableSet());
270257

271258
for (CommandLineAndParamFileInfo commandLine : commandLines.getCommandLines()) {
272259
result.add(Args.forRegisteredAction(commandLine, directoryInputs));
@@ -536,7 +523,7 @@ public ExtraActionInfo.Builder getExtraActionInfo(ActionKeyContext actionKeyCont
536523
* <p>Subclasses of SpawnAction may override this in order to provide action-specific behaviour.
537524
* This can be necessary, for example, when the action discovers inputs.
538525
*/
539-
protected SpawnInfo getExtraActionSpawnInfo()
526+
private SpawnInfo getExtraActionSpawnInfo()
540527
throws CommandLineExpansionException, InterruptedException {
541528
SpawnInfo.Builder info = SpawnInfo.newBuilder();
542529
Spawn spawn = getSpawnForExtraAction();
@@ -601,7 +588,7 @@ private ActionSpawn(
601588
throws CommandLineExpansionException {
602589
super(
603590
arguments,
604-
ImmutableMap.<String, String>of(),
591+
ImmutableMap.of(),
605592
parent.getExecutionInfo(),
606593
parent.getRunfilesSupplier(),
607594
parent,
@@ -805,8 +792,7 @@ private SpawnAction buildSpawnAction(
805792
owner,
806793
tools,
807794
inputsAndTools,
808-
ImmutableList.copyOf(outputs),
809-
outputs.get(0),
795+
ImmutableSet.copyOf(outputs),
810796
resourceSetOrBuilder,
811797
commandLines,
812798
commandLineLimits,
@@ -826,8 +812,7 @@ protected SpawnAction createSpawnAction(
826812
ActionOwner owner,
827813
NestedSet<Artifact> tools,
828814
NestedSet<Artifact> inputsAndTools,
829-
ImmutableList<Artifact> outputs,
830-
Artifact primaryOutput,
815+
ImmutableSet<Artifact> outputs,
831816
ResourceSetOrBuilder resourceSetOrBuilder,
832817
CommandLines commandLines,
833818
CommandLineLimits commandLineLimits,
@@ -843,7 +828,6 @@ protected SpawnAction createSpawnAction(
843828
tools,
844829
inputsAndTools,
845830
outputs,
846-
primaryOutput,
847831
resourceSetOrBuilder,
848832
commandLines,
849833
commandLineLimits,
@@ -949,13 +933,6 @@ public Builder addOutputs(Iterable<Artifact> artifacts) {
949933
return this;
950934
}
951935

952-
/**
953-
* Checks whether the action produces any outputs
954-
*/
955-
public boolean hasOutputs() {
956-
return !outputs.isEmpty();
957-
}
958-
959936
/**
960937
* Sets RecourceSet for builder. If ResourceSetBuilder set, then ResourceSetBuilder will
961938
* override setResources.

src/main/java/com/google/devtools/build/lib/analysis/actions/StarlarkAction.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import com.google.common.annotations.VisibleForTesting;
1919
import com.google.common.collect.ImmutableList;
2020
import com.google.common.collect.ImmutableMap;
21+
import com.google.common.collect.ImmutableSet;
22+
import com.google.common.collect.Iterables;
2123
import com.google.common.collect.Maps;
2224
import com.google.common.collect.Sets;
2325
import com.google.devtools.build.lib.actions.Action;
@@ -81,7 +83,6 @@ public final class StarlarkAction extends SpawnAction implements ActionCacheAwar
8183
* @param inputs the set of all files potentially read by this action; must not be subsequently
8284
* modified
8385
* @param outputs the set of all files written by this action; must not be subsequently modified.
84-
* @param primaryOutput the primary output of this action
8586
* @param resourceSetOrBuilder the resources consumed by executing this Action
8687
* @param commandLines the command lines to execute. This includes the main argv vector and any
8788
* param file-backed command lines.
@@ -103,7 +104,6 @@ private StarlarkAction(
103104
NestedSet<Artifact> tools,
104105
NestedSet<Artifact> inputs,
105106
Iterable<Artifact> outputs,
106-
Artifact primaryOutput,
107107
ResourceSetOrBuilder resourceSetOrBuilder,
108108
CommandLines commandLines,
109109
CommandLineLimits commandLineLimits,
@@ -123,7 +123,6 @@ private StarlarkAction(
123123
? createInputs(shadowedAction.get().getInputs(), inputs)
124124
: inputs,
125125
outputs,
126-
primaryOutput,
127126
resourceSetOrBuilder,
128127
commandLines,
129128
commandLineLimits,
@@ -376,8 +375,7 @@ protected SpawnAction createSpawnAction(
376375
ActionOwner owner,
377376
NestedSet<Artifact> tools,
378377
NestedSet<Artifact> inputsAndTools,
379-
ImmutableList<Artifact> outputs,
380-
Artifact primaryOutput,
378+
ImmutableSet<Artifact> outputs,
381379
ResourceSetOrBuilder resourceSetOrBuilder,
382380
CommandLines commandLines,
383381
CommandLineLimits commandLineLimits,
@@ -403,7 +401,6 @@ protected SpawnAction createSpawnAction(
403401
tools,
404402
inputsAndTools,
405403
outputs,
406-
primaryOutput,
407404
resourceSetOrBuilder,
408405
commandLines,
409406
commandLineLimits,
@@ -415,7 +412,7 @@ protected SpawnAction createSpawnAction(
415412
mnemonic,
416413
unusedInputsList,
417414
shadowedAction,
418-
stripOutputPaths(mnemonic, inputsAndTools, primaryOutput, configuration));
415+
stripOutputPaths(mnemonic, inputsAndTools, outputs, configuration));
419416
}
420417

421418
/**
@@ -435,7 +432,7 @@ protected SpawnAction createSpawnAction(
435432
private static boolean stripOutputPaths(
436433
String mnemonic,
437434
NestedSet<Artifact> inputs,
438-
Artifact primaryOutput,
435+
ImmutableSet<Artifact> outputs,
439436
BuildConfigurationValue configuration) {
440437
ImmutableList<String> qualifyingMnemonics =
441438
ImmutableList.of(
@@ -456,7 +453,8 @@ private static boolean stripOutputPaths(
456453
CoreOptions coreOptions = configuration.getOptions().get(CoreOptions.class);
457454
return coreOptions.outputPathsMode == OutputPathsMode.STRIP
458455
&& qualifyingMnemonics.contains(mnemonic)
459-
&& PathStripper.isPathStrippable(inputs, primaryOutput.getExecPath().subFragment(0, 1));
456+
&& PathStripper.isPathStrippable(
457+
inputs, Iterables.get(outputs, 0).getExecPath().subFragment(0, 1));
460458
}
461459
}
462460
}

src/main/java/com/google/devtools/build/lib/analysis/extra/ExtraAction.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414

1515
package com.google.devtools.build.lib.analysis.extra;
1616

17-
import com.google.common.base.Function;
1817
import com.google.common.base.Preconditions;
1918
import com.google.common.collect.ImmutableMap;
20-
import com.google.common.collect.Iterables;
2119
import com.google.common.collect.Sets;
2220
import com.google.devtools.build.lib.actions.AbstractAction;
2321
import com.google.devtools.build.lib.actions.Action;
@@ -56,9 +54,6 @@ public final class ExtraAction extends SpawnAction {
5654
private final boolean createDummyOutput;
5755
private final NestedSet<Artifact> extraActionInputs;
5856

59-
public static final Function<ExtraAction, Action> GET_SHADOWED_ACTION =
60-
e -> e != null ? e.getShadowedAction() : null;
61-
6257
ExtraAction(
6358
NestedSet<Artifact> extraActionInputs,
6459
RunfilesSupplier runfilesSupplier,
@@ -78,7 +73,6 @@ public final class ExtraAction extends SpawnAction {
7873
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
7974
extraActionInputs),
8075
outputs,
81-
Iterables.getFirst(outputs, null),
8276
AbstractAction.DEFAULT_RESOURCE_SET,
8377
CommandLines.of(argv),
8478
CommandLineLimits.UNLIMITED,
@@ -128,7 +122,7 @@ public NestedSet<Artifact> discoverInputs(ActionExecutionContext actionExecution
128122
updateInputs(
129123
createInputs(shadowedAction.getInputs(), inputFilesForExtraAction, extraActionInputs));
130124
return NestedSetBuilder.wrap(
131-
Order.STABLE_ORDER, Sets.<Artifact>difference(getInputs().toSet(), oldInputs.toSet()));
125+
Order.STABLE_ORDER, Sets.difference(getInputs().toSet(), oldInputs.toSet()));
132126
}
133127

134128
private static NestedSet<Artifact> createInputs(

src/main/java/com/google/devtools/build/lib/analysis/test/TestRunnerAction.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ public void setupEnvVariables(Map<String, String> env, Duration timeout) {
676676
env.put("TEST_RANDOM_SEED", Integer.toString(getRunNumber() + 1));
677677
}
678678
// TODO(b/184206260): Actually set TEST_RANDOM_SEED with random seed.
679-
// The above TEST_RANDOM_SEED has histroically been set with the run number, but we should
679+
// The above TEST_RANDOM_SEED has historically been set with the run number, but we should
680680
// explicitly set TEST_RUN_NUMBER to indicate the run number and actually set TEST_RANDOM_SEED
681681
// with a random seed. However, much code has come to depend on it being set to the run number
682682
// and this is an externally documented behavior. Modifying TEST_RANDOM_SEED should be done
@@ -923,11 +923,6 @@ public String getRunfilesPrefix() {
923923
return workspaceName;
924924
}
925925

926-
@Override
927-
public Artifact getPrimaryOutput() {
928-
return testLog;
929-
}
930-
931926
public PackageSpecificationProvider getNetworkAllowlist() {
932927
return networkAllowlist;
933928
}

src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,7 @@ private void createModuleCodegenAction(
17321732
configuration, featureConfiguration, builder, ruleErrorConsumer);
17331733
CppCompileAction compileAction = builder.buildOrThrowRuleError(ruleErrorConsumer);
17341734
actionRegistry.registerAction(compileAction);
1735-
Artifact objectFile = compileAction.getOutputFile();
1735+
Artifact objectFile = compileAction.getPrimaryOutput();
17361736
if (pic) {
17371737
result.addPicObjectFile(objectFile);
17381738
} else {
@@ -1775,7 +1775,7 @@ private void createHeaderAction(
17751775
configuration, featureConfiguration, builder, ruleErrorConsumer);
17761776
CppCompileAction compileAction = builder.buildOrThrowRuleError(ruleErrorConsumer);
17771777
actionRegistry.registerAction(compileAction);
1778-
Artifact tokenFile = compileAction.getOutputFile();
1778+
Artifact tokenFile = compileAction.getPrimaryOutput();
17791779
result.addHeaderTokenFile(tokenFile);
17801780
}
17811781

@@ -1868,13 +1868,13 @@ private ImmutableList<Artifact> createSourceAction(
18681868
configuration, featureConfiguration, picBuilder, ruleErrorConsumer);
18691869
CppCompileAction picAction = picBuilder.buildOrThrowRuleError(ruleErrorConsumer);
18701870
actionRegistry.registerAction(picAction);
1871-
directOutputs.add(picAction.getOutputFile());
1871+
directOutputs.add(picAction.getPrimaryOutput());
18721872
if (addObject) {
1873-
result.addPicObjectFile(picAction.getOutputFile());
1873+
result.addPicObjectFile(picAction.getPrimaryOutput());
18741874

18751875
if (bitcodeOutput) {
18761876
result.addLtoBitcodeFile(
1877-
picAction.getOutputFile(), ltoIndexingFile, getCopts(sourceArtifact, sourceLabel));
1877+
picAction.getPrimaryOutput(), ltoIndexingFile, getCopts(sourceArtifact, sourceLabel));
18781878
}
18791879
}
18801880
if (dwoFile != null) {
@@ -1939,7 +1939,7 @@ private ImmutableList<Artifact> createSourceAction(
19391939
configuration, featureConfiguration, builder, ruleErrorConsumer);
19401940
CppCompileAction compileAction = builder.buildOrThrowRuleError(ruleErrorConsumer);
19411941
actionRegistry.registerAction(compileAction);
1942-
Artifact objectFile = compileAction.getOutputFile();
1942+
Artifact objectFile = compileAction.getPrimaryOutput();
19431943
directOutputs.add(objectFile);
19441944
if (addObject) {
19451945
result.addObjectFile(objectFile);
@@ -2118,7 +2118,7 @@ private ImmutableList<Artifact> createTempsActions(
21182118
CppCompileAction sdAction = sdBuilder.buildOrThrowRuleError(ruleErrorConsumer);
21192119
actionRegistry.registerAction(sdAction);
21202120

2121-
return ImmutableList.of(dAction.getOutputFile(), sdAction.getOutputFile());
2121+
return ImmutableList.of(dAction.getPrimaryOutput(), sdAction.getPrimaryOutput());
21222122
}
21232123

21242124
/**

0 commit comments

Comments
 (0)