50

I've seen a lot of questions here about Java lambdas performance, but most of them go like "Lambdas are slightly faster, but become slower when using closures" or "Warm-up vs execution times are different" or other such things.

However, I hit a rather strange thing here. Consider this LeetCode problem:

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

The problem was tagged hard, so I assumed that a linear approach is not what they want there. So I decided to come up with a clever way to combine binary search with modifications to the input list. Now the problem is not very clear on modifying the input list—it says "insert", even though the signature requires to return a reference to list, but never mind that for now. Here's the full code, but only the first few lines are relevant to this question. I'm keeping the rest here just so that anyone can try it:

public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
    int start = Collections.binarySearch(intervals, newInterval,
                                         (i1, i2) -> Integer.compare(i1.start, i2.start));
    int skip = start >= 0 ? start : -start - 1;
    int end = Collections.binarySearch(intervals.subList(skip, intervals.size()),
                                       new Interval(newInterval.end, 0),
                                       (i1, i2) -> Integer.compare(i1.start, i2.start));
    if (end >= 0) {
        end += skip; // back to original indexes
    } else {
        end -= skip; // ditto
    }
    int newStart = newInterval.start;
    int headEnd;
    if (-start - 2 >= 0) {
        Interval prev = intervals.get(-start - 2);
        if (prev.end < newInterval.start) {
            // the new interval doesn't overlap the one before the insertion point
            headEnd = -start - 1;
        } else {
            newStart = prev.start;
            headEnd = -start - 2;
        }
    } else if (start >= 0) {
        // merge the first interval
        headEnd = start;
    } else { // start == -1, insertion point = 0
        headEnd = 0;
    }
    int newEnd = newInterval.end;
    int tailStart;
    if (-end - 2 >= 0) {
        // merge the end with the previous interval
        newEnd = Math.max(newEnd, intervals.get(-end - 2).end);
        tailStart = -end - 1;
    } else if (end >= 0) {
        newEnd = intervals.get(end).end;
        tailStart = end + 1;
    } else { // end == -1, insertion point = 0
        tailStart = 0;
    }
    intervals.subList(headEnd, tailStart).clear();
    intervals.add(headEnd, new Interval(newStart, newEnd));
    return intervals;
}

This worked fine and got accepted, but with 80 ms runtime, while most solutions were 4-5 ms and some 18-19 ms. When I looked them up, they were all linear and very primitive. Not something one would expect from a problem tagged "hard".

But here comes the question: my solution is also linear at worst case (because add/clear operations are linear time). Why is it that slower? And then I did this:

Comparator<Interval> comparator = new Comparator<Interval>() {
    @Override
    public int compare(Interval i1, Interval i2) {
        return Integer.compare(i1.start, i2.start);
    }
};
int start = Collections.binarySearch(intervals, newInterval, comparator);
int skip = start >= 0 ? start : -start - 1;
int end = Collections.binarySearch(intervals.subList(skip, intervals.size()),
                                   new Interval(newInterval.end, 0),
                                   comparator);

From 80 ms down to 4 ms! What's going on here? Unfortunately I have no idea what kind of tests LeetCode runs or under what environment, but still, isn't 20 times too much?

