On Feb 8, 2012, at 3:00 PM, Charles Oliver Nutter wrote: > I have been creating specific versions for long and double RHS because > it does save some overhead to not construct the box or get a cached > box. I think it's worth it for common cases (int, long, float, double) > at least.
Hotspot has some optimizations for collapsing box/unbox pairs, as in (int)(Integer)(int)n. I forget when they kick in, sorry. Meanwhile, you can sometimes arrange for exactly one generic routine to automatically specialize in different caller contexts, based on the types at the call site. That was the outcome of this experiment: http://blogs.oracle.com/jrose/entry/an_experiment_with_generic_arithmetic The key is to make the type tests in the generic function be transparent enough to the JIT. Example: Object plus(Object x, Object y) { // we expect this function to inline and customize if (x instanceof Integer && y instanceof Integer) return (int)x + (int)y; return slowPlus(x, y); // slow path is out-of-line } The function must be kept simple to encourage the JIT to inline your fast path, but not get scared away by a complex slow path. Then, calls like the following will constant-fold the type tests: x = plus(1, 2); // no type tests left x = plus(x, 3); // one type test left x = plus((int)x, 4); // no type tests left static final MethodHandle plusCustom = (::plus).asType(methodType(Object, int, int)); // or invokedynamic x = plusCustom.invokeExact((int)x, 5); // no type tests left > I don't think the smaller types would gain you much unless you have > behavioral differences you need to express for them. In JRuby, there's > only one integer type and one floating-point type (not counting > arbitrary-precision types). I would try to rely on JIT customization of a generic. If there were measurable bottlenecks, I would add just a few hand-customized versions. > JRuby doesn't have any static typed numerics for LHS, so we only have > about 20 special paths for math, boolean, and comparison operators > with Object LHS and long or double RHS, which currently are only used > for literals (e.g. a + 1, b < 10.0, etc). Likewise, I would think that the (Object,int)Object version of +, etc., would be useful to Jochen. But only few like this are needed, to hit the sweet spot without overkill. The motivation for these cases comes mainly from loop control and array indexes. (...In languages where those are your iteration tools; sigh.) -- John -- You received this message because you are subscribed to the Google Groups "JVM Languages" group. To post to this group, send email to jvm-languages@googlegroups.com. To unsubscribe from this group, send email to jvm-languages+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/jvm-languages?hl=en.