[concurrency-interest] DirectByteBuffers and reachabilityFence

Vitaly Davidovich vitalyd at gmail.com
Tue Dec 8 07:07:25 EST 2015

sent from my phone
On Dec 8, 2015 4:58 AM, "Andrew Haley" <aph at redhat.com> wrote:
> On 07/12/15 19:17, Vitaly Davidovich wrote:
> >>
> >> Correct me if I'm wrong, but basically what you are saying is that
> >> does not perform advanced enough analysis to see that 'this' is
> >> unnecessary after the call to unsafe.
> >
> >
> > In which case? In DBB, `this` is still necessary after the Unsafe call
> > since it needs to update position.  In theory, it could move position
> > update prior to calling Bits.copyToArray() but it would have to prove
> > it's safe to do so.
> That's not at all hard for HotSpot to do.
> > Part of that safety check would require it to reason about memory
> > effects, side effects, and failure cases (i.e. position update isn't
> > reached if there's an exception,
> What exception?  HotSpot knows everything about Bits.copyToArray()
> because it is inlined.  It knows about all the arguments and
> eliminates bounds checks.

This was a general statement about optimization.  By exception, I really
mean control flow.

> > it would need to preserve that effect, also taking into account that
> > it may not be able to speculatively write to memory and then undo
> > those effects in case of exception).  So the answer is probably
> > "yes" to your question above, in a theoretical sense only.
> Not at all.

Anything more substantive here?

> > I think the reasoning here is similar to the above.
> > ByteBuffer.get() is invoked on a DBB instance, compiler sees a call
> > to Unsafe.getXXX at some point.  The "natural" lifetime of the
> > instance survives until the instance method returns.
> No, this is completely wrong.

Same here

> > So if compiler now wanted to shorten this by observing that `this`
> > is unreachable sooner than method exit, it has to prove a bunch of
> > things like I mentioned above.
> Now inline everything.
> Please stop sepeculating about what an optimizer can and can't do.
> Please instead concerntrate on the semantics of the language and think
> about correctness.

It's speculation based on observing a real compiler and looking at its code
generation in various scenarios.  The bottom line is that it plays
conservative a lot of the time, and that is the sentiment I've picked up
from various conversations on the compiler list over the years.  Yes, it's
speculation and a Sufficiently Smart Compiler can do things differently.
But keep in mind if hotspot started scheduling operations more aggressively
around Unsafe usage a lot of code would break.

> Andrew.
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20151208/69fd08d7/attachment-0001.html>

More information about the Concurrency-interest mailing list