Eliminating Abstraction Overhead of Java Stream Pipelines using Ahead-of-Time Program Optimization

Anders Møller & Oskar Haarklou Veileborg, Aarhus University 

Read the paper

To evaluate the proposed approach we developed the Streamliner tool that performs ahead-of-time optimisations of stream pipelines in Java programs. The tool is packaged as a Maven project and is available in two different distributions:

The tool works as a bytecode-to-bytecode transformation, and can therefore be used as a post-processing step in a compilation pipeline. It applies aggressive inlining and stack allocation transformations to stream pipelines such as these:

private static List<String> toOptimize(String[] strings) {
    return Stream.of(strings)
            .filter(s -> s.startsWith("prefix"))
            .collect(Collectors.toList());
}

To obtain optimised, functionally equivalent code:

private static List<String> optimized(String[] strings) {
    int endExclusive = strings.length;
    if (strings == null) {
        throw new NullPointerException();
    }
    int arrayLength = ((Object[])strings).length;
    if (endExclusive > arrayLength) {
        throw new ArrayIndexOutOfBoundsException(endExclusive);
    }
    ArrayList state = new ArrayList();
    if (strings.length >= endExclusive) {
        int i = 0;
        if (endExclusive > 0) {
            do {
                String u;
                String string;
                if (!(string = (u = strings[i])).startsWith("prefix")) continue;
                ((List)state).add(u);
            } while (++i < endExclusive);
        }
    }
    return state;
}

In short, Java JITs do not handle the complexity of the stream library well, but are very good at optimising simple loops. By specialising the stream libary code ahead-of-time, the JIT is able to produce very efficient code at run-time. Read the paper for further details, and to get an overview of the performance improvements Streamliner can provide.

Two type pre-analyses are provided. The first is powered by Soot's SPARK analysis, and the second is powered by WALA's refinement based pointer analysis. For details on these, see the SPARKOracle and WALAOracle classes.

Contact {oskar,amoeller}@cs.au.dk if you experience issues with, or have questions about the tool.

Note that the tool is a research prototype, and is by no means production-ready. However, we will try our best to fix bugs when reported.