RFR: 8354300: Fields in String are not trusted

Per Minborg pminborg at openjdk.org
Wed Apr 16 07:23:45 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.

As discussed, we could integrate the current PR and then revisit `j.l.String` later. I have raised a new issue for this:

https://bugs.openjdk.org/browse/JDK-8354777

The output of the attached benchmark in the `777 issue confirms zero hash values are not constant folded:


Benchmark                     Mode  Cnt  Score   Error  Units
StringHashCodeStatic.empty    avgt   15  5.774 ± 0.143  ns/op
StringHashCodeStatic.nonZero  avgt   15  0.571 ± 0.012  ns/op
StringHashCodeStatic.zero     avgt   15  4.039 ± 0.038  ns/op


Unless no one objects within a day or so, I will integrate this PR.

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

PR Comment: https://git.openjdk.org/jdk/pull/24625#issuecomment-2808627521


More information about the core-libs-dev mailing list