[concurrency-interest] CAS using a MethodHandle

Doug Lea dl at cs.oswego.edu
Mon Dec 19 14:15:38 EST 2011

On 12/19/11 13:28, Rémi Forax wrote:
> Hi all,
> Some time ago, I said that we should try to use a MethodHandle instead of an
> Atomic*FieldUpdater and let the VM inline the method handle so there should be
> no cost (or a little one) compared to directly calling unsafe.compareAndSwapObject.
> The following code does exactly that, I have also include a small test that just
> demonstrates that it works.
> Because I'm neither a benchmark expert nor an assembler expert, I've just
> checked that the method handle is fully inlined by the JIT and that the
> generated code seems to don't have more code than it should.

Thanks! This is potentially better than FieldUpdaters since it avoids
some of the dynamic type checks.  The checks inside your MethodHandle
returning method look either identical or compatible with updaters.

And on jdk7u2 (but not 1.7.0)  performance seems better.

I'll try experimenting with using this approach on the other various forms
(scalars; stores/loads; maybe somehow arrays) and contemplate a
general factory API that when put in place could be used instead
of updaters (and if all goes well, might even lead to deprecating them).


More information about the Concurrency-interest mailing list