Skip to content

Commit e218924

Browse files
yuyue730copybara-github
authored andcommitted
Reduce interning memory overhead Part III -- Applies SkyKeyInterner to all SkyKeys that are using weak interner
836c608 implements how `InMemoryGraph` interacts with `SkyKeyInterner`, and applies this feature only to `FileValue#Key`. In this commit, we want to apply `SkyKeyInterner` to all `SkyKey`s using weak interner. Currently there are ~30 types of `SkyKey`s that use bazel weak interner. PiperOrigin-RevId: 511521005 Change-Id: Ia6fb0fc2d494ac1b470268f7e263f7564d64a56b
1 parent 3c11366 commit e218924

22 files changed

+163
-73
lines changed

src/main/java/com/google/devtools/build/lib/concurrent/BlazeInterners.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ private static InternerBuilder setConcurrencyLevel(InternerBuilder builder) {
3535
return builder.concurrencyLevel(CONCURRENCY_LEVEL);
3636
}
3737

38+
/**
39+
* Creates an interner which retains a weak reference to each instance it has interned.
40+
*
41+
* <p>It is preferred to use {@code SkyKey#SkyKeyInterner} instead for interning {@code SkyKey}
42+
* types.
43+
*/
3844
public static <T> Interner<T> newWeakInterner() {
3945
return setConcurrencyLevel(Interners.newBuilder().weak()).build();
4046
}

