A brief note and question about how JRuby optimizes case/when statements in some cases.
When a case/when is homogeneous, JRuby may emit an optimized version that is faster than O(n) performance, as in other implementations. For example, if the elements are all Fixnums, JRuby will emit a JVM bytecode "lookupswitch" like this: case a when 1 when 2 when 3 when 4 end ...yields... LOOKUPSWITCH 1: L2 2: L3 3: L4 4: L5 default: L6 This optimization is done for Fixnum, Symbol, and String. In the case of Symbol and String, there are two modes: single-character (byte), which is compiled similar to the Fixnum version; and hash-based. The hash-based version uses the hash of the String or Symbol as the key for the lookupswitch, as in this case: case a when 'foo' when 'bar' when 'baz' end ...yields... LOOKUPSWITCH 841490416: L2 841490424: L3 876516207: L4 default: L5 Now there's actually a subtle bug here I need to fix: it doesn't compensate for hash collisions. Nobody's ever reported it, so it's probably not common, but I'll fix it anyway. So, my question is this: how common is it for folks to use homogeneous case/when constructs with literal Fixnums, Symbols, or Strings? Is this optimization worth keeping (and fixing)? - Charlie --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email