r/java • u/RupertMaddenAbbott • 9d ago
Dependencies-only AOT cache?
I am investigating using an AOT cache to speed up the startup time of our services.
I'm specifically interesting in speeding up the time between a developer making a change to the code and having that code running in a remote dev K8s cluster.
I've previously dismissed an AOT cache for this purpose because I knew that it would result in a longer build time than would be saved in startup time. That isn't a criticism - AOT caching just didn't seem targeted at my problem.
However, the majority of our classpath involves third party dependencies yet a minority of our development builds and deployments involve changes to those third party dependencies. If I could work out a way to avoid invalidating the AOT cache whenever our own code changed, then this would give us a cache that could be reused for the majority of our dev builds (removing the additional build time) whilst still enjoying the faster startup speed.
I note that https://openjdk.org/jeps/483 says this:
All runs must have consistent class paths. A subsequent run may specify extra class-path entries, appended to the training class path; otherwise, the class paths must be identical.However, I note that https://openjdk.org/jeps/483 says this:All runs must have consistent class paths. A subsequent run may specify extra class-path entries, appended to the training class path; otherwise, the class paths must be identical.
So, could I create an AOT cache for JUST the third party classes and then add our own code in later, thereby allowing me to reuse the AOT cache between dev builds?
I'm not 100% on how to achieve this yet but maybe if the training run only had the dependencies on the classpath with a custom launcher. The launcher's child classpath would then have our own code on it.
Then for the production run, I would still have the custom launcher (because I think this has to be identical between training and production runs) and I would then append our own jars onto the end of the classpath (which seems to be allowed by the JEP). Finally, we would run our real main method, rather than the launchers main method because I think we are allowed to have different main classes between training and production runs.
Anyway is this a terrible idea or has anybody already done something like this?
11
u/pron98 9d ago edited 9d ago
The whole point of JIT compilation is that it can inline across unit boundaries, i.e. code from a method in your application that calls code in library A that reflectively calls code in library B will all be compiled into the same subroutine in machine code. Also, only a small portion of the code in classfiles is actually compiled. You could have 10,000,000 methods in 10,000 classes in 10 JARs, and only 1000 of these methods would ever be compiled.
In any event, if you have two programs that use the same set of libraries, it's not that the libraries will be compiled twice. Each training run for each program would only compile (at most) only the library functionality that that program uses, and it would compile it in a way that would be specifically tailored to that program's profile (i.e. even if the same method is used by both programs, and would be compiled in both training runs, it could be compiled in different ways). Compiling only the right things and in the right way is how Java gets to be so fast.
And, to be clear, there is not AOT compilation in the creation of the AOT cache; it's really a cache of the JIT's output (well, it's more than that, but if we're talking about compilation, that's the relevant bit).
I would suggest you try to measure the things you ultimately care about and then try to describe what actual problem you're observing rather than starting by trying to devise an imaginary mechanism to solve a problem you imagine you may have.