Are L-world value types (from project valhalla) useless in practice?

194 views Asked by At

I wrote two versions of a program to test the performance impact of object allocations in a java program. One uses simple java objects:

class Point {
    public final double x;
    public final double y;

    public Point(double a, double b) {
        x = a;
        y = b;
    }

    public double len() {
        return Math.sqrt(x*x + y*y);
    }

    public Point minus(Point o) {
        return new Point(x-o.x, y-o.y);
    }
}
// ...

public static void test () {
    Point[] points = new Point[size];
    for (int i=0; i<size; i++) {
        points[i] = new Point(rnd.nextDouble(), rnd.nextDouble());
    }
    double sum = 0;
    for (int i=0; i<size; i++) {
        for (int j=i; j<size; j++) {
            sum += points[i].minus(points[j]).len();
        }
    }
}

and other flattens the java objects into local variables:

class PointStatic {
    static class PointBuffer {
        double x;
        double y;
    }

    static public double len(double x, double y) {
        return Math.sqrt(x*x + y*y);
    }

    static public void minus(double x, double y, double o_x, double o_y, PointBuffer b) {
        b.x = x - o_x;
        b.y = y - o_y;
    }
}
// ...

public static void test () {
    double[] points_x = new double[size];
    double[] points_y = new double[size];
    for (int i=0; i<size; i++) {
        points_x[i] = rnd.nextDouble();
        points_y[i] = rnd.nextDouble();
    }
    double sum = 0;
    PointStatic.PointBuffer b = new PointStatic.PointBuffer();
    for (int i=0; i<size; i++) {
        for (int j=i; j<size; j++) {
            PointStatic.minus(points_x[i], points_y[i], points_x[j], points_y[j], b);
            sum += PointStatic.len(b.x, b.y);
        }
    }
    System.gc();
}

Both of the tests accomplish the same (computation heavy) task, the only difference is that the later doesn't allocate any point objects. I used the following runner to test these implementations:

static int size = 50000;
static int iters = 5;

public static void main (String[] args) throws InterruptedException {
    for (int i=0; i<iters; i++) {
        System.out.println(i);
        Thread[] threads = new Thread[8];
        for (int j=0; j<8; j++) {
            threads[j] = new Thread(new Runnable() {
                public void run() { test(); }
            });
            threads[j].start();
        }
        for (int j=0; j<8; j++) {
            threads[j].join();
        }
        System.gc();
    }
}

On my machine, the first version took 2:36 minutes, and the second (optimized) version took 2:29 minutes. As you can see the difference is negligible! Does that mean the jit compiler makes the value types to be added in java 12 useless?

It might be possible that my test case is not a good emulation of L-world value types. In that case, what are the basic concepts behind L-world values types of project valhalla?

0

There are 0 answers