Skip to content

Commit 5bbd8c7

Browse files
justinhorvitzcopybara-github
authored andcommitted
Use SkyKeyInterner for ArtifactNestedSetKey and intern all instances.
This was likely missed during the `SkyKeyInterner` rollout since it uses `MapMaker` instead of an `Interner`. The original intention for that was to avoid the garbage of creating a key when one is already present. The additional garbage is more than offset by the memory savings in this change. PiperOrigin-RevId: 531246598 Change-Id: I2c1710c6931f6e59da02dafd2bc1698eb8472675
1 parent ebfaa4c commit 5bbd8c7

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

src/main/java/com/google/devtools/build/lib/skyframe/ArtifactNestedSetFunction.java

+4-18
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
import static com.google.common.base.Preconditions.checkNotNull;
1717

1818
import com.google.common.base.Suppliers;
19-
import com.google.common.collect.MapMaker;
19+
import com.google.common.collect.ImmutableList;
2020
import com.google.devtools.build.lib.actions.ActionExecutionException;
2121
import com.google.devtools.build.lib.actions.Artifact;
2222
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
2323
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
24-
import com.google.devtools.build.lib.concurrent.BlazeInterners;
2524
import com.google.devtools.build.lib.skyframe.ArtifactFunction.SourceArtifactException;
2625
import com.google.devtools.build.lib.util.Pair;
2726
import com.google.devtools.build.skyframe.SkyFunction;
@@ -82,17 +81,6 @@ final class ArtifactNestedSetFunction implements SkyFunction {
8281
*/
8382
private ConcurrentMap<SkyKey, SkyValue> artifactSkyKeyToSkyValue = new ConcurrentHashMap<>();
8483

85-
/**
86-
* Maps the NestedSets' underlying objects to the corresponding SkyKey. This is to avoid
87-
* re-creating SkyKey for the same nested set upon reevaluation because of e.g. a missing value.
88-
*
89-
* <p>The map weakly references its values: when the ArtifactNestedSetKey becomes otherwise
90-
* unreachable, the entry is collected.
91-
*/
92-
// Note: Not using a caffeine cache here because it used more memory (b/193294367).
93-
private final ConcurrentMap<NestedSet.Node, ArtifactNestedSetKey> nestedSetToSkyKey =
94-
new MapMaker().concurrencyLevel(BlazeInterners.concurrencyLevel()).weakValues().makeMap();
95-
9684
private final Supplier<ArtifactNestedSetValue> valueSupplier;
9785

9886
private static ArtifactNestedSetFunction singleton = null;
@@ -162,17 +150,15 @@ public SkyValue compute(SkyKey skyKey, Environment env)
162150
}
163151

164152
private List<SkyKey> getDepSkyKeys(ArtifactNestedSetKey skyKey) {
165-
List<Artifact> leaves = skyKey.getSet().getLeaves();
166-
List<NestedSet<Artifact>> nonLeaves = skyKey.getSet().getNonLeaves();
153+
ImmutableList<Artifact> leaves = skyKey.getSet().getLeaves();
154+
ImmutableList<NestedSet<Artifact>> nonLeaves = skyKey.getSet().getNonLeaves();
167155

168156
List<SkyKey> keys = new ArrayList<>(leaves.size() + nonLeaves.size());
169157
for (Artifact file : leaves) {
170158
keys.add(Artifact.key(file));
171159
}
172160
for (NestedSet<Artifact> nonLeaf : nonLeaves) {
173-
keys.add(
174-
nestedSetToSkyKey.computeIfAbsent(
175-
nonLeaf.toNode(), node -> new ArtifactNestedSetKey(nonLeaf, node)));
161+
keys.add(ArtifactNestedSetKey.create(nonLeaf));
176162
}
177163
return keys;
178164
}

src/main/java/com/google/devtools/build/lib/skyframe/ArtifactNestedSetKey.java

+15-7
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,31 @@
1818
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
1919
import com.google.devtools.build.skyframe.ExecutionPhaseSkyKey;
2020
import com.google.devtools.build.skyframe.SkyFunctionName;
21+
import com.google.devtools.build.skyframe.SkyKey;
22+
import com.google.devtools.build.skyframe.SkyKey.SkyKeyInterner;
2123

2224
/** SkyKey for {@code NestedSet<Artifact>}. */
2325
public final class ArtifactNestedSetKey implements ExecutionPhaseSkyKey {
2426

25-
// TODO(jhorvitz): Consider sharing the nestedSetToSkyKey map in ArtifactNestedSetFunction.
27+
private static final SkyKeyInterner<ArtifactNestedSetKey> interner = SkyKey.newInterner();
28+
2629
public static ArtifactNestedSetKey create(NestedSet<Artifact> set) {
27-
return new ArtifactNestedSetKey(set, set.toNode());
30+
return interner.intern(new ArtifactNestedSetKey(set, set.toNode()));
2831
}
2932

3033
private final NestedSet<Artifact> set;
3134
private final NestedSet.Node node;
3235

36+
private ArtifactNestedSetKey(NestedSet<Artifact> set, NestedSet.Node node) {
37+
this.set = set;
38+
this.node = node;
39+
}
40+
3341
@Override
3442
public SkyFunctionName functionName() {
3543
return SkyFunctions.ARTIFACT_NESTED_SET;
3644
}
3745

38-
ArtifactNestedSetKey(NestedSet<Artifact> set, NestedSet.Node node) {
39-
this.set = set;
40-
this.node = node;
41-
}
42-
4346
/** Returns the set of artifacts that this key represents. */
4447
public NestedSet<Artifact> getSet() {
4548
return set;
@@ -52,6 +55,11 @@ public boolean valueIsShareable() {
5255
return false;
5356
}
5457

58+
@Override
59+
public SkyKeyInterner<?> getSkyKeyInterner() {
60+
return interner;
61+
}
62+
5563
@Override
5664
public int hashCode() {
5765
return node.hashCode();

0 commit comments

Comments
 (0)