RFR: 8354300: Fields in String are not trusted

Chen Liang liach at openjdk.org
Wed Apr 16 05:41:48 UTC 2025


On Wed, 16 Apr 2025 05:12:28 GMT, Rémi Forax <forax at openjdk.org> wrote:

>> src/java.base/share/classes/java/lang/String.java line 183:
>> 
>>> 181:      * us to avoid recalculating this.
>>> 182:      */
>>> 183:     @Stable
>> 
>> Suggestion:
>> 
>>     // Marking hashIsZero stable is meaningless, as a read of `0` on hash blocks
>>     // constant folding of reading hashIsZero
>> 
>> A compromise. A stable on the hash field is a net improvement; we can investigate constant folding the zero hashes later.
>
> yes ! I would write
> "given the current implementation of hashCode(), marking hasIsZero @Stable is not useful for constant folding, as the field `hash` of value '0' is read first."

Yep. To be more exact, JIT anticipates a read of `hash` to be non-zero at any future point instead of trusting it to be zero when it observes `hashIsZero == 1`.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/24625#discussion_r2046111256


More information about the core-libs-dev mailing list