Reducing Binary Size with Code Compression
Motivation
Ahead-of-time (AOT) compilation produces machine code before an application starts. The resulting executable includes methods that are reachable in principle but may never run during a particular execution. Such methods still occupy space in the executable. Code compression aims to reduce this space while limiting execution overhead, particularly when only part of an application's compiled code is needed for a given workload.
Implementation
The approach selects methods for compression at build time. When execution profiles are available, selection can use them to identify methods that were not called during profiling. These observations describe the profiled workload; they do not guarantee that a method will remain unused in later executions.
Selected methods are grouped into clusters, each stored as an independent compressed stream. The remaining methods stay uncompressed in the executable's regular code area. Methods are grouped into clusters according to the packages and classes they belong to, because it is likely that a call to a method will require other methods from the same class.
On-demand decompression and runtime integration
Calls involving selected methods use indirect call targets. The first call to a compressed method reaches a runtime resolver. The resolver decompresses the containing cluster into a reserved memory region, makes its code executable, and updates the cluster's call targets to the installed addresses. Subsequent calls use these addresses without repeating decompression, although call indirection remains.
Runtime integration also covers the metadata needed for stack walking, exception handling, and garbage collection. Metadata lookup accounts for the separate runtime code region so that installed methods remain visible to these mechanisms. Installed clusters remain in memory for the process lifetime in case they are needed again.
Acknowledgements
Code compression is developed as part of the research collaboration between the Faculty of Mathematics, University of Belgrade, and Oracle LLC.

