31
31
import com .google .devtools .build .lib .analysis .config .FragmentOptions ;
32
32
import com .google .devtools .build .lib .cmdline .Label ;
33
33
import com .google .devtools .build .lib .skyframe .serialization .autocodec .AutoCodec ;
34
+ import com .google .devtools .build .lib .util .HashCodes ;
34
35
import com .google .devtools .build .skyframe .SkyValue ;
35
36
import com .google .devtools .common .options .OptionsParsingException ;
36
37
import java .util .List ;
37
38
import java .util .Map ;
38
- import java .util .Objects ;
39
39
import java .util .concurrent .CompletionException ;
40
40
41
41
/**
@@ -52,7 +52,7 @@ public final class PlatformMappingValue implements SkyValue {
52
52
private final ImmutableMap <Label , ParsedFlagsValue > platformsToFlags ;
53
53
private final ImmutableMap <ParsedFlagsValue , Label > flagsToPlatforms ;
54
54
private final ImmutableSet <Class <? extends FragmentOptions >> optionsClasses ;
55
- private final LoadingCache <BuildOptions , BuildOptions > mappingCache ;
55
+ private final LoadingCache <BuildOptions , BuildConfigurationKey > mappingCache ;
56
56
57
57
/**
58
58
* Creates a new mapping value which will match on the given platforms (if a target platform is
@@ -75,7 +75,12 @@ public final class PlatformMappingValue implements SkyValue {
75
75
}
76
76
77
77
/**
78
- * Maps one {@link BuildOptions} to another by way of mappings provided in a file.
78
+ * Maps one {@link BuildOptions} to another's {@link BuildConfigurationKey} by way of mappings
79
+ * provided in a file.
80
+ *
81
+ * <p>Returns a {@link BuildConfigurationKey} instead of just {@link BuildOptions} so that caching
82
+ * of mappings also saves the CPU cost of interning {@link BuildConfigurationKey} (which is what
83
+ * callers typically need).
79
84
*
80
85
* <p>The <a href=https://docs.google.com/document/d/1Vg_tPgiZbSrvXcJ403vZVAGlsWhH9BUDrAxMOYnO0Ls>
81
86
* full design</a> contains the details for the mapping logic but in short:
@@ -89,12 +94,12 @@ public final class PlatformMappingValue implements SkyValue {
89
94
* </ol>
90
95
*
91
96
* @param original the key representing the configuration to be mapped
92
- * @return the mapped key if any mapping matched the original or else the original
97
+ * @return a {@link BuildConfigurationKey} to request the mapped configuration
93
98
* @throws OptionsParsingException if any of the user configured flags cannot be parsed
94
99
* @throws IllegalArgumentException if the original does not contain a {@link PlatformOptions}
95
100
* fragment
96
101
*/
97
- public BuildOptions map (BuildOptions original ) throws OptionsParsingException {
102
+ public BuildConfigurationKey map (BuildOptions original ) throws OptionsParsingException {
98
103
try {
99
104
return mappingCache .get (original );
100
105
} catch (CompletionException e ) {
@@ -104,19 +109,18 @@ public BuildOptions map(BuildOptions original) throws OptionsParsingException {
104
109
}
105
110
}
106
111
107
- private BuildOptions computeMapping (BuildOptions originalOptions ) throws OptionsParsingException {
112
+ private BuildConfigurationKey computeMapping (BuildOptions originalOptions )
113
+ throws OptionsParsingException {
108
114
if (originalOptions .hasNoConfig ()) {
109
115
// The empty configuration (produced by NoConfigTransition) is terminal: it'll never change.
110
- return originalOptions ;
116
+ return BuildConfigurationKey . create ( originalOptions ) ;
111
117
}
112
118
113
119
var platformOptions = originalOptions .get (PlatformOptions .class );
114
120
checkArgument (
115
121
platformOptions != null ,
116
122
"When using platform mappings, all configurations must contain platform options" );
117
123
118
- BuildOptions modifiedOptions = null ;
119
-
120
124
if (!platformOptions .platforms .isEmpty ()) {
121
125
List <Label > platforms = platformOptions .platforms ;
122
126
@@ -125,32 +129,28 @@ private BuildOptions computeMapping(BuildOptions originalOptions) throws Options
125
129
if (!platformsToFlags .containsKey (targetPlatform )) {
126
130
// This can happen if the user has set the platform and any other flags that would normally
127
131
// be mapped from it on the command line instead of relying on the mapping.
128
- return originalOptions ;
132
+ return BuildConfigurationKey . create ( originalOptions ) ;
129
133
}
130
134
131
135
ParsedFlagsValue parsedFlags = platformsToFlags .get (targetPlatform );
132
- modifiedOptions = parsedFlags .mergeWith (originalOptions );
133
- } else {
134
- boolean mappingFound = false ;
135
- for (Map .Entry <ParsedFlagsValue , Label > flagsToPlatform : flagsToPlatforms .entrySet ()) {
136
- ParsedFlagsValue parsedFlags = flagsToPlatform .getKey ();
137
- Label platformLabel = flagsToPlatform .getValue ();
138
- if (originalOptions .matches (parsedFlags .parsingResult ())) {
139
- modifiedOptions = originalOptions .clone ();
140
- modifiedOptions .get (PlatformOptions .class ).platforms = ImmutableList .of (platformLabel );
141
- mappingFound = true ;
142
- break ;
143
- }
144
- }
136
+ return BuildConfigurationKey .create (parsedFlags .mergeWith (originalOptions ));
137
+ }
145
138
146
- if (!mappingFound ) {
147
- Label targetPlatform = platformOptions .computeTargetPlatform ();
148
- modifiedOptions = originalOptions .clone ();
149
- modifiedOptions .get (PlatformOptions .class ).platforms = ImmutableList .of (targetPlatform );
139
+ for (Map .Entry <ParsedFlagsValue , Label > flagsToPlatform : flagsToPlatforms .entrySet ()) {
140
+ ParsedFlagsValue parsedFlags = flagsToPlatform .getKey ();
141
+ Label platformLabel = flagsToPlatform .getValue ();
142
+ if (originalOptions .matches (parsedFlags .parsingResult ())) {
143
+ BuildOptions modifiedOptions = originalOptions .clone ();
144
+ modifiedOptions .get (PlatformOptions .class ).platforms = ImmutableList .of (platformLabel );
145
+ return BuildConfigurationKey .create (modifiedOptions );
150
146
}
151
147
}
152
148
153
- return modifiedOptions ;
149
+ // No mapping found.
150
+ Label targetPlatform = platformOptions .computeTargetPlatform ();
151
+ BuildOptions modifiedOptions = originalOptions .clone ();
152
+ modifiedOptions .get (PlatformOptions .class ).platforms = ImmutableList .of (targetPlatform );
153
+ return BuildConfigurationKey .create (modifiedOptions );
154
154
}
155
155
156
156
@ Override
@@ -168,7 +168,7 @@ public boolean equals(Object obj) {
168
168
169
169
@ Override
170
170
public int hashCode () {
171
- return Objects . hash (flagsToPlatforms , platformsToFlags , optionsClasses );
171
+ return HashCodes . hashObjects (flagsToPlatforms , platformsToFlags , optionsClasses );
172
172
}
173
173
174
174
@ Override
0 commit comments