[concurrency-interest] DirectByteBuffers and reachabilityFence

Vitaly Davidovich vitalyd at gmail.com
Tue Dec 8 10:35:55 EST 2015


>
> Okay.  Can you tell me the relevance of this, though?


Relevance is if code works correctly in interpreter then, practically
speaking, compiler tries to not break it.  Of course this isn't always the
case (e.g. compiler hoisting reads of a non-volatile field that should be
volatile), but is generally true.

Or simply determine that nothing depends on position, so an update to
> it doesn't have to be generated.


This is irrelevant.  There are, today, artifacts the optimizer leaves
behind even if it eliminates certain other operations to preserve behavior,
warranted by spec or simply being cautious about breaking code.

Not really: the JMM determines what is possible.  It's not just
> "anything".


Yes, "anything" was hyperbole.

It might already have happened.  The problem with reasoning in such a
> fragile way is that things get broken by accident.  The current Unsafe
> support works by accident: as we have seen, copyMemory works because
> position is updated after the call to Unsafe.  People who are
> reasoning in terms of the compiler don't necessarily think of all the
> incorrect things library authors might have done.


There are other operations in DBB that touch native memory that appear to
work without any position update (i.e. the various getters).  Again, I'm
not advocating reasoning like this for new code that someone may write.
The thread started with Alexandre asking a question for which he then added
a segfaulting example; we tried to figure out why his didn't work and DBB
works.  By definition, this is speculation unless you're expecting me to
provide a full trace through the compiler optimization pipeline.  At no
point did I advise/suggest that someone should reason like this for their
code, I was merely trying to use educated guesses as to why it works
keeping compiler optimizations in mind.

As to the theoretical vs practical aspect, I agree that there's nothing
holding this together spec/theory wise; afterall, I'm quite happy that
reachabilityFence is being added (don't particularly like that name, but
whatever).  But if you create a spec conforming JVM implementation today
that segfaults in DBB, congrats but nobody is going to use it.  Moreover,
once reachabilityFence is added and sprinkled in all the appropriate JDK
places, there may be a time when someone advertently or inadvertently makes
a compiler optimization that will break DBB-like clones in user code.  My
hunch, given the mindset of java and emphasis on not breaking code, even
code that's misbehaved and not conforming to spec, is that such
optimization will not go forward.  There are already cases where JVM treads
carefully to cater to java code out in the wild; the fact that final fields
are not treated as constants due to fear of reflection updates is a prime
example which actually could have tangible performance benefits if it
weren't so.  AFAIK, there's nothing in the spec that states it's legal to
update final fields.

We can agree on the spec all we want, but the reality/practical aspects are
more nuanced.




On Tue, Dec 8, 2015 at 10:17 AM, Andrew Haley <aph at redhat.com> wrote:

> On 12/08/2015 02:40 PM, Vitaly Davidovich wrote:
> >>
> >> The lifetime, natural or otherwise, of an instance does not survive
> >> until an instance method returns because, a lot of the time, that
> >> instance method is inlined.
> >
> > You're talking about optimization here (inlining); by "natural" I
> > meant the naive/no optimization case (e.g. interpreter, debugger
> > attached w/breakpoint in method, etc).
>
> Okay.  Can you tell me the relevance of this, though?
>
> > It's not just HotSpot, though: some VMs are even more aggressive, and
> >
> > Which java VMs are these? Just curious.
>
> IBM's J9.
>
> >> we have seen finalizers executed even before constructors have
> >> completed.  And that is allowed by the specification.
> >
> > Ok, but that's beside the point, really.  Surely if compiler can
> > optimize and arrange for liveness to allow for it, then it's a good
> > thing it does that.  My point isn't that this cannot happen due to
> > spec, but rather that in places like DBB where `this` is used after
> > the Unsafe call the compiler has to schedule things differently in
> > order to reduce lifetime.
>
> Or simply determine that nothing depends on position, so an update to
> it doesn't have to be generated.
>
> > And my point is that compilers generally tend to be cautious in
> > doing things that may break code.  This is the practical aspect we
> > were referring to - it's actual humans writing these optimizations,
> > and they're sensitive to breaking code, particularly in java.
> > Theoretically, yes, anything is possible.
>
> Not really: the JMM determines what is possible.  It's not just
> "anything".
>
> > It's already broken.  Sure.  Now try to submit a patch to Hotspot
> > that will break this case, even if allowed by spec, and see how far
> > you get :).
>
> It might already have happened.  The problem with reasoning in such a
> fragile way is that things get broken by accident.  The current Unsafe
> support works by accident: as we have seen, copyMemory works because
> position is updated after the call to Unsafe.  People who are
> reasoning in terms of the compiler don't necessarily think of all the
> incorrect things library authors might have done.
>
> Andrew.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20151208/6881ab69/attachment.html>


More information about the Concurrency-interest mailing list