A few months ago I wrote a post about startup times of AWS Lambdas written in Java. This post has a similar title, but a different topic: it looks at the first-run time for lambdas (lowercase) in a Java program, and has nothing to do with AWS. Although I did discover this issue while writing code for AWS. Confused yet?
Lambda expressions were added to the Java language with the release of Java 8 in 2014. By now I'm assuming every Java programmer has used them, if only as arguments to higher-order functions in the java.util.stream
package:
List<String> uppercasedNames = names.stream() .map(s -> s.toUpperCase()) .collect(Collectors.toList());
You can implement your own higher-order functions, with parameter types from the java.util.function
package (or, if you have more complex needs, defining your own functional interfaces). So, for example, you might have a function with this signature:
public static String retryLambda(long interval, long timeout, Supplier<String> lambda) throws Exception
This can be called with any lambda expression that doesn't take arguments and returns a string. For example:
retryLambda(50, 100, () -> Instant.now().toString());
As you might have guessed from the signature, this function retries some operation. But before I dig into the implementation, here's some background about why you'd implement such a function. Most of my recent posts have referenced my AWS logging library, and this one's no different. When working with AWS, you need to be prepared to retry operations: either because AWS throttled the request (returning an error that indicates you should retry after a short delay), or because operations are eventually-consistent (there's a delay between creating something and being able to use it). As a result, AWS code can include a lot of retry loops:*
long timeoutAt = System.currentTimeMillis() + timeout; while (System.currentTimeMillis() < timeoutAt) { String value = doSomething(); if (value != null) return value; Thread.sleep(interval); } throw new RuntimeException("timeout expired");
That's seven lines of boilerplate wrapping one line that actually does something. Functional programming is all about getting rid of boilerplate, so I implemented a function that would accept a lambda:**
public static String retryLambda(long interval, long timeout, Supplier<String> lambda) throws Exception { long timeoutAt = System.currentTimeMillis() + timeout; while (System.currentTimeMillis() < timeoutAt) { String value = lambda.get(); if (value != null) return value; Thread.sleep(interval); } throw new RuntimeException("timeout expired"); }
The hardcoded loops can now be replaced with a call to this function:
retryLambda(50, 250, () -> doSomething());
All well and good, and it reduced the size of the code, but then my tests started failing.
When you're actually talking to AWS, you might need a timeout of 30 seconds or more. But you definitely don't want such a long timeout in a unit test. To solve that problem, I replaced the interval
and timeout
arguments with much shorter values: 50 and 200 milliseconds. And then my tests would assert the number of times the function was called: based on those values, the operation should be attempted four times before timing out. However, I was seeing that they were only executed two or three times.
When I dug into the problem, what I discovered is that the first execution of a lambda takes 40 to 50 milliseconds on my Core i7-3770K running Oracle Java 1.8.0_271. I knew there was a lot happening behind the scenes to make lambdas work, but wow, that's nearly infinity!
I also ran on an EC2 m5a.xlarge
instance running AWS Linux 2, and saw that it took over 70 milliseconds with OpenJDK 1.8.0_272, but only 18 milliseconds running Corretto 11.0.10.9.1. I have to assume that the performance improvement is similar across Java11 implementations, but haven't tested. If you'd like to try it out yourself, I've created a GitHub Gist with the test program.
One thing that I do not want you to take from this post is the idea that Java lambdas are bad, or are poorly implemented. I didn't delve too deeply into what happens during that first invocation, but suspect that the JVM is loading something from disk (much like the initial JVM startup time). And in my experiments, invoking additional, different lambdas did not add to the execution time. So, like anything Java, lambdas are best used in a long-running program.
However, if you are in a similar situation, testing timing-dependent code that utilizes lambdas, you need to be prepared. When I ran into the problem, I simply wanted to move on with my life and relaxed the assertions (the primary assertion was elapsed time, which didn't change; it was simply the number of invocations). Now, after thinking about the problem and writing the example program for this post, I think I'd use a @BeforeClass
function to “warm up” the lambda mechanism.
* Not all AWS code needs to have retry loops. But, for example, if you create a Kinesis stream you will need to wait until it becomes active before writing to it. I've seen some mock implementations of AWS services that don't accurately reflect these delays, leading to code that fails in the real world.
* Actually, I implemented a class, which was far easier to replace during testing. For an example if its use, look here.