Skip to content

Improve uber jar performance by looking up classes and resources directly in nested jars - #52246

Merged
philwebb merged 2 commits into
spring-projects:mainfrom
janwaleed09:nested-jar-direct-lookup
Oct 17, 2026
Merged

philwebb merged 2 commits into
spring-projects:mainfrom
janwaleed09:nested-jar-direct-lookup

Conversation

@janwaleed09

Copy link
Copy Markdown
Contributor

Closes #52245

JarUrlClassLoader now looks up classes and resources in nested jars through NestedJarFile directly. Before, URLClassPath created a URL and opened a URLConnection for every nested jar on every lookup. Now a URL is only created for entries that exist, and packages are defined with the manifest of the jar that contains the class.

The new code is only used when all class path URLs are nested jar URLs and the name doesn't need URL encoding. Otherwise, the existing code is used. Class code sources, resource URLs and their order, multi-release entries, code signers and the sealing check stay as they are. I compared them with the current loader for PetClinic and a sample app (15,268 and 14,014 classes, 1,397 and 976 resource lookups).

The measurements are in #52245. In short, with --cpus=2: 8-13% faster startup, 43-50% less memory allocated during startup, and less CPU for requests that resolve static resources.

The tests cover missing resources, resources from several nested jars, a class path with other URLs, and the org.slf4j.helpers case, where a lookup of a missing class stopped the package from getting its manifest attributes.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. label Oct 15, 2026
@philwebb philwebb self-assigned this Oct 16, 2026
@philwebb philwebb changed the title Look up classes and resources directly in nested jars Improve uber jar performance by looking up classes and resources directly in nested jars Oct 17, 2026
@philwebb philwebb added the type: enhancement A general enhancement label Oct 17, 2026
@philwebb philwebb added this to the 4.2.x milestone Oct 17, 2026
@philwebb philwebb removed the status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. label Oct 17, 2026
janwaleed09 and others added 2 commits October 17, 2026 00:14
Optimize `JarUrlClassLoader` to directly lookup classes and resources
in nested jars when the classpath only contains `jar:nested:...` URLs.

See spring-projectsgh-52246

Signed-off-by: Jan Mohr <[email protected]>
Update the `JarUrlClassLoader` lookup optimizations used when
all URLs are nested. A new `NestedOnlyOptimizedLoader`
inner-class now groups all the related optimizations into a
single location.

In addition, the optimizations now support URL encoding and
so can be used with all class/resource names.

A further optimization has also been added to bypass loading
entirely when a name is known to be invalid.

See spring-projectsgh-52246
@philwebb
philwebb force-pushed the nested-jar-direct-lookup branch from 22b2d90 to 059525f Compare October 17, 2026 07:24
@philwebb
philwebb merged commit 8b5c1e7 into spring-projects:main Oct 17, 2026
3 of 4 checks passed
@philwebb philwebb modified the milestones: 4.2.x, 4.2.0-RC1 Oct 17, 2026
@philwebb

Copy link
Copy Markdown
Member

Thanks very much @janwaleed09, this is a very interesting optimization. I've polished things up a bit in 059525f, including adding URL encoding support which I hope means we can use this with all class names. I hope I've not broken anything to undone any of the performance improvements.

I'd really like to backport this to earlier versions, but I feel it's a bit risky. I think I will backport one small optimization I added in the polish commit that bypasses loading entirely if we know a name isn't valid. That seems low risk and could even be considered a bug.

Thanks again!

houxianghui pushed a commit to houxianghui/spring-boot that referenced this pull request Oct 17, 2026
Backport part of the polish commit from spring-projectsgh-52246 to improve performance
when names are known to be invalid.

Closes spring-projectsgh-52271
ikhoon pushed a commit to line/armeria that referenced this pull request Oct 26, 2026
Motivation:

`DocStringExtractor` and `XdsResourceReader` scan jars with shaded
Reflections 0.9.11, which obtains the jar through
`JarURLConnection.getJarFile()` with caching enabled and closes it after
scanning. The closed `JarFile` can be the cached instance shared with a
class loader.

