Skip to content

Add dump-warm-image: images of Java programs with their code compiled - #24

Merged
atgreen merged 2 commits into
masterfrom
warm-images
Oct 20, 2026
Merged

atgreen merged 2 commits into
masterfrom
warm-images

Conversation

@atgreen

@atgreen atgreen commented Oct 20, 2026

Copy link
Copy Markdown
Owner

Builds on #23, which it includes as its first commit (815fe06). Merge #23 first, and this PR's diff shrinks to its own commit, 64f621d.

OpenLDK compiles each Java method on first call, and for a big program that's most of its startup time. dump-app-image (make kawa) saves a program with its classpath but none of that work. make-javac-image saves it, but only for javac. This adds a general version.

dump-warm-image (src/cli.lisp)

(openldk::dump-warm-image "app" "com.example.Main"
                          :classpath "app.jar"
                          :warmups '((:args ("sample-input")) (:input "typed\nlines\n")))

It runs main once per warmup, with arguments and/or stdin, at build time, under the same timeout and error handling as javac's warmups. Then it saves the image with every method those runs compiled.

  • Warm code, cold state (default). Before saving, the application's classes are reset: static fields go back to their initial values and <clinit> runs again on first use, so main starts fresh.
  • :reset-classes nil. Keeps the state the warmups built, for programs whose <clinit>s can't rebuild their statics. main has to reuse that state.

Checked with a program that counts its runs in a static field: a fresh JVM prints runs=1, the default image runs=1, and the :reset-classes nil image runs=2.

Shared save-time cleanup

The cleanup inside make-javac-image (threads, locks, classpath handles, jimage buffers, caches) is now %reset-runtime-for-save, shared by both builders. It also makes files survive a restart:

  • FileInputStream, FileOutputStream and RandomAccessFile streams open at save time reopen at their saved position, through an sb-ext:*init-hooks* hook. Checked with /dev/urandom and a half-read text file: the image resumes at the next byte.
  • ZipFile reopens its jar lazily.

javacl was rebuilt with the shared cleanup and still compiles Hello.java correctly.

Example: make abcl

make abcl downloads ABCL 1.9.2, compiles ABCLMain, and runs lib/abcl/build-image.lisp, which warms ABCL up with one REPL session. ABCL's compiled Lisp classes get their statics from its fasl loader, so this example uses :reset-classes nil, and ABCLMain now reuses the interpreter it finds.

Cold ./abcl
Launch to prompt, then (quit) ~3.5 min 0.4 s
Short session (defun, defclass, defgeneric, defmethod, loop) over 3.5 min 7 s

The image is 533 MB and builds in about 6 minutes.

Limitation: the warmup doesn't call compile. OpenLDK can't yet load ABCL-compiled code containing closures, and that fails cold too: (compile 'make-adder) on a closure fails on master. The generated classes refer to each other and only ABCL's in-memory class loader can find them, but the JIT resolves class constants without asking the defining loader (src/classfile.lisp:217) and substitutes a stub. If the warmup preloaded ABCL's compiler, ABCL would also compile forms like defclass and hit this. It's documented in docs/ABCL.md as a known issue, along with a second bug found along the way: Field.get(null) on a static field of a custom-loader class returns null.

Docs

docs/IMAGES.md covers make-image, dump-app-image and dump-warm-image, with Kawa, ABCL and javacl as examples, and explains when to use :reset-classes nil. It also flags a catch: in an app image, the first command-line argument is taken as a main class. .cache/ (already the Makefile's XDG_CACHE_HOME) and /abcl are now gitignored.

Testing: make check-regression couldn't run locally (no DejaGnu), so CI's gate is the full check. Locally, zipfile, filestat, waitwake, monitorwake and jitwake pass on the rebuilt openldk.

🤖 Generated with Claude Code

atgreen and others added 2 commits October 20, 2026 05:28
ABCL now boots on JDK 25 and runs its REPL, but two JDK 8 leftovers
stopped it right after low-level initialization:

- java.util.zip.ZipFile, implemented in Lisp, kept its state in JDK 8's
  name and jzfile fields.  The class takes its fields from JDK 25's
  ZipFile.class, which has neither, so opening any jar directly failed.
  Keep the path and open zip in a weak side table instead, as url.lisp
  already does for JarFile, add the ZipFile(String) constructor, and load
  ZipEntry before getEntry instantiates it.
- UnixFileSystem's getLastModifiedTime0, getLength0 and list0 were only
  defined under their JDK 8 names.  Define the JDK 25 names, returning 0
  for a missing file as the JDK does, and have the old names delegate.

New aaa/zipfile.java and aaa/filestat.java cover both.  docs/ABCL.md and
the README now describe the JDK 25 setup and current status.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
dump-warm-image runs a Java program on sample inputs (arguments and/or
stdin) at build time, then saves an SBCL image with the methods those runs
compiled.  By default it resets the application's classes first (static
fields back to their initial values, <clinit> runs again), so main starts
fresh; :reset-classes nil keeps the warmup's state, for programs whose
statics their <clinit>s can't rebuild.

The pre-save cleanup that lived inside make-javac-image is now the shared
%reset-runtime-for-save, which also makes files survive the restart:
FileInputStream/FileOutputStream/RandomAccessFile streams reopen at their
saved position through an init hook, and ZipFile reopens its jar lazily.

make abcl builds ./abcl, a warm ABCL image that reaches its prompt in
under a second instead of ~3.5 minutes.  ABCLMain reuses an existing
interpreter so the booted Lisp in the image is used.  The warmup avoids
COMPILE: OpenLDK can't yet load ABCL-compiled closures (documented in
docs/ABCL.md; it fails cold too).

docs/IMAGES.md describes make-image, dump-app-image and dump-warm-image
with Kawa, ABCL and javacl as examples.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@atgreen
atgreen merged commit 142083f into master Oct 20, 2026
2 checks passed
@atgreen
atgreen deleted the warm-images branch October 20, 2026 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant