Skip to content

Commit

Permalink
Cache updated BuildConfigurationKey instances to reduce repeated work.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 642707715
Change-Id: I79f7c7706d529a4120ac17d700c9c9dc8b1854a3
  • Loading branch information
katre authored and Copybara-Service committed Jun 12, 2024
1 parent 116d4f1 commit 3695909
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ java_library(
"//src/main/java/net/starlark/java/syntax",
"//src/main/protobuf:failure_details_java_proto",
"//third_party:auto_value",
"//third_party:caffeine",
"//third_party:error_prone_annotations",
"//third_party:guava",
"//third_party:jsr305",
Expand All @@ -117,6 +118,7 @@ java_library(
"//src/main/java/com/google/devtools/build/skyframe",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//src/main/java/com/google/devtools/common/options",
"//third_party:caffeine",
"//third_party:guava",
"//third_party:jsr305",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.
package com.google.devtools.build.lib.analysis.producers;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.devtools.build.lib.analysis.PlatformOptions;
import com.google.devtools.build.lib.analysis.config.BuildOptions;
import com.google.devtools.build.lib.cmdline.Label;
Expand Down Expand Up @@ -44,6 +46,10 @@ public class BuildConfigurationKeyProducer<C>
ValueOrExceptionSink<PlatformMappingException>,
PlatformFlagsProducer.ResultSink {

// Static cache for transformed options.
private static final Cache<BuildOptions, BuildConfigurationKey> CACHE =
Caffeine.newBuilder().weakKeys().build();

/** Interface for clients to accept results of this computation. */
public interface ResultSink<C> {

Expand Down Expand Up @@ -76,11 +82,15 @@ public BuildConfigurationKeyProducer(

@Override
public StateMachine step(Tasks tasks) throws InterruptedException {
BuildConfigurationKey result = CACHE.getIfPresent(this.options);
if (result != null) {
this.sink.acceptTransitionedConfiguration(this.context, result);
return this.runAfter;
}

// Short-circuit if there are no platform options.
if (!this.options.contains(PlatformOptions.class)) {
this.sink.acceptTransitionedConfiguration(
this.context, BuildConfigurationKey.create(this.options));
return this.runAfter;
return finishConfigurationKeyProcessing(BuildConfigurationKey.create(this.options));
}

// Find platform mappings and platform-based flags for merging.
Expand Down Expand Up @@ -167,12 +177,17 @@ private StateMachine applyFlags(Tasks tasks) {

try {
BuildConfigurationKey newConfigurationKey = applyFlagsForOptions(options);
sink.acceptTransitionedConfiguration(this.context, newConfigurationKey);
return finishConfigurationKeyProcessing(newConfigurationKey);
} catch (OptionsParsingException e) {
sink.acceptTransitionError(e);
return runAfter;
}
return runAfter;
}

private StateMachine finishConfigurationKeyProcessing(BuildConfigurationKey newConfigurationKey) {
CACHE.put(this.options, newConfigurationKey);
sink.acceptTransitionedConfiguration(this.context, newConfigurationKey);
return this.runAfter;
}

/**
Expand Down

0 comments on commit 3695909

Please sign in to comment.