On Mon, 14 Apr 2025 14:47:59 GMT, Per Minborg <pminb...@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 recommended by @iwanowww, an approach is to write 2 to `hashIsZero` if we 
calculated a non-zero hash, so we can constant-fold zero hashes like:



var isZero = hashIsZero;
if (isZero == 1) return 0;
if (isZero == 2) {
    int h = hash;
    if (h != 0) return h;
}
return computeHash(); // and set hash, hashIsZero fields


I tried something like that at 
https://github.com/liachmodded/jdk/commit/f265710ce7d2d02ee51d6429b5603ecc0590c383,
 but did not submit because it end up still having a regression compared to 
baseline in remi's benchmark, with the resulting throughput/average cost 
between the other constant cases and the non-folding string hash cases.

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

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

Reply via email to