Release reference to the delegate supplier in InterruptibleSupplier.Memoize.
I don't have any evidence that this is causing a significant memory leak, but it theoretically could be if the delegate supplier holds a reference to an expensive object (which is easy to do unintentionally with a lambda).
Also got rid of unnecessary InterruptibleSupplier.Instance class and added unit tests.
RELNOTES: None.
PiperOrigin-RevId: 234166340
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/TargetPatternValue.java b/src/main/java/com/google/devtools/build/lib/skyframe/TargetPatternValue.java
index 0237af1..c9d5da3 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/TargetPatternValue.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/TargetPatternValue.java
@@ -333,8 +333,8 @@
Iterable<PathFragment> blacklistedPackagePrefixes) throws InterruptedException {
ImmutableSet.Builder<PathFragment> excludedPathsBuilder = ImmutableSet.builder();
excludedPathsBuilder.addAll(getExcludedSubdirectories());
- excludedPathsBuilder.addAll(getAllBlacklistedSubdirectoriesToExclude(
- new InterruptibleSupplier.Instance<>(blacklistedPackagePrefixes)));
+ excludedPathsBuilder.addAll(
+ getAllBlacklistedSubdirectoriesToExclude(() -> blacklistedPackagePrefixes));
return excludedPathsBuilder.build();
}
diff --git a/src/main/java/com/google/devtools/build/skyframe/InterruptibleSupplier.java b/src/main/java/com/google/devtools/build/skyframe/InterruptibleSupplier.java
index 5f4ab55..db5b7ab 100644
--- a/src/main/java/com/google/devtools/build/skyframe/InterruptibleSupplier.java
+++ b/src/main/java/com/google/devtools/build/skyframe/InterruptibleSupplier.java
@@ -13,31 +13,20 @@
// limitations under the License.
package com.google.devtools.build.skyframe;
+import com.google.common.base.Preconditions;
import javax.annotation.Nullable;
/** Supplier that may throw {@link InterruptedException} when value is retrieved. */
public interface InterruptibleSupplier<T> {
T get() throws InterruptedException;
- class Instance<T> implements InterruptibleSupplier<T> {
- private final T instance;
-
- public Instance(T instance) {
- this.instance = instance;
- }
-
- @Override
- public T get() {
- return instance;
- }
- }
-
- class Memoize<T> implements InterruptibleSupplier<T> {
- private final InterruptibleSupplier<T> delegate;
- private @Nullable T value = null;
+ /** Memoizes the result of {@code delegate} after the first call to {@link #get}. */
+ final class Memoize<T> implements InterruptibleSupplier<T> {
+ private InterruptibleSupplier<T> delegate;
+ @Nullable private volatile T value = null;
private Memoize(InterruptibleSupplier<T> delegate) {
- this.delegate = delegate;
+ this.delegate = Preconditions.checkNotNull(delegate);
}
public static <S> InterruptibleSupplier<S> of(InterruptibleSupplier<S> delegate) {
@@ -55,6 +44,7 @@
synchronized (this) {
if (value == null) {
value = delegate.get();
+ delegate = null; // Free up for GC.
}
}
return value;
diff --git a/src/test/java/com/google/devtools/build/skyframe/InterruptibleSupplierTest.java b/src/test/java/com/google/devtools/build/skyframe/InterruptibleSupplierTest.java
new file mode 100644
index 0000000..bcf29fe
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/skyframe/InterruptibleSupplierTest.java
@@ -0,0 +1,69 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.google.devtools.build.skyframe;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.testing.GcFinalization;
+import java.lang.ref.WeakReference;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link InterruptibleSupplier}. */
+@RunWith(JUnit4.class)
+public final class InterruptibleSupplierTest {
+
+ private int callCount = 0;
+ private String returnVal = "";
+ private CallCounter callCounter = new CallCounter();
+
+ private final class CallCounter {
+ public String call() {
+ ++callCount;
+ return returnVal;
+ }
+ }
+
+ @Test
+ public void memoize_returnsCorrectResult() throws Exception {
+ InterruptibleSupplier<String> supplier = InterruptibleSupplier.Memoize.of(callCounter::call);
+ returnVal = "abc";
+
+ String result = supplier.get();
+
+ assertThat(result).isEqualTo("abc");
+ }
+
+ @Test
+ public void memoize_onlyCallsDelegateOnce() throws Exception {
+ InterruptibleSupplier<String> supplier = InterruptibleSupplier.Memoize.of(callCounter::call);
+
+ supplier.get();
+ supplier.get();
+
+ assertThat(callCount).isEqualTo(1);
+ }
+
+ @Test
+ public void memoize_freesReferenceToDelegeteAfterGet() throws Exception {
+ InterruptibleSupplier<String> supplier = InterruptibleSupplier.Memoize.of(callCounter::call);
+ WeakReference<Object> ref = new WeakReference<>(callCounter);
+ callCounter = null;
+
+ supplier.get();
+
+ GcFinalization.awaitClear(ref);
+ }
+}