-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
(2011/11/16 6:45), Charles Oliver Nutter wrote:
> 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
For others who may interested; see
org.jruby.compiler.ASTCompiler#getOptimizedCases()
> 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.
As you already know (just for FYI to others... if exists :-), Java 7
compiler dumps hashCode() lookupswitch jump then call String#equals()
to check. You're planning similar change I guess.
> 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)?
No idea about the question. (Sorry!) But with the above change, it
might not need to be homogeneous? Just a wild guess...
// NaHi
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
iQEcBAEBAgAGBQJOw0ApAAoJEC7N6P3yLbI2AxsIAIqgxZNyvPtRzyGDjHo0RvBl
FxXMajv9witAf3Yfq32Hyul4JMk/wHr7CUvia0wGKRwLfY6dLgM/tnei3FYXclNe
Qd95imS9iMmnN7ktPnaxTUN9KQNEXO13ccbeL/gid2ThEA0K4jBv01CZBhRU0VGo
ff1Hb+2XdZkRxtYtF/k1J/LznT1ZJo8lCmVm46ujJACkv0GXsiGdittTE4e1HwhR
6oyIeUjod14BDnunMyUyeSaqMrUZW63ohKWs2vQoaRHx7k3JT0QrSWtSkyYCGoxl
31ZyFzVONGS6jmuaEkHmFcD/z12jj0YcW632dmCG/MtX9W6STBju2OyKivq/3/s=
=1S7A
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email