On 28 July 2010 06:20, Charles Oliver Nutter <[email protected]> wrote:
> I think I just thunk up a way to optimize integer math to primitives
> safely in JRuby. Perhaps this will work for us and for other languages
> attempting to do the same thing.
[snip]
Ng has pretty much the Groovy semantics for arithmetic operators which
means that any arithmetic operation can be overridden globally or on a
per thread basis. The standard semantics are Java's but there is a
standard override which gives graceful handling of overflow.
Arithmetic operations are translated into method calls on a thread
local dispatcher.
Taking addition as an example: there is an addQuick() method with
takes two ints and returns an int and an add() method which takes two
ints and returns an Object (there are lots of flavours of addition
methods which deal with different type combinations but we can ignore
them for now).
When I know I'm adding two ints I use the addQuick(int,int) method. If
the semantics of add have been changed so that the operation no longer
returns an int the addQuick() method throws an exception. I catch this
exception and use the Object version of add() to perform the
operation.
The effect of this is to try to do the calculation using normal, fast,
operations and if any of the operations can't be performed that way
throw the entire calculation away and do it the slow, general, way.
Example
1 + a + 2 (a is known to be an int)
try {
tc.addQuick(tc.addQuick(1, a), 2)
catch(CantDoPimativeOperationException e) {
tc.add(tc.add(tc.wrap(1), tc.wrap(a)), tc.wrap(2))
}
tc is the per thread dispatch object and wrap() turns a primitive into
the appropriate object.
It seems a similar approach to the one you are suggestion and does
result in reasonable performance.
John Wilson
--
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.