Don't wrap values in Pair in CcToolchainVariables.
Saves about a third of the memory taken up by that cache.
RELNOTES: None.
@@ -34,7 +34,6 @@ import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization;
import com.google.devtools.build.lib.skylarkbuildapi.cpp.CcToolchainVariablesApi;
import com.google.devtools.build.lib.syntax.EvalException;
-import com.google.devtools.build.lib.util.Pair;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@@ -299,7 +298,10 @@ public abstract class CcToolchainVariables implements CcToolchainVariablesApi {
/** An empty variables instance. */
public static final CcToolchainVariables EMPTY = builder().build();
- private Map<String, Pair<VariableValue, String>> cache;
+ private static final Object NULL_MARKER = new Object();
+
+ // Values in this cache are either VariableValue, String error message, or NULL_MARKER.
+ private Map<String, Object> cache;
/**
* Retrieves a {@link StringSequence} variable named {@code variableName} from {@code variables}
@@ -345,21 +347,20 @@ public abstract class CcToolchainVariables implements CcToolchainVariablesApi {
if (cache == null) {
cache = Maps.newConcurrentMap();
}
- Pair<VariableValue, String> variableOrError =
+ Object variableOrError =
cache.computeIfAbsent(
name,
n -> {
- VariableValue nonStructuredVariable = getNonStructuredVariable(n);
- if (nonStructuredVariable != null) {
- return Pair.of(nonStructuredVariable, null);
+ VariableValue variable = getNonStructuredVariable(n);
+ if (variable != null) {
+ return variable;
}
try {
- VariableValue structuredVariable =
- getStructureVariable(n, throwOnMissingVariable, expander);
- return Pair.of(structuredVariable, null);
+ variable = getStructureVariable(n, throwOnMissingVariable, expander);
+ return variable != null ? variable : NULL_MARKER;
} catch (ExpansionException e) {
if (throwOnMissingVariable) {
- return Pair.of(null, e.getMessage());
+ return e.getMessage();
} else {
throw new IllegalStateException(
"Should not happen - call to getStructuredVariable threw when asked not to.",
@@ -367,14 +368,18 @@ public abstract class CcToolchainVariables implements CcToolchainVariablesApi {
}
}
});
- if (variableOrError.first == null && throwOnMissingVariable) {
+
+ if (variableOrError instanceof VariableValue) {
+ return (VariableValue) variableOrError;
+ }
+ if (throwOnMissingVariable) {
throw new ExpansionException(
- variableOrError.second != null
- ? variableOrError.second
+ variableOrError instanceof String
+ ? (String) variableOrError
: String.format(
"Invalid toolchain configuration: Cannot find variable named '%s'.", name));
}
- return variableOrError.first;
+ return null;
}
private VariableValue getStructureVariable(
