Repository navigation
[7.0] Resolve BeanManager.getBeans(Class) without building a Resolvable - #1
Open
AngeloRubens wants to merge 1 commit into
Open
AngeloRubens wants to merge 1 commit into
AngeloRubens wants to merge 1 commit into
Conversation
BeanManagerImpl.getBeans(Type, Annotation...) with no qualifiers and a plain Class type now goes through TypeSafeBeanResolver.resolveWithDefaultQualifier(), which keeps a small per-class map of the already resolved result. Facade types (Instance, Event, ...) and metadata types (InjectionPoint, Bean, ...) keep the old path. The map is replaced, not emptied, in clear(), so a result computed before clear() cannot be stored after it. Its size is capped by min(1024, RESOLUTION_CACHE_SIZE). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
BeanManagerImpl.getBeans(Type, Annotation...)is called with a plainClassand no qualifiers by, for example, RESTEasy'sCdiConstructorInjectoron every request. Today each call builds aResolvableBuilder, aResolvableImpl(hash over the types andthe qualifier sets) and goes through the resolver cache.
This PR adds
TypeSafeBeanResolver.resolveWithDefaultQualifier(Class): a per-class map of the already resolved result.qualifiers.length == 0andbeanType instanceof Class. Everything else takes the old path.Instance,Event, ...) and metadata types (InjectionPoint,Bean, ...) are excluded (ResolvableBuilder.isPlainBeanType) because their resolution is special-cased.clear(), aftersuper.clear(). Readers take the map reference before resolving, so a result computed beforeclear()cannot be stored into the map that is live after it.min(1024, RESOLUTION_CACHE_SIZE); past the cap, lookups just use the old path.The returned set is the same one the resolver returns today for
@Default; the code path only skips building the key.Measurements
JMH micro benchmark of one
BeanManager.getBeans()call (GetBeansBenchmark, Weld SE, 9 application scoped beans), run on GitHub Actions(JDK 21, 4-core shared runners), 5 forks x 2 interleaved rounds,
-prof gc. Baseline = weld#3552 head (d1fa54e), variant = baseline + this PR.Time per call, lower is better:
getBeans(Class), 1 threadgetBeans(Class), 8 classes in turn, 1 threadgetBeans(Class), 4 threadsgetBeans(Class), 8 classes in turn, 4 threadsgetBeans(Class, @Default), 1 threadgetBeans(Class, @Special), 1 threadThe two controls take the old path in both builds and do not move, so the difference comes from the shortcut and not from the machine.
The benchmark checks in
@Setupthat the shortcut returns the same set as the explicit@Defaultlookup, and the workflow checks that thejar under test contains (or, for the baseline, does not contain) the change.
Scope: this is the cost of the call itself on a cache hit, in isolation. It says how much each
getBeans(Class)call gets cheaper, not how mucha whole application gets faster; that depends on how often a caller (for example RESTEasy per request) makes the call.
Shared runners: absolute numbers move between runs, ratios inside a run are what count. I did not run the older
weld-core-benchmarkssuitefor this change.
Tests
DefaultQualifierLookupTest(6 cases): same beans as explicit@Default, facade and metadata types unchanged, bean that only has a custom qualifier is not a candidate, primitive finds the boxed bean, unknown type, and a lookup during boot (inAfterBeanDiscovery) must not hide a bean added afterwards.clear()case). The other five are characterization tests that pin the old behaviour and pass on both sides; they guard against semantic drift, they do not prove the optimization.getBeans(Type, Set<Annotation>)with an empty set, which does not use the shortcut.Semantics and risks
AbstractTypeSafeBeanResolver.clear()has a small window between invalidation and publishing that also exists for the existing cache;addBeandoes not clear the resolver. Fixing it would be a separate change.getBeans(Class), but I would also accept that as the better place.Disclosure
This change, its tests and the benchmark harness were written with AI assistance (Claude), and reviewed in two adversarial passes by a
separate reviewer agent (the first found a real race between
clear()and the store; that is why the map is replaced). I have run and read theresults above and I answer for the code. Benchmarks and workflow files: https://github.com/AngeloRubens/weld-core-benchmarks (branch
ci/getbeans-micro:getbeans/and the workflow).🤖 Generated with Claude Code