Skip to content

Commit

Permalink
Make HeaderInfo.modularHeaders lazy.
Browse files Browse the repository at this point in the history
This makes later serialization work slightly simpler.

PiperOrigin-RevId: 607735291
Change-Id: I17f980c8f947d1eeb43fa0099c2434ed5590d037
  • Loading branch information
aoeui authored and copybara-github committed Feb 16, 2024
1 parent dcda51e commit f5c8deb
Showing 1 changed file with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public IncludeScanningHeaderData.Builder createIncludeScanningHeaderData(
for (HeaderInfo transitiveHeaderInfo : transitiveHeaderInfos) {
boolean isModule = createModularHeaders && transitiveHeaderInfo.getModule(usePic) != null;
handleHeadersForIncludeScanning(
transitiveHeaderInfo.modularHeaders,
transitiveHeaderInfo.getModularHeaders(),
pathToLegalArtifact,
treeArtifacts,
isModule,
Expand All @@ -492,7 +492,7 @@ public IncludeScanningHeaderData.Builder createIncludeScanningHeaderData(
if (transitiveHeaderCount == -1) {
transitiveHeaderCount = pathToLegalArtifact.size();
}
removeArtifactsFromSet(modularHeaders, headerInfo.modularHeaders);
removeArtifactsFromSet(modularHeaders, headerInfo.getModularHeaders());
removeArtifactsFromSet(modularHeaders, headerInfo.textualHeaders);
removeArtifactsFromSet(modularHeaders, headerInfo.separateModuleHeaders);
return new IncludeScanningHeaderData.Builder(pathToLegalArtifact, modularHeaders);
Expand All @@ -514,8 +514,9 @@ public Set<DerivedArtifact> computeUsedModules(
}
// Not using range-based for loops here as often there is exactly one element in this list
// and the amount of garbage created by SingletonImmutableList.iterator() is significant.
for (int i = 0; i < transitiveHeaderInfo.modularHeaders.size(); i++) {
Artifact header = transitiveHeaderInfo.modularHeaders.get(i);
ImmutableList<Artifact> modularHeaders = transitiveHeaderInfo.getModularHeaders();
for (int i = 0; i < modularHeaders.size(); i++) {
Artifact header = modularHeaders.get(i);
if (includes.contains(header)) {
modules.add(module);
break;
Expand Down Expand Up @@ -603,7 +604,7 @@ ImmutableList<Artifact> getHeaderModuleSrcs(boolean separateModule) {
return headerInfo.separateModuleHeaders;
}
return new ImmutableSet.Builder<Artifact>()
.addAll(headerInfo.modularHeaders)
.addAll(headerInfo.getModularHeaders())
.addAll(headerInfo.textualHeaders)
.addAll(headerInfo.separateModuleHeaders)
.build()
Expand Down Expand Up @@ -918,7 +919,7 @@ public Builder addSystemIncludeDirs(Iterable<PathFragment> systemIncludeDirs) {
return this;
}

/** Add framewrok include directories to be added with "-F". */
/** Add framework include directories to be added with "-F". */
@CanIgnoreReturnValue
public Builder addFrameworkIncludeDirs(Iterable<PathFragment> frameworkIncludeDirs) {
this.frameworkIncludeDirs.addAll(frameworkIncludeDirs);
Expand Down Expand Up @@ -1181,9 +1182,6 @@ static final class HeaderInfo {
/** All private header files that are compiled into this module. */
final ImmutableList<Artifact> modularPrivateHeaders;

/** All header files that are compiled into this module. */
private final ImmutableList<Artifact> modularHeaders;

/** All textual header files that are contained in this module. */
final ImmutableList<Artifact> textualHeaders;

Expand All @@ -1196,6 +1194,13 @@ static final class HeaderInfo {
/** HeaderInfos of direct dependencies of C++ target represented by this context. */
final ImmutableList<HeaderInfo> deps;

/**
* All header files that are compiled into this module.
*
* <p>Lazily initialized. Use {@link #getModularHeaders} instead.
*/
private transient volatile ImmutableList<Artifact> lazyModularHeaders;

/** Collection representing the memoized form of transitive information, set by flatten(). */
private TransitiveHeaderCollection memo = null;

Expand All @@ -1213,8 +1218,6 @@ static final class HeaderInfo {
this.picHeaderModule = picHeaderModule;
this.modularPublicHeaders = modularPublicHeaders;
this.modularPrivateHeaders = modularPrivateHeaders;
this.modularHeaders =
ImmutableList.copyOf(Iterables.concat(modularPublicHeaders, modularPrivateHeaders));
this.textualHeaders = textualHeaders;
this.separateModuleHeaders = separateModuleHeaders;
this.separateModule = separateModule;
Expand Down Expand Up @@ -1271,6 +1274,18 @@ private void addOthers(Set<HeaderInfo> result, List<HeaderInfo> additionalDeps)
}
}

private ImmutableList<Artifact> getModularHeaders() {
if (lazyModularHeaders == null) {
synchronized (this) {
if (lazyModularHeaders == null) {
lazyModularHeaders =
ImmutableList.copyOf(Iterables.concat(modularPublicHeaders, modularPrivateHeaders));
}
}
}
return lazyModularHeaders;
}

/** Represents the memoized transitive information for a HeaderInfo instance. */
private class TransitiveHeaderCollection extends AbstractCollection<HeaderInfo> {
private final int size;
Expand Down

0 comments on commit f5c8deb

Please sign in to comment.