Skip to content

Commit

Permalink
Precompute StarlarkDefinedConfigTransition hashcodes.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 676921410
Change-Id: Ie12eb5ecc28c4eeaa15b684847a7fe3ec8c75e3b
  • Loading branch information
justinhorvitz authored and copybara-github committed Sep 20, 2024
1 parent 6661820 commit f10f307
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/analysis/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2087,6 +2087,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/packages",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/config:configuration_transition_api",
"//src/main/java/com/google/devtools/build/lib/util",
"//src/main/java/com/google/devtools/build/lib/util:hash_codes",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/com/google/devtools/common/options",
"//src/main/java/net/starlark/java/eval",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.google.devtools.build.lib.packages.RuleTransitionData;
import com.google.devtools.build.lib.packages.StructImpl;
import com.google.devtools.build.lib.starlarkbuildapi.config.ConfigurationTransitionApi;
import com.google.devtools.build.lib.util.HashCodes;
import com.google.devtools.build.lib.util.RegexFilter;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.common.options.Converter;
Expand Down Expand Up @@ -73,7 +74,7 @@
*
* <p>Represents a configuration transition across a dependency edge defined in Starlark.
*/
public abstract class StarlarkDefinedConfigTransition implements ConfigurationTransitionApi {
public abstract sealed class StarlarkDefinedConfigTransition implements ConfigurationTransitionApi {

public static final String COMMAND_LINE_OPTION_PREFIX = "//command_line_option:";

Expand All @@ -92,7 +93,7 @@ public enum Settings {

private final ImmutableMap<String, String> inputsCanonicalizedToGiven;
private final ImmutableMap<String, String> outputsCanonicalizedToGiven;
protected final Label parentLabel;
final Label parentLabel;
private final Location location;
private final Label.PackageContext packageContext;

Expand Down Expand Up @@ -297,8 +298,9 @@ public static StarlarkDefinedConfigTransition newAnalysisTestTransition(
return new AnalysisTestTransition(changedSettings, repoMapping, parentLabel, location);
}

private static class AnalysisTestTransition extends StarlarkDefinedConfigTransition {
private static final class AnalysisTestTransition extends StarlarkDefinedConfigTransition {
private final Map<String, Object> changedSettings;
private final int hashCode;

AnalysisTestTransition(
Map<String, Object> changedSettings,
Expand All @@ -307,12 +309,13 @@ private static class AnalysisTestTransition extends StarlarkDefinedConfigTransit
Location location)
throws EvalException {
super(
/*inputs=*/ ImmutableList.of(),
/* inputs= */ ImmutableList.of(),
ImmutableList.copyOf(changedSettings.keySet()),
repoMapping,
parentLabel,
location);
this.changedSettings = changedSettings;
this.hashCode = HashCodes.hashObjects(getInputs(), getOutputs(), changedSettings);
}

@Override
Expand Down Expand Up @@ -354,15 +357,16 @@ public boolean equals(Object object) {

@Override
public int hashCode() {
return Objects.hash(this.getInputs(), this.getOutputs(), this.changedSettings);
return hashCode;
}
}

/** A transition with a user-defined implementation function. */
public static class RegularTransition extends StarlarkDefinedConfigTransition {
public static sealed class RegularTransition extends StarlarkDefinedConfigTransition {
private final StarlarkCallable impl;
private final StarlarkSemantics semantics;
private final RepositoryMapping repoMapping;
private final int hashCode;

RegularTransition(
StarlarkCallable impl,
Expand All @@ -377,6 +381,7 @@ public static class RegularTransition extends StarlarkDefinedConfigTransition {
this.impl = impl;
this.semantics = semantics;
this.repoMapping = repoMapping;
this.hashCode = HashCodes.hashObjects(getInputs(), getOutputs(), impl);
}

@Override
Expand Down Expand Up @@ -755,12 +760,12 @@ public boolean equals(Object object) {

@Override
public int hashCode() {
return Objects.hash(this.getInputs(), this.getOutputs(), this.impl);
return hashCode;
}
}

/** A transition implementation used only for Starlark-defined exec transitions. */
private static class ExecTransition extends RegularTransition {
private static final class ExecTransition extends RegularTransition {
private ExecTransition(
StarlarkCallable impl,
List<String> inputs,
Expand Down

0 comments on commit f10f307

Please sign in to comment.