user16217248
  • 3,119
  • 19
  • 19
  • 37
Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73
  • 3
    Have you **repeatedly** run this method and measured time? – Jan Jan 04 '16 at 06:08
  • 6
    See http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – Bart Kiers Jan 04 '16 at 06:14
  • I don't have access to the test cases, so I can't exactly run it repeatedly. I don't know what LeetCode does to achieve this weird performance, so the question is this: what it _could_ be doing to make it that bad? – Sergei Tachenov Jan 04 '16 at 07:45
  • 14
    If the code is executed only once (for the examples on [leetcode](https://leetcode.com/problems/insert-interval/)) the decreasing execution time might be related due to the fact that the lambda bytecode is **generated at runtime**. Whereas your anonymous comparator class is created **at compile time**. – SubOptimal Jan 04 '16 at 08:13
  • I believe that should really be the answer, thank you! I had no idea. I thought that lambdas were something like syntactic sugar for anonymous classes. – Sergei Tachenov Jan 04 '16 at 10:20
  • So far, my anecdotal experience (from my own use cases) is that lambdas are just slower than anonymous classes. However, by like a few percent, not any 20x... – Brian Knoblauch Jan 06 '16 at 20:53

1 Answers1

82

You are obviously encountering the first-time initialization overhead of lambda expressions. As already mentioned in the comments, the classes for lambda expressions are generated at runtime rather than being loaded from your class path.

However, being generated isn’t the cause for the slowdown. After all, generating a class having a simple structure can be even faster than loading the same bytes from an external source. And the inner class has to be loaded too. But when the application hasn’t used lambda expressions before¹, even the framework for generating the lambda classes has to be loaded (Oracle’s current implementation uses ASM under the hood). This is the actual cause of the slowdown, loading and initialization of a dozen internally used classes, not the lambda expression itself².

You can easily verify this. In your current code using lambda expressions, you have two identical expressions (i1, i2) -> Integer.compare(i1.start, i2.start). The current implementation doesn’t recognize this (actually, the compiler doesn’t provide a hint neither). So here, two lambda instances, having even different classes, are generated. You can refactor the code to have only one comparator, similar to your inner class variant:

final Comparator<? super Interval> comparator
  = (i1, i2) -> Integer.compare(i1.start, i2.start);
int start = Collections.binarySearch(intervals, newInterval, comparator);
int skip = start >= 0 ? start : -start - 1;
int end = Collections.binarySearch(intervals.subList(skip, intervals.size()),
                                   new Interval(newInterval.end, 0),
                                   comparator);

You won’t notice any significant performance difference, as it’s not the number of lambda expressions that matters, but just the class loading and initialization of the framework, which happens exactly once.

You can even max it out by inserting additional lambda expressions like

final Comparator<? super Interval> comparator1
    = (i1, i2) -> Integer.compare(i1.start, i2.start);
final Comparator<? super Interval> comparator2
    = (i1, i2) -> Integer.compare(i1.start, i2.start);
final Comparator<? super Interval> comparator3
    = (i1, i2) -> Integer.compare(i1.start, i2.start);
final Comparator<? super Interval> comparator4
    = (i1, i2) -> Integer.compare(i1.start, i2.start);
final Comparator<? super Interval> comparator5
    = (i1, i2) -> Integer.compare(i1.start, i2.start);

without seeing any slowdown. It’s really the initial overhead of the very first lambda expression of the entire runtime you are noticing here. Since Leetcode itself apparently doesn’t use lambda expressions before entering your code, whose execution time gets measured, this overhead adds to your execution time here.

See also “How will Java lambda functions be compiled?” and “Does a lambda expression create an object on the heap every time it's executed?”

¹ This implies that JDK code that will be executed before handing control over to your application doesn’t use lambda expressions itself. Since this code stems from times before the introduction of lambda expressions, this is usually the case. With newer JDKs, modular software will be initialized by different, newer code, which seems to use lambda expressions, so the initialization of the runtime facility can’t be measured within the application anymore in these setups.

² The initialization time has been reduced significantly in newer JDKs. There are different possible causes, general performance improvements, dedicated lambda optimizations, or both. Improving initialization time in general, is an issue that the JDK developers did not forget.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • 2
    Any documentation to enforce your answer (and help us to convince sceptical) ? – yunandtidus Jan 06 '16 at 20:27
  • 6
    @yunandtidus: I added links to other questions (whose answers provide even more links). The fact, that ASM is used, is an implementation detail, that won’t appear in a specification, but can be seen by looking at the [source code](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/lang/invoke/InnerClassLambdaMetafactory.java#249) – Holger Jan 06 '16 at 20:38
  • This is maybe more than initially expected by OP, but very interesting in my opinion. Thanks for pointing this out. – yunandtidus Jan 06 '16 at 20:42
  • Has the first-time initialization overhead of lambda expressions improved in JDK 11? – lalo Jan 29 '19 at 14:38
  • 4
    @lalo a quick test on my machine suggests that the first time initialization has been improved by factor four between JDK 8 and JDK 11, though, it’s not clear whether this is the result of a dedicated lambda optimization or a general speedup. It’s still more than the initialization of a single inner class, but mind that we’re talking about a single-time overhead of ~10 ms on my machine. Also, you only notice it when no-one used them before; just specifying a `--module-path` on the command line make it disappear; apparently, the code handling application modules does use lambda expressions. – Holger Jan 29 '19 at 15:31
  • @Holger this "first-time" slowness was part why my team abandoned `Nashhorn`; we have *lots* of code that relied on it, but because of `invokedynamic` used and being rather slow for the first runs (in our case in some cases it was 25-30 s) we dropped it a few years ago. Might be time to test this against java-11, but no idea if someone would want to re-write those portions of the code though – Eugene Jan 29 '19 at 20:40
  • 1
    @Eugene It’s “Nashorn” (German for rhinoceros) and it has been deprecated for removal with JDK 11 ([JDK-8202786](https://bugs.openjdk.java.net/browse/JDK-8202786)). Honestly, I never understood why someone would want to have JavaScript in their application, unless when implementing a web browser… – Holger Jan 30 '19 at 07:37
  • @Holger as usual, it's some decision that was not really in developers control, anyway very useful info that it is removed! thank you – Eugene Jan 30 '19 at 08:46
  • 2
    @Eugene not “is” but “will be” – Holger Jan 30 '19 at 08:52
  • 1
    @Holger I would suggest moving [this comment](https://stackoverflow.com/questions/34585444/java-lambdas-20-times-slower-than-anonymous-classes#comment95659060_34642063) as a part of an edit. It becomes relevant now that JDK-11 has been around for some time. – Naman Aug 07 '20 at 09:03