[concurrency-interest] CAS using a MethodHandle

Roman Elizarov elizarov at devexperts.com
Mon Dec 19 17:14:49 EST 2011


Is it possible to implement Atomic*FieldUpdater on top of the MethodHandle to Unsafe?
That is, place all checks in Atomic*FieldUpdater factory method, keep method handle(s) in final fields of updater and trivially implement CAS and other updater methods via method handle(s)? Can we thus get the best of two worlds -- type-safe and backwards-compatible API and better performance? // I assume, that HotSpot will be able to inline it all the way through, will not it? 

-----Original Message-----
From: concurrency-interest-bounces at cs.oswego.edu [mailto:concurrency-interest-bounces at cs.oswego.edu] On Behalf Of Doug Lea
Sent: Monday, December 19, 2011 11:16 PM
To: concurrency-interest at cs.oswego.edu
Subject: Re: [concurrency-interest] CAS using a MethodHandle

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).

-Doug





_______________________________________________
Concurrency-interest mailing list
Concurrency-interest at cs.oswego.edu
http://cs.oswego.edu/mailman/listinfo/concurrency-interest



More information about the Concurrency-interest mailing list