Move argStartIndexes field from StarlarkCustomCommandLine into a specialised class.
StarlarkCustomCommandLine took 1 pointer more, because it was storing argStartIndexes unconditionally. The pointer is only needed when flags_per_line is used.
This reduces retained heap on all Starlark command lines.
@@ -62,14 +62,7 @@ import net.starlark.java.syntax.Location;
@AutoCodec
public class StarlarkCustomCommandLine extends CommandLine {
- private final ImmutableList<Object> arguments;
- /**
- * If non-empty, an extra level of grouping on top of the 'arguments' list. Each element is the
- * beginning of a group of args. For example, if this contains 0 and 3, then arguments 0, 1 and 2
- * constitute the first group, and arguments 3 to the end constitute the next. The expanded
- * version of these arguments will be concatenated together to support flag_per_line format.
- */
- private final ImmutableList<Integer> argStartIndexes;
+ protected final ImmutableList<Object> arguments;
private static final Joiner LINE_JOINER = Joiner.on("\n").skipNulls();
private static final Joiner FIELD_JOINER = Joiner.on(": ").skipNulls();
@@ -678,19 +671,19 @@ public class StarlarkCustomCommandLine extends CommandLine {
}
StarlarkCustomCommandLine build(boolean flagPerLine) {
- return new StarlarkCustomCommandLine(
- ImmutableList.copyOf(arguments),
- flagPerLine ? ImmutableList.copyOf(argStartIndexes) : ImmutableList.of());
+ if (flagPerLine) {
+ return new StarlarkCustomCommandLineWithIndexes(
+ ImmutableList.copyOf(arguments), ImmutableList.copyOf(argStartIndexes));
+ } else {
+ return new StarlarkCustomCommandLine(ImmutableList.copyOf(arguments));
+ }
}
}
@AutoCodec.VisibleForSerialization
@AutoCodec.Instantiator
- StarlarkCustomCommandLine(
- ImmutableList<Object> arguments,
- ImmutableList<Integer> argStartIndexes) {
+ StarlarkCustomCommandLine(ImmutableList<Object> arguments) {
this.arguments = arguments;
- this.argStartIndexes = argStartIndexes;
}
@Override
@@ -698,6 +691,41 @@ public class StarlarkCustomCommandLine extends CommandLine {
return arguments(null);
}
+ @Override
+ public Iterable<String> arguments(@Nullable ArtifactExpander artifactExpander)
+ throws CommandLineExpansionException, InterruptedException {
+ List<String> result = new ArrayList<>();
+
+ for (int argi = 0; argi < arguments.size(); ) {
+ Object arg = arguments.get(argi++);
+ if (arg instanceof VectorArg) {
+ argi = ((VectorArg) arg).eval(arguments, argi, result, artifactExpander);
+ } else if (arg instanceof ScalarArg) {
+ argi = ((ScalarArg) arg).eval(arguments, argi, result);
+ } else {
+ result.add(CommandLineItem.expandToCommandLine(arg));
+ }
+ }
+ return ImmutableList.copyOf(result);
+ }
+
+ private static class StarlarkCustomCommandLineWithIndexes extends StarlarkCustomCommandLine {
+ /**
+ * If non-empty, an extra level of grouping on top of the 'arguments' list. Each element is the
+ * beginning of a group of args. For example, if this contains 0 and 3, then arguments 0, 1 and
+ * 2 constitute the first group, and arguments 3 to the end constitute the next. The expanded
+ * version of these arguments will be concatenated together to support flag_per_line format.
+ */
+ private final ImmutableList<Integer> argStartIndexes;
+
+ @AutoCodec.VisibleForSerialization
+ @AutoCodec.Instantiator
+ public StarlarkCustomCommandLineWithIndexes(
+ ImmutableList<Object> arguments, ImmutableList<Integer> argStartIndexes) {
+ super(arguments);
+ this.argStartIndexes = argStartIndexes;
+ }
+
@Override
public Iterable<String> arguments(@Nullable ArtifactExpander artifactExpander)
throws CommandLineExpansionException, InterruptedException {
@@ -751,6 +779,7 @@ public class StarlarkCustomCommandLine extends CommandLine {
}
return groupedBuilder.build();
}
+ }
@Override
public void addToFingerprint(
