RFR: 8354300: Fields in String are not trusted

Chen Liang liach at openjdk.org
Wed Apr 16 00:03:46 UTC 2025


On Mon, 14 Apr 2025 14:47:59 GMT, Per Minborg <pminborg at openjdk.org> wrote:

> This PR proposes to add the `@Stable` annotation to `j.l.String.hash` and `j.l.String.hashIsZero`. This means the VM can trust these fields to never change which enables constant folding optimizations.
> 
> This PR is tested in tier1, tier2, tier3, and tier4 which all pass.

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.

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

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


More information about the core-libs-dev mailing list