We observed rare `ClassNotFoundException`s in applications using
`DocService` (Spring Boot 3.2.2 with Armeria 1.27.1 in production, and
Spring Boot 3.5.15 with Armeria 1.34.2 in development). In the
[reproduction](https://github.com/cprayer/armeria-docservice-jar-repro/tree/3a26552b77288b50e7492c4884fee8b4e649d263)
(Armeria 1.42.0, Spring Boot 3.5.16), starting a `Server` with
`DocService` once did not trigger the failure. Repeating
`DocStringExtractor` scans while other threads read from the same jar
made 173 of 174 `armeria-grpc` classes fail to load in most runs with a
`ClassNotFoundException` without a cause, thrown through the same Spring
Boot class loader frames as the observed failures. With this change, no
class failed.

A likely mechanism is a race with Spring Boot's nested jar loader.
Closing a cached `JarFile` normally removes it from the cache, so later
lookups open a new one. However,
[`JarUrlConnection.connect()`](https://github.com/spring-projects/spring-boot/blob/v3.5.16/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/jar/JarUrlConnection.java)
gets the cached `JarFile`, looks up the entry and then calls
`cacheIfAbsent()`. If the scan closes the jar after the entry lookup and
before `cacheIfAbsent()`, the closed `JarFile` is cached again and class
loading from that jar fails until the JVM restarts. We did not observe
this interleaving directly.

This affects Spring Boot 3.2 through 4.1, which use the nested jar
loader introduced in 3.2. Spring Boot 4.2 is not released yet, but
[spring-boot#52246](spring-projects/spring-boot#52246)
(4.2.0-RC1) looks up classes directly in nested jars, which appears to
take class loading out of this race, although that was not its intent.
On a 4.2.0-SNAPSHOT build, no class failed to load in the reproduction,
but the scan itself still failed with `Zip file closed`.

The same ownership problem was fixed in [Elastic
APM](elastic/apm-agent-java#4543) (identified in
[spring-boot#50925](spring-projects/spring-boot#50925 (comment)))
and [Datadog](DataDog/dd-trace-java#6720).
Reflections itself [disabled caching in its jar URL
handler](ronmamo/reflections@e56cd65)
in 0.9.12.

The 0.9.11 pin was added for [a `JarFile.stream()`
failure](ronmamo/reflections#279) caused by
Spring Boot's loader, which was [fixed in Spring Boot
2.3.6.RELEASE](spring-projects/spring-boot#23821).

Modifications:

- Upgrade Reflections from 0.9.11 to 0.9.12 and remove the pin comment.
- Adapt the gRPC utility to JDK predicates and the builder integration
test to the updated Store API.
- Add tests for shared `JarFile` ownership, filename filtering and xDS
type discovery.
- Note that Reflections 0.9.12 throws `ReflectionsException` when a
scanner finds nothing. `DocStringExtractor` already guards against this,
and `XdsResourceReader` always finds the xDS API types.

Result:

- Reflections scans no longer close a cached `JarFile` shared with a
class loader.
- The new regression test fails with `zip file closed` on 0.9.11 and
passes on 0.9.12.
- With the upgraded core, gRPC and xDS shaded jars in Spring Boot
executable jars (Java 25), `JarFile.stream()`, `DocStringExtractor` and
`XdsResourceReader` work and leave the shared jar open on Spring Boot
2.3.12.RELEASE, 2.7.18, 3.5.16 and 4.1.1. Spring Boot 2.3.4.RELEASE
fails with `zip file closed` as expected ([local
check](https://github.com/cprayer/armeria-docservice-jar-repro/blob/3a26552b77288b50e7492c4884fee8b4e649d263/VALIDATION.md),
[Reflections-only compatibility
repro](https://github.com/cprayer/armeria-docservice-jar-repro/tree/3a26552b77288b50e7492c4884fee8b4e649d263/compatibility)).
- Targeted core, gRPC and xDS tests and checkstyle pass on JDK 25.

AI-assisted: AI tools were used to assist with implementation,
validation, and drafting this PR description.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Class and resource lookups in executable jars open a URL connection for every nested jar

3 participants