Le 07/11/2010 03:58, John Rose a écrit :
On Nov 6, 2010, at 3:50 PM, Rémi Forax wrote:
This introduces maxstack supplementary local variables.
Not sure the JIT will love that.
I should try that to see the overhead.
That should be fine. The distinction between stack and local should disappear
early in a good JIT. I think it does in both HotSpot JITs.
-- John
I've played a little with this idea.
Instead of using exceptions, I use invokedynamic and its lazy init
to bailout to the interpreter because the generated code
is more lightweight.
On my laptop, I've encoded by hand fib with overflow detection.
- the overflow test of n-1 and n-2 is done by testing if n is close to
Integer.MIN_INT.
- the overflow test of +is done using the hacker's delight hack.
Note that here the first ixor is not necessary if your compiler is
able to detect
that the result of fib is always positive.
This code use 3 supplementary local variables to reconstruct the stack
if necessary
and to avoid to compute the addition twice. You have to compute the addition
before being able to know if it's overflow or not.
This code takes 73 bytecodes instead of 21 for the original fib and
is a little less than 20% slower on my laptop which is not that bad.
Rémi
private static int fib(int);
Code:
0: iload_0
1: iconst_2
2: if_icmpge 7
5: iconst_1
6: ireturn
7: iload_0
8: dup
9: ldc #11 // int -2147483647
11: if_icmpeq 51
14: iconst_1
15: isub
16: invokestatic #13 // Method fib:(I)I
19: dup
20: istore_1
21: iload_0
22: dup
23: ldc #14 // int -2147483646
25: if_icmple 58
28: iconst_2
29: isub
30: invokestatic #13 // Method fib:(I)I
33: dup
34: istore_2
35: iadd
36: istore_3
37: iload_1
38: iload_2
39: ixor
40: iflt 49
43: iload_1
44: iload_3
45: ixor
46: iflt 65
49: iload_3
50: ireturn
51: iload_0
52: invokedynamic #17, 0 // NameAndType
bailout1:(I)Ljava/lang/Throwable;
57: athrow
58: iload_0
59: invokedynamic #19, 0 // NameAndType
bailout2:(I)Ljava/lang/Throwable;
64: athrow
65: iload_1
66: iload_2
67: invokedynamic #22, 0 // NameAndType
bailout3:(II)Ljava/lang/Throwable;
72: athrow
--
You received this message because you are subscribed to the Google Groups "JVM
Languages" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jvm-languages?hl=en.