Skip to content

[7.0] Resolve BeanManager.getBeans(Class) without building a Resolvable - #1

Open
AngeloRubens wants to merge 1 commit into
pr/request-cache-mainfrom
pr/resolve-default-main
Open

AngeloRubens wants to merge 1 commit into
pr/request-cache-mainfrom
pr/resolve-default-main

Conversation

@AngeloRubens

@AngeloRubens AngeloRubens commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

What

BeanManagerImpl.getBeans(Type, Annotation...) is called with a plain Class and no qualifiers by, for example, RESTEasy's
CdiConstructorInjector on every request. Today each call builds a ResolvableBuilder, a ResolvableImpl (hash over the types and
the qualifier sets) and goes through the resolver cache.

This PR adds TypeSafeBeanResolver.resolveWithDefaultQualifier(Class): a per-class map of the already resolved result.

  • Only for qualifiers.length == 0 and beanType instanceof Class. Everything else takes the old path.
  • Facade types (Instance, Event, ...) and metadata types (InjectionPoint, Bean, ...) are excluded (ResolvableBuilder.isPlainBeanType) because their resolution is special-cased.
  • The map is replaced, not emptied, in clear(), after super.clear(). Readers take the map reference before resolving, so a result computed before clear() cannot be stored into the map that is live after it.
  • Size is capped at 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:

benchmark baseline with this PR change
getBeans(Class), 1 thread 136.8 ns 3.5 ns -97%
getBeans(Class), 8 classes in turn, 1 thread 147.3 ns 4.0 ns -97%
getBeans(Class), 4 threads 255.0 ns 7.2 ns -97%
getBeans(Class), 8 classes in turn, 4 threads 269.5 ns 11.1 ns -96%
allocation per call 640 B ~0 B -100%
control: getBeans(Class, @Default), 1 thread 318.1 ns 319.7 ns +0.5% (noise)
control: getBeans(Class, @Special), 1 thread 375.2 ns 371.9 ns -0.9% (noise)

The 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 @Setup that the shortcut returns the same set as the explicit @Default lookup, and the workflow checks that the
jar 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 much
a 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-benchmarks suite
for this change.

Tests

  • Full Weld test suite and TCK (SE) green on this tree (CI on a fork).
  • New 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 (in AfterBeanDiscovery) must not hide a bean added afterwards.
  • Honest account of coverage: with the change reverted only the last case fails (the stale-result-after-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.
  • Not tested: more than 1024 distinct classes (cap), and getBeans(Type, Set<Annotation>) with an empty set, which does not use the shortcut.

Semantics and risks

  • Result for a given (class, no qualifiers) is the same set the resolver already returns; no new ordering, no change to ambiguity resolution.
  • Self-invocation / interceptor behaviour is not touched (this is lookup only; unlike [7.0] Reduce invocation overhead for intercepted bean methods weld/core#3545, nothing here concerns interceptor chains).
  • Pre-existing, not changed here: AbstractTypeSafeBeanResolver.clear() has a small window between invalidation and publishing that also exists for the existing cache; addBean does not clear the resolver. Fixing it would be a separate change.
  • Alternative: RESTEasy could cache its own lookup. This PR helps every caller of 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 the
results 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

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>
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