Memory Segment efficient array handling
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Thu Apr 1 14:26:09 UTC 2021
On 01/04/2021 14:33, Ty Young wrote:
> Is the expectation that people have to buy and read a metaphorical
> "Java <X> for dummies"book every new Java release just so they know
> what the current set of optimizations are and in what situations they
> do and don't come into play?
On this point - the optimization we're talking about here is escape
analysis. One of the first docs I find on Google on the topic is
https://wiki.openjdk.java.net/display/HotSpot/EscapeAnalysis
Which is a 2009 document. So, it's not like we're talking about bleeding
edge here. It's a technique that has been around for years and which
works with some degree of success (see below).
In the case of the simple bulk copy routines discussed here, it is easy
to see how all the intermediate heap segment and the various slices
never leave the method where they are used - they are just temporary
artifacts. In such cases, Hotspot will try hard (as the benchmark shows)
to remove allocation. But this is nothing new.
A different thing is e.g. to write a method that computes a slice *and*
returns it - in that case the slice "escapes" the method - so in such a
case you can't robustly rely on Hotspot to be able to figure it out (in
the case where the slicing method is only called in one place and
inlined there, well, maybe Hotspot could detect it doesn't escape that
new context).
AFAIK, the biggest impediment to escape analysis is dependency on
control flow. So, if you have a method which branches, and create a
slice in one case and a different slice in another, in general Hotspot
will refuse to escape-analyze that. GraalVM has a better escape-analysis
logic (known as "partial escape analysis") which helps out in that case;
Valhalla completely side-steps the issue by marking, at declaration
site, which classes can reliably avoid allocation.
Since, usual disclaimer, I'm not a VM engineer, a muuuch better
explanation on the topic, with some benchmarks, written by Alex
Shipilev, can be found here:
https://shipilev.net/jvm/anatomy-quarks/18-scalar-replacement/
Maurizio
More information about the panama-dev
mailing list