Skip params file handling for resource jar actions with short command lines
@@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.ParameterFile.ParameterFileType;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
@@ -119,6 +120,17 @@ public class ResourceJarActionBuilder {
if (!classpathResources.isEmpty()) {
command.addExecPaths("--classpath_resources", classpathResources);
}
+ // TODO(b/37444705): remove this logic and always call useParameterFile once the bug is fixed
+ // Most resource jar actions are very small and expanding the argument list for
+ // ParamFileHelper#getParamsFileMaybe is expensive, so avoid doing that work if
+ // we definitely don't need a params file.
+ // This heuristic could be much more aggressive, but we don't ever want to skip
+ // the params file in situations where it is required for --min_param_file_size.
+ if (sizeGreaterThanOrEqual(
+ Iterables.concat(messages, resources.values(), resourceJars, classpathResources), 10)
+ || ruleContext.getConfiguration().getMinParamFileSize() < 10000) {
+ builder.useParameterFile(ParameterFileType.SHELL_QUOTED);
+ }
ruleContext.registerAction(
builder
.addOutput(outputJar)
@@ -126,13 +138,16 @@ public class ResourceJarActionBuilder {
.addInputs(resources.values())
.addTransitiveInputs(resourceJars)
.addInputs(classpathResources)
- .useParameterFile(ParameterFileType.SHELL_QUOTED)
.setCommandLine(command.build())
.setProgressMessage("Building Java resource jar")
.setMnemonic("JavaResourceJar")
.build(ruleContext));
}
+ boolean sizeGreaterThanOrEqual(Iterable<?> elements, int size) {
+ return Iterables.size(Iterables.limit(elements, size)) == size;
+ }
+
private static void addAsResourcePrefixedExecPath(
PathFragment resourcePath, Artifact artifact, CustomCommandLine.Builder builder) {
PathFragment execPath = artifact.getExecPath();
