Use int instead of AtomicInteger when caching transitiveHeaderCount: no concurrent accesses are expected, so thread safety is unnecessary. (Caching the number of transitive headers was introduced in https://github.com/bazelbuild/bazel/commit/3e7acaf3f886709d960f508cfe4d2883098fb160.)
@@ -55,7 +55,6 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.StarlarkList;
@@ -107,14 +106,12 @@ public final class CcCompilationContext implements CcCompilationContextApi<Artif
// This is needed only when code coverage collection is enabled, to report the actual source file
// name in the coverage output file.
private final NestedSet<Pair<String, String>> virtualToOriginalHeaders;
-
/**
* Caches the actual number of transitive headers reachable through transitiveHeaderInfos. We need
* to create maps to store these and so caching this count can substantially help with memory
* allocations.
*/
- private final AtomicInteger transitiveHeaderCount;
-
+ private int transitiveHeaderCount;
/** Aftifacts generated by the header validation actions. */
private final NestedSet<Artifact> headerTokens;
@@ -151,7 +148,7 @@ public final class CcCompilationContext implements CcCompilationContextApi<Artif
this.propagateModuleMapAsActionInput = propagateModuleMapAsActionInput;
this.headersCheckingMode = headersCheckingMode;
this.virtualToOriginalHeaders = virtualToOriginalHeaders;
- this.transitiveHeaderCount = new AtomicInteger(-1);
+ this.transitiveHeaderCount = -1;
this.headerTokens = headerTokens;
}
@@ -447,10 +444,9 @@ public final class CcCompilationContext implements CcCompilationContextApi<Artif
ArrayList<Artifact> treeArtifacts = new ArrayList<>();
// We'd prefer for these types to use ImmutableSet/ImmutableMap. However, constructing these is
// substantially more costly in a way that shows up in profiles.
- int headerCount = transitiveHeaderCount.get();
Map<PathFragment, Artifact> pathToLegalArtifact =
CompactHashMap.createWithExpectedSize(
- headerCount == -1 ? transitiveHeaderInfos.size() : headerCount);
+ transitiveHeaderCount == -1 ? transitiveHeaderInfos.size() : transitiveHeaderCount);
Set<Artifact> modularHeaders =
CompactHashSet.createWithExpectedSize(transitiveHeaderInfos.size());
// Not using range-based for loops here and below as the additional overhead of the
@@ -480,7 +476,9 @@ public final class CcCompilationContext implements CcCompilationContextApi<Artif
env, pathToLegalArtifact, treeArtifacts)) {
return null;
}
- transitiveHeaderCount.compareAndSet(-1, pathToLegalArtifact.size());
+ if (transitiveHeaderCount == -1) {
+ transitiveHeaderCount = pathToLegalArtifact.size();
+ }
removeArtifactsFromSet(modularHeaders, headerInfo.modularHeaders);
removeArtifactsFromSet(modularHeaders, headerInfo.textualHeaders);
removeArtifactsFromSet(modularHeaders, headerInfo.separateModuleHeaders);
