From 682df9edbc96183b72687a82e605e8615e7808fa Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Tue, 20 Jul 2021 11:02:37 -0700 Subject: [PATCH] Simplify ResultCallbacks by not using WeakReference This is a follow up CL to my previous CL [1], which introduced ResultCallbacks in a bit conservative way, that is, to always wrap Completable.* with WeakReference in case the target app fails to respond for some reasons. Using WeakReference would make sense if otherwise we would put more stress on the memory than holding WeakReference object itself. In our case the expected memory stress on extending lifetime of Completable.* objects is still comparable with the memory cost of holding WeakReference objects instead. Let's remove WeakReference from ResultCallbacks in favor of simplicity. This change should be completely transparent to other layers since there is no change in the semantics (except for the lifetime itself of Completable.* object, which is technically observable with APIs such as java.lang.ref.Cleaner). [1]: Ic65a95eb5d0fd56f505a02fd9083bcf6694b6734 f87f75088899d0f7513132e0f397b0edbf373d71 Bug: 192412909 Test: atest -c CtsInputMethodTestCases Change-Id: I43966494ccb4fceb8e7f8c60bd6ce3cc7fbfdd82 --- .../internal/inputmethod/ResultCallbacks.java | 35 ++++++------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/core/java/com/android/internal/inputmethod/ResultCallbacks.java b/core/java/com/android/internal/inputmethod/ResultCallbacks.java index 398cffbe1142c..6838cbda4ab9b 100644 --- a/core/java/com/android/internal/inputmethod/ResultCallbacks.java +++ b/core/java/com/android/internal/inputmethod/ResultCallbacks.java @@ -21,7 +21,6 @@ import android.annotation.BinderThread; import android.annotation.NonNull; import android.annotation.Nullable; -import java.lang.ref.WeakReference; import java.util.concurrent.atomic.AtomicReference; /** @@ -38,15 +37,9 @@ public final class ResultCallbacks { @AnyThread @Nullable - private static T unwrap(@NonNull AtomicReference> atomicRef) { - final WeakReference ref = atomicRef.getAndSet(null); - if (ref == null) { - // Double-call is guaranteed to be ignored here. - return null; - } - final T value = ref.get(); - ref.clear(); - return value; + private static T unwrap(@NonNull AtomicReference atomicRef) { + // Only the first caller will receive the non-null original object. + return atomicRef.getAndSet(null); } /** @@ -58,8 +51,7 @@ public final class ResultCallbacks { */ @AnyThread public static IIntResultCallback.Stub of(@NonNull Completable.Int value) { - final AtomicReference> - atomicRef = new AtomicReference<>(new WeakReference<>(value)); + final AtomicReference atomicRef = new AtomicReference<>(value); return new IIntResultCallback.Stub() { @BinderThread @@ -95,8 +87,7 @@ public final class ResultCallbacks { @AnyThread public static ICharSequenceResultCallback.Stub of( @NonNull Completable.CharSequence value) { - final AtomicReference> atomicRef = - new AtomicReference<>(new WeakReference<>(value)); + final AtomicReference atomicRef = new AtomicReference<>(value); return new ICharSequenceResultCallback.Stub() { @BinderThread @@ -122,8 +113,7 @@ public final class ResultCallbacks { @AnyThread public static IExtractedTextResultCallback.Stub of( @NonNull Completable.ExtractedText value) { - final AtomicReference> - atomicRef = new AtomicReference<>(new WeakReference<>(value)); + final AtomicReference atomicRef = new AtomicReference<>(value); return new IExtractedTextResultCallback.Stub() { @BinderThread @@ -149,8 +139,7 @@ public final class ResultCallbacks { @AnyThread public static ISurroundingTextResultCallback.Stub of( @NonNull Completable.SurroundingText value) { - final AtomicReference> - atomicRef = new AtomicReference<>(new WeakReference<>(value)); + final AtomicReference atomicRef = new AtomicReference<>(value); return new ISurroundingTextResultCallback.Stub() { @BinderThread @@ -174,8 +163,7 @@ public final class ResultCallbacks { */ @AnyThread public static IBooleanResultCallback.Stub of(@NonNull Completable.Boolean value) { - final AtomicReference> - atomicRef = new AtomicReference<>(new WeakReference<>(value)); + final AtomicReference atomicRef = new AtomicReference<>(value); return new IBooleanResultCallback.Stub() { @BinderThread @@ -209,8 +197,7 @@ public final class ResultCallbacks { */ @AnyThread public static IVoidResultCallback.Stub of(@NonNull Completable.Void value) { - final AtomicReference> atomicRef = - new AtomicReference<>(new WeakReference<>(value)); + final AtomicReference atomicRef = new AtomicReference<>(value); return new IVoidResultCallback.Stub() { @BinderThread @@ -246,8 +233,8 @@ public final class ResultCallbacks { @AnyThread public static IIInputContentUriTokenResultCallback.Stub of( @NonNull Completable.IInputContentUriToken value) { - final AtomicReference> - atomicRef = new AtomicReference<>(new WeakReference<>(value)); + final AtomicReference + atomicRef = new AtomicReference<>(value); return new IIInputContentUriTokenResultCallback.Stub() { @BinderThread