Make the guava-android
copy of AbstractFuture
try VarHandle
when run under the JVM.
#7759
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Make the
guava-android
copy ofAbstractFuture
tryVarHandle
when run under the JVM.Under Android, I still don't have us even try
VarHandle
:VarHandle
is possible with our current internal Android build setup, as discussed in the internal commentary on cl/711733182.VarHandle
there may at least somewhat more complex: My testing suggests that some versions of Android exposeVarHandle
to our reflective check but don't actually exposeMethodHandles.Lookup.findVarHandle
. Accordingly, sgjesse@ recommends using a check onSDK_INT
(33 or higher) instead of reflection under Android. Sinceguava-android
can be used under the JVM, too, we'd need to use a combination of the SDK check and reflection. And we'd need to perform theSDK_INT
check reflectively, as we do inTempFileCreator
(which, hmm, I should clean up as obsolete one of these days...). (We could at least reduce the amount of reflection we need if we were to depend on the Android SDK at build time, as discussed in b/403282918.)VarHandle
is faster or slower thanUnsafe
there (and I can't easily tell because of the build problems above).Unsafe
under Android.This CL has two advantages for JVM users of
guava-android
:Unsafe
under newer JDKs. Note thatAbstractFuture
already has run correctly if access toUnsafe
is outright disallowed (as with-sun-misc-unsafe-memory-access=deny
): It detects this and falls back to an alternative implementation. However, ifUnsafe
is available but produces warnings,guava-android
would use it, triggering those warnings. This CL makes Guava not even try to use it under newer JVMs because it now triesVarHandle
first.VarHandle
may be marginally faster thanUnsafe
under the JVM, as discussed in cl/711733182. (It also doesn't lead to VM crashes if you manage to passnull
where you shouldn't, as I did back in b/397641020 :) But that's more something that's nice for us as Guava developers, not something that's nice for Guava users.)This CL is probably the most prominent part of our migration of
guava-android
offUnsafe
. It is debatable whether it is the most important, given that at least one class,Striped64
, does not seem to have a fallback at all ifUnsafe
is unavailable. Still, this CL addresses the warning that most users are seeing, and it gives us some precedent for how to deal withStriped64
.Finally, it looks like our existing tests for
VarHandle
had a mismatch between the context class loader and the class loader that we loadAbstractFutureTest
in? That seems fairly bad. This CL fixes it, extracting a method to guard against future such mismatches. Out of an abundance of caution, I made a similar change inAggregateFutureStateFallbackAtomicHelperTest
, even though there's not really an opportunity for a mismatch there, given that there's only one alternative class loader.RELNOTES=
util.concurrent
: Changed theguava-android
copy ofAbstractFuture
to tryVarHandle
beforeUnsafe
, eliminating a warning under newer JDKs.