msfroh commented on PR #16313:
URL: https://github.com/apache/lucene/pull/16313#issuecomment-5052152044

   > It's compiled into a table lookup over the input string's hashCode. How 
it's optimized within hotspot - I've no idea. 
https://docs.oracle.com/en/java/javase/26/docs/specs/jvms/jvms-3.html#jvms-3.10
   
   Ooh! I know this one. 
   
   Initially it'll do a binary search over the valid hashCodes 
(`lookupswitch`), then do the `String#equals` check to rule out a hash 
collision. Based on that, it will push the ordinal of the reached branch onto 
the stack to decide which code to jump to (`tableswitch`). In general, we're 
talking O(log n) versus the if/else chain's O(n). That's the code that will 
show up in the `javap` output, before any Hotspot optimization. 
   
   Hotspot can make a couple of possible optimizations to the `lookupswitch`: 
unroll the binary search, and potentially move hot cases before the binary 
search. I think that second one can be a big deal. Because `String#equals` is a 
method call, AFAIK the C2 compiler can't move hot cases to the top of an 
if-else chain, because it doesn't know for sure that each `if` body is 
side-effect-free, whereas any `switch` condition is guaranteed to be.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to