src/main/java/com/google/devtools/build/lib/packages/WorkspaceFileValue.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
import com.google.common.base.Preconditions;
1818
import com.google.common.collect.ImmutableMap;
19-
import com.google.common.collect.Interner;
2019
import com.google.devtools.build.lib.cmdline.RepositoryName;
21-
import com.google.devtools.build.lib.concurrent.BlazeInterners;
2220
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
2321
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
2422
import com.google.devtools.build.lib.vfs.RootedPath;
@@ -43,7 +41,7 @@ public class WorkspaceFileValue implements SkyValue {
4341
@Immutable
4442
@AutoCodec
4543
public static class WorkspaceFileKey implements SkyKey {
46-
private static final Interner<WorkspaceFileKey> interner = BlazeInterners.newWeakInterner();
44+
private static final SkyKeyInterner<WorkspaceFileKey> interner = SkyKey.newInterner();
4745

4846
private final RootedPath path;
4947
private final int idx;
@@ -93,6 +91,11 @@ public int hashCode() {
9391
public String toString() {
9492
return path + ", " + idx;
9593
}
94+
95+
@Override
96+
public SkyKeyInterner<WorkspaceFileKey> getSkyKeyInterner() {
97+
return interner;
98+
}
9699
}
97100

98101
private final Package pkg;

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616
import com.google.common.base.Objects;
1717
import com.google.common.collect.ImmutableList;
1818
import com.google.common.collect.ImmutableMap;
19-
import com.google.common.collect.Interner;
2019
import com.google.devtools.build.lib.actions.ActionLookupKey;
2120
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
2221
import com.google.devtools.build.lib.cmdline.Label;
23-
import com.google.devtools.build.lib.concurrent.BlazeInterners;
2422
import com.google.devtools.build.lib.packages.AspectClass;
2523
import com.google.devtools.build.lib.packages.AspectDescriptor;
2624
import com.google.devtools.build.lib.packages.AspectParameters;
2725
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
2826
import com.google.devtools.build.skyframe.SkyFunctionName;
27+
import com.google.devtools.build.skyframe.SkyKey;
2928
import java.util.Comparator;
3029
import javax.annotation.Nullable;
3130

@@ -85,7 +84,7 @@ public final int hashCode() {
8584
/** Represents an aspect applied to a particular target. */
8685
@AutoCodec
8786
public static final class AspectKey extends AspectBaseKey {
88-
private static final Interner<AspectKey> interner = BlazeInterners.newWeakInterner();
87+
private static final SkyKeyInterner<AspectKey> interner = SkyKey.newInterner();
8988

9089
private final ImmutableList<AspectKey> baseKeys;
9190
private final AspectDescriptor aspectDescriptor;
@@ -234,6 +233,11 @@ AspectKey withLabel(Label label) {
234233
newBaseKeys.build(),
235234
aspectDescriptor);
236235
}
236+
237+
@Override
238+
public SkyKeyInterner<AspectKey> getSkyKeyInterner() {
239+
return interner;
240+
}
237241
}
238242

239243
/**
@@ -242,7 +246,7 @@ AspectKey withLabel(Label label) {
242246
*/
243247
@AutoCodec
244248
public static final class TopLevelAspectsKey extends AspectBaseKey {
245-
private static final Interner<TopLevelAspectsKey> interner = BlazeInterners.newWeakInterner();
249+
private static final SkyKeyInterner<TopLevelAspectsKey> interner = SkyKey.newInterner();
246250

247251
private final ImmutableList<AspectClass> topLevelAspectsClasses;
248252
private final Label targetLabel;
@@ -324,5 +328,10 @@ public boolean equals(Object o) {
324328
&& Objects.equal(topLevelAspectsClasses, that.topLevelAspectsClasses)
325329
&& Objects.equal(topLevelAspectsParameters, that.topLevelAspectsParameters);
326330
}
331+
332+
@Override
333+
public SkyKeyInterner<TopLevelAspectsKey> getSkyKeyInterner() {
334+
return interner;
335+
}
327336
}
328337
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
package com.google.devtools.build.lib.skyframe;
1616

1717
import com.google.common.base.Preconditions;
18-
import com.google.common.collect.Interner;
1918
import com.google.devtools.build.lib.analysis.config.BuildOptions;
20-
import com.google.devtools.build.lib.concurrent.BlazeInterners;
2119
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
2220
import com.google.devtools.build.skyframe.SkyFunctionName;
2321
import com.google.devtools.build.skyframe.SkyKey;
@@ -57,7 +55,7 @@ public static BuildConfigurationKey withoutPlatformMapping(BuildOptions options)
5755
return interner.intern(new BuildConfigurationKey(options));
5856
}
5957

60-
private static final Interner<BuildConfigurationKey> interner = BlazeInterners.newWeakInterner();
58+
private static final SkyKeyInterner<BuildConfigurationKey> interner = SkyKey.newInterner();
6159

6260
private final BuildOptions options;
6361

@@ -96,4 +94,9 @@ public String toString() {
9694
// This format is depended on by integration tests.
9795
return "BuildConfigurationKey[" + options.checksum() + "]";
9896
}
97+
98+
@Override
99+
public SkyKeyInterner<BuildConfigurationKey> getSkyKeyInterner() {
100+
return interner;
101+
}
99102
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414
package com.google.devtools.build.lib.skyframe;
1515

1616
import com.google.common.base.Preconditions;
17-
import com.google.common.collect.Interner;
1817
import com.google.devtools.build.lib.actions.ActionLookupKey;
1918
import com.google.devtools.build.lib.actions.Actions.GeneratingActions;
2019
import com.google.devtools.build.lib.actions.BasicActionLookupValue;
2120
import com.google.devtools.build.lib.analysis.buildinfo.BuildInfoCollection;
2221
import com.google.devtools.build.lib.analysis.buildinfo.BuildInfoKey;
2322
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
2423
import com.google.devtools.build.lib.cmdline.Label;
25-
import com.google.devtools.build.lib.concurrent.BlazeInterners;
2624
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
2725
import com.google.devtools.build.skyframe.SkyFunctionName;
26+
import com.google.devtools.build.skyframe.SkyKey;
2827
import java.util.Objects;
2928
import javax.annotation.Nullable;
3029

@@ -58,8 +57,7 @@ public static BuildInfoKeyAndConfig key(
5857
/** Key for BuildInfoCollectionValues. */
5958
@AutoCodec
6059
public static final class BuildInfoKeyAndConfig extends ActionLookupKey {
61-
private static final Interner<BuildInfoKeyAndConfig> keyInterner =
62-
BlazeInterners.newWeakInterner();
60+
private static final SkyKeyInterner<BuildInfoKeyAndConfig> keyInterner = SkyKey.newInterner();
6361

6462
private final BuildInfoKey infoKey;
6563
private final BuildConfigurationKey configKey;
@@ -114,5 +112,10 @@ public boolean equals(Object other) {
114112
return Objects.equals(this.infoKey, that.infoKey)
115113
&& Objects.equals(this.configKey, that.configKey);
116114
}
115+
116+
@Override
117+
public SkyKeyInterner<BuildInfoKeyAndConfig> getSkyKeyInterner() {
118+
return keyInterner;
119+
}
117120
}
118121
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
import com.google.common.base.Preconditions;
2020
import com.google.common.collect.ImmutableList;
2121
import com.google.common.collect.ImmutableMap;
22-
import com.google.common.collect.Interner;
2322
import com.google.devtools.build.lib.analysis.AspectCollection;
2423
import com.google.devtools.build.lib.analysis.AspectCollection.AspectCycleOnPathException;
25-
import com.google.devtools.build.lib.concurrent.BlazeInterners;
2624
import com.google.devtools.build.lib.events.Event;
2725
import com.google.devtools.build.lib.packages.Aspect;
2826
import com.google.devtools.build.lib.packages.AspectClass;
@@ -244,8 +242,8 @@ public ImmutableList<AspectDetails> getUsedAspects() {
244242
/** {@link SkyKey} for building top-level aspects details. */
245243
@AutoCodec
246244
static final class BuildTopLevelAspectsDetailsKey implements SkyKey {
247-
private static final Interner<BuildTopLevelAspectsDetailsKey> interner =
248-
BlazeInterners.newWeakInterner();
245+
private static final SkyKeyInterner<BuildTopLevelAspectsDetailsKey> interner =
246+
SkyKey.newInterner();
249247

250248
private final ImmutableList<AspectClass> topLevelAspectsClasses;
251249
private final ImmutableMap<String, String> topLevelAspectsParameters;
@@ -311,6 +309,11 @@ public String toString() {
311309
.add("topLevelAspectsParameters", topLevelAspectsParameters)
312310
.toString();
313311
}
312+
313+
@Override
314+
public SkyKeyInterner<BuildTopLevelAspectsDetailsKey> getSkyKeyInterner() {
315+
return interner;
316+
}
314317
}
315318

316319
/**

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
package com.google.devtools.build.lib.skyframe;
1616

1717
import com.google.common.base.Preconditions;
18-
import com.google.common.collect.Interner;
1918
import com.google.devtools.build.lib.cmdline.Label;
20-
import com.google.devtools.build.lib.concurrent.BlazeInterners;
2119
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
2220
import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant;
2321
import com.google.devtools.build.lib.vfs.Root;
@@ -132,8 +130,6 @@ public static BzlCompileValue withProgram(Program prog, byte[] digest) {
132130
return new Success(prog, digest);
133131
}
134132

135-
private static final Interner<Key> keyInterner = BlazeInterners.newWeakInterner();
136-
137133
/** Types of bzl files we may encounter. */
138134
enum Kind {
139135
/** A regular .bzl file loaded on behalf of a BUILD or WORKSPACE file. */
@@ -159,6 +155,8 @@ enum Kind {
159155
/** SkyKey for retrieving a compiled .bzl program. */
160156
@AutoCodec
161157
public static class Key implements SkyKey {
158+
private static final SkyKeyInterner<Key> interner = SkyKey.newInterner();
159+
162160
/** The root in which the .bzl file is to be found. Null for EMPTY_PRELUDE. */
163161
@Nullable final Root root;
164162

@@ -180,7 +178,7 @@ private Key(Root root, Label label, Kind kind) {
180178
@AutoCodec.VisibleForSerialization
181179
@AutoCodec.Instantiator
182180
static Key create(Root root, Label label, Kind kind) {
183-
return keyInterner.intern(new Key(root, label, kind));
181+
return interner.intern(new Key(root, label, kind));
184182
}
185183

186184
/** Returns whether this key is for a {@code @_builtins} .bzl file. */
@@ -225,6 +223,11 @@ public SkyFunctionName functionName() {
225223
public String toString() {
226224
return String.format("%s:[%s]%s", functionName(), root, label);
227225
}
226+
227+
@Override
228+
public SkyKeyInterner<Key> getSkyKeyInterner() {
229+
return interner;
230+
}
228231
}
229232

230233
/** Constructs a key for loading a regular (non-prelude) .bzl. */

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717

1818
import com.google.common.annotations.VisibleForTesting;
1919
import com.google.common.base.MoreObjects;
20-
import com.google.common.collect.Interner;
2120
import com.google.devtools.build.lib.cmdline.Label;
22-
import com.google.devtools.build.lib.concurrent.BlazeInterners;
2321
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
2422
import com.google.devtools.build.lib.packages.BzlVisibility;
2523
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
2624
import com.google.devtools.build.lib.vfs.Root;
2725
import com.google.devtools.build.lib.vfs.RootedPath;
2826
import com.google.devtools.build.skyframe.SkyFunctionName;
2927
import com.google.devtools.build.skyframe.SkyKey;
28+
import com.google.devtools.build.skyframe.SkyKey.SkyKeyInterner;
3029
import com.google.devtools.build.skyframe.SkyValue;
3130
import java.util.Objects;
3231
import net.starlark.java.eval.Module;
@@ -70,7 +69,7 @@ public BzlVisibility getBzlVisibility() {
7069
return bzlVisibility;
7170
}
7271

73-
private static final Interner<Key> keyInterner = BlazeInterners.newWeakInterner();
72+
private static final SkyKeyInterner<Key> keyInterner = SkyKey.newInterner();
7473

7574
/** SkyKey for a Starlark load. */
7675
public abstract static class Key implements SkyKey {
@@ -157,6 +156,11 @@ protected final MoreObjects.ToStringHelper toStringHelper() {
157156
public String toString() {
158157
return toStringHelper().toString();
159158
}
159+
160+
@Override
161+
public SkyKeyInterner<Key> getSkyKeyInterner() {
162+
return keyInterner;
163+
}
160164
}
161165

162166
/** A key for loading a .bzl during package loading (BUILD evaluation). */

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
package com.google.devtools.build.lib.skyframe;
1616

17-
import com.google.common.collect.Interner;
18-
import com.google.devtools.build.lib.concurrent.BlazeInterners;
1917
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
2018
import com.google.devtools.build.skyframe.AbstractSkyKey;
2119
import com.google.devtools.build.skyframe.SkyFunction;
@@ -35,7 +33,7 @@ public static Key key(String keyString) {
3533
@AutoCodec.VisibleForSerialization
3634
@AutoCodec
3735
static class Key extends AbstractSkyKey<String> {
38-
private static final Interner<Key> interner = BlazeInterners.newWeakInterner();
36+
private static final SkyKeyInterner<Key> interner = SkyKey.newInterner();
3937

4038
private Key(String arg) {
4139
super(arg);
@@ -51,6 +49,11 @@ static Key create(String arg) {
5149
public SkyFunctionName functionName() {
5250
return SkyFunctions.CLIENT_ENVIRONMENT_VARIABLE;
5351
}
52+
53+
@Override
54+
public SkyKeyInterner<Key> getSkyKeyInterner() {
55+
return interner;
56+
}
5457
}
5558

5659
private final AtomicReference<Map<String, String>> clientEnv;

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
import static com.google.common.base.Preconditions.checkNotNull;
1818

1919
import com.google.common.base.MoreObjects;
20-
import com.google.common.collect.Interner;
2120
import com.google.devtools.build.lib.actions.ActionLookupKey;
2221
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
2322
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
2423
import com.google.devtools.build.lib.cmdline.Label;
25-
import com.google.devtools.build.lib.concurrent.BlazeInterners;
2624
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
2725
import com.google.devtools.build.skyframe.SkyFunctionName;
26+
import com.google.devtools.build.skyframe.SkyKey;
2827
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2928
import java.util.Objects;
3029
import javax.annotation.Nullable;
@@ -63,7 +62,7 @@ public class ConfiguredTargetKey extends ActionLookupKey {
6362
* Cache so that the number of ConfiguredTargetKey instances is {@code O(configured targets)} and
6463
* not {@code O(edges between configured targets)}.
6564
*/
66-
private static final Interner<ConfiguredTargetKey> interner = BlazeInterners.newWeakInterner();
65+
private static final SkyKeyInterner<ConfiguredTargetKey> interner = SkyKey.newInterner();
6766

6867
private final Label label;
6968
@Nullable private final BuildConfigurationKey configurationKey;
@@ -161,11 +160,16 @@ public final String toString() {
161160
return helper.toString();
162161
}
163162

163+
@Override
164+
public SkyKeyInterner<? extends ConfiguredTargetKey> getSkyKeyInterner() {
165+
return interner;
166+
}
167+
164168
@AutoCodec.VisibleForSerialization
165169
@AutoCodec
166170
static class ToolchainDependencyConfiguredTargetKey extends ConfiguredTargetKey {
167-
private static final Interner<ToolchainDependencyConfiguredTargetKey>
168-
toolchainDependencyConfiguredTargetKeyInterner = BlazeInterners.newWeakInterner();
171+
private static final SkyKeyInterner<ToolchainDependencyConfiguredTargetKey>
172+
toolchainDependencyConfiguredTargetKeyInterner = SkyKey.newInterner();
169173

170174
private final Label executionPlatformLabel;
171175

@@ -194,6 +198,11 @@ static ToolchainDependencyConfiguredTargetKey create(
194198
public final Label getExecutionPlatformLabel() {
195199
return executionPlatformLabel;
196200
}
201+
202+
@Override
203+
public final SkyKeyInterner<? extends ConfiguredTargetKey> getSkyKeyInterner() {
204+
return toolchainDependencyConfiguredTargetKeyInterner;
205+
}
197206
}
198207

199208
/** Returns a new {@link Builder} to create instances of {@link ConfiguredTargetKey}. */

0 commit comments

Comments
 (0)