RFR: 8261731: shallow copy the internal buffer of a scalar-replaced java.lang.String object

Xin Liu xliu at openjdk.java.net
Tue Feb 16 20:59:41 UTC 2021


On Tue, 16 Feb 2021 08:14:22 GMT, Richard Reingruber <rrich at openjdk.org> wrote:

> Hi,
> 
> this is a smart optimization.
> 
> > There are 3 nodes involving in the construction of a java.lang.String object.
> > ```
> > 1. Allocate of itself, aka. alloc
> > 
> > 2. AllocateArray of a byte array, which is value:byte[], aka. aa
> > 
> > 3. ArrayCopyNode which copys in the contents of value, aka. ac
> > ```
> > 
> > 
> > Lemma
> > When a String object `alloc` is scalar replaced, C2 can eliminate `aa` and `ac`.
> > Because `alloc` is scalar replaced, it must be non-escaped. The field value:byte[] of j.l.String cannot be seen by external world, therefore it must not be global escaped. Because the buffer is marked as stable, it is safe to assume its contents are whatever ac copies in. Because all public java.lang.String constructors clone the incoming array, the source of `ac` is stable as well.
> 
> Are you saying that the source of `ac` cannot be accessed by another thread because of the cloning in the constructor? But the resulting string instance which is used to construct the non-escape instance can be GlobalEscape and then the source of `ac` is accessible to other threads, isn't it?

Hi, @reinrich 
You might not know, but I learn how to reallocate an object in deoptimization from your previous patches. Thank you!

You are right. The source of ac (`src`) might be escaped.  I didn't say other threads can't access it. I said we need to guarantee the `src` is stable,  or  it's a "frozen" array in JDK-8261007. 

To be honest, I didn't check the src is frozen. In practice, I only see ArrayCopy in construction [here](https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/StringLatin1.java#L769).This method is creating a new substring from an established array, so its value is `stable`. 

After I read your comment, I went through String.java. I do find one open-ended constructor. yes, it's a problem!
https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/String.java#L395

If the frozen attribute is not present, what I can come up with. an array is "stable". What's do you think?
1) has annotation "stable" or
2) it's non-escaped and
3) can't find any store nodes along its mem stream.

-------------

PR: https://git.openjdk.java.net/jdk/pull/2570


More information about the hotspot-compiler-dev mailing list