3 ms·
This is similar to a technique used in Renjin to compile specific vector computation expressions down to straight-line JVM bytecode, which is then JITed down to
by bedatadriven 10y ago
This is similar to a technique used in Renjin to compile specific vector computation expressions down to straight-line JVM bytecode, which is then JITed down to machine code. It's a very powerful technique because it removes all the indirection involved in a evaluating dynamic expressions and lets the processor just do its job.
For example, if you evaluate sum(sqrt(x^2 + y^2) * 3) in Renjin, and x or y happen to be very long vectors, then we'll jit out a JVM class for this specific expression that would look something like this in Java:
class JittedComputation1E5374A3 {
SEXP compute(SEXP[] args) {
double[] x = args[0].toDoubleArrayUnsafe();
double[] y = args[1].toDoubleArrayUnsafe();
double sum = 0;
for(int i=0;i<x.length;++i) {
double xi = x[i];
double yi = y[i]
sum += Math.sqrt(xi*xi+yi*yi) * 3;
}
return DoubleVector.valueOf(sum);
}
}
The computation is specialized to the types of x and y, so if for example x is a sequence 1:1000000 then a new class gets written for that doesn't even use an array for x.
The speedup is so impressive that even if you don't cache the compiled expression you see dramatic improvements: http://www.renjin.org/blog/2015-06-28-renjin-at-rsummit-2015.html http://www.renjin.org/blog/2015-06-28-renjin-at-rsummit-2015...