RFR: 8372845: Fold identity hash code if object is constant

Tobias Hartmann thartmann at openjdk.org
Tue Dec 2 06:44:49 UTC 2025


On Tue, 2 Dec 2025 02:49:52 GMT, Chen Liang <liach at openjdk.org> wrote:

>  I think it may be possible for users to create a Object::hashCode site with a constant receiver that is of a specialized class that overrides hashCode.

Yes, I think so too. We need a test for this scenario.

Just an observation: This patch will only allow folding during parsing. I would expect that often, opportunities only arise after other optimizations already took place. For example, something like this would not be optimized if we run with `-XX:+AlwaysIncrementalInline`, right?


    static final Object a = new Object();
    
    @ForceInline
    public Object getter(Object obj) {
        return obj;
    }

    public long test() {
        return getter(a).hashCode();
    }


Another example:

Object val = new Object();
 
int limit = 2;
for (; limit < 4; limit *= 2);
for (int i = 2; i < limit; i++) {
    val = a;
}

return val.hashCode(); // After loop opts, C2 knows that val == a


So ideally, we would move this optimization to IGVN. This would also help Valhalla, where we need to (re-)compute the hashcode for a scalarized value object and would therefore like to fold the computation as aggressively as possible.

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

PR Comment: https://git.openjdk.org/jdk/pull/28589#issuecomment-3600438904


More information about the hotspot-compiler-dev mailing list