`ReflectionFallbackPersistentPropertyAccessorFactory` re-evaluates `isSupported(…)` on every `getPropertyAccessor(…)` call in native images
@mp911de ci sta già lavorando.
Dal 9/11/2026.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 48/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Ambito
- backend, performance
Direzione di ricerca
Start with ReflectionFallbackPersistentPropertyAccessorFactory.getPropertyAccessor(…) and trace how ClassGeneratingPropertyAccessorFactory.isSupported(…) decides whether to use generated accessors or the bean wrapper. The report provides a native image benchmark and two possible places to cache the per-entity decision. Done means the decision is made once per entity while preserving the correct accessor choice; add or update tests for supported and unsupported entities.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Description
In a GraalVM native image, AbstractMappingContext uses ReflectionFallbackPersistentPropertyAccessorFactory:
PersistentPropertyAccessorFactory accessorFactory = NativeDetector.inNativeImage()
? new ReflectionFallbackPersistentPropertyAccessorFactory()
: new ClassGeneratingPropertyAccessorFactory();
ReflectionFallbackPersistentPropertyAccessorFactory.getPropertyAccessor(…) calls
ClassGeneratingPropertyAccessorFactory.isSupported(entity) on every invocation, and nothing caches the result:
public <T> PersistentPropertyAccessor<T> getPropertyAccessor(PersistentEntity<?, ?> entity, T bean) {
if (accessorFactory.isSupported(entity)) {
return accessorFactory.getPropertyAccessor(entity, bean);
}
return BeanWrapperPropertyAccessorFactory.INSTANCE.getPropertyAccessor(entity, bean);
}
isSupported(…) builds the accessor class name (PropertyAccessorClassGenerator.generateClassName) and looks
it up with ClassUtils.loadIfPresent(…). For an entity without an AOT-generated __Accessor_… class, the lookup
fails, and isClassLoaderDefineClassAvailable, isTypeInjectable and hasUniquePropertyHashCodes run again.
That includes further ClassUtils.isPresent(…) lookups. Each failed lookup constructs a ClassNotFoundException,
and filling in its stack trace is costly in a native image.
getPropertyAccessor(…) runs on every entity read: MappingMongoConverter calls it for each document and each
nested entity. It also runs on writes and on auditing (MappingAuditableBeanWrapperFactory), so the check sits on
the hot path. Its result depends only on the entity and on the classes compiled into the image, so re-evaluating it
cannot change the outcome.
The code on main (4.2.0-SNAPSHOT) is unchanged.
Related: #2182, which disabled class generation in native images, and #3532, about late-discovered entities in
native images.
Environment
- Spring Data Commons 4.1.2, Spring Data MongoDB 5.1.2, Spring Boot 4.1.2
- GraalVM CE / Liberica NIK 25 (
native-image), Linux amd64 containers - Entities without an AOT-generated accessor class. Others, such as entities included in the AOT-processed
managed types, take the cheaper path: a successful class lookup on every call.
Observed impact
Microbenchmark in a native image: 20,000 entity.getPropertyAccessor(bean) calls plus identifier set/get, after a
warm-up pass. It compares the application's MongoMappingContext with the check decided once per entity (see the
workaround below) against an unconfigured MongoMappingContext (stock behaviour):
| Entity | Stock | Decided once per entity |
|---|---|---|
| Without generated accessor | 74 ms | 1 ms |
| With generated accessor | 25–36 ms | 27–35 ms (no measurable difference) |
Service level: a native Spring Boot service with Spring Data MongoDB was load-tested at 1–128 concurrent requests.
JFR showed:
ClassGeneratingPropertyAccessorFactory.isSupportedin 7–40% of execution samples at every concurrency above 1.- In the first burst of 128 concurrent requests after start-up, more than half of sampled allocation (about
0.9–1.1 GB of 1.7 GB).
With the check decided once per entity:
- No samples remain in
isSupportedorClassNotFoundException. - First-burst CPU drops by 33–55% at 32–128 concurrent requests.
- Steady-state throughput at 128 concurrent requests rises by up to 32%.
Expected behaviour
The accessor factory for an entity is decided once, as on the JVM, where ClassGeneratingPropertyAccessorFactory
caches the accessor constructor per entity. It is not re-evaluated on every accessor lookup.
Possible fix
Either of these:
- Cache the decision per entity in
ReflectionFallbackPersistentPropertyAccessorFactory, for example in a
ConcurrentHashMap<PersistentEntity<?, ?>, Boolean>or a map to the chosen delegate. - Make the decision once in
AbstractMappingContext.addPersistentEntity(…)and set the chosen factory on the entity.
Workaround
A MappingContextEvent listener that decides once per entity and sets a fixed factory. The event is published
after addPersistentEntity has set the default factory:
public class NativePropertyAccessorConfigurer implements ApplicationListener<MappingContextEvent<?, ?>> {
private final ClassGeneratingPropertyAccessorFactory generatedAccessors = new ClassGeneratingPropertyAccessorFactory();
private final EntityInstantiators instantiators = new EntityInstantiators();
private final PersistentPropertyAccessorFactory generated =
new InstantiationAwarePropertyAccessorFactory(generatedAccessors, instantiators);
private final PersistentPropertyAccessorFactory beanWrapper =
new InstantiationAwarePropertyAccessorFactory(BeanWrapperPropertyAccessorFactory.INSTANCE, instantiators);
@Override
public void onApplicationEvent(MappingContextEvent<?, ?> event) {
if (NativeDetector.inNativeImage()
&& event.getPersistentEntity() instanceof MutablePersistentEntity<?, ?> entity) {
entity.setPersistentPropertyAccessorFactory(generatedAccessors.isSupported(entity) ? generated : beanWrapper);
}
}
}
- Lingua principale
- Java
- Stelle
- 842
- Fork
- 745
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di spring-projects/spring-data-commons
-
Release 4.0.9 (2025.1.9)Apertatype: task
Difficoltà 3/5 1-2 giorni Idoneità per principianti 30/100
-
Release 4.1.3 (2026.0.3)Apertatype: task
Difficoltà 4/5 3-5 giorni Idoneità per principianti 25/100
-
Release 4.2.0 (2026.1.0)Apertatype: task
Difficoltà 4/5 3-5 giorni Idoneità per principianti 25/100
-
type: enhancement
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
spring-projects/spring-data-commons#3548 · 1 commento ·
-
Avoid allocations when rewriting queries without value expressionsForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Apertatype: enhancement
Difficoltà 3/5 1-2 giorni Idoneità per principianti 64/100
spring-projects/spring-data-commons#3536 · 1 commento ·
Tutte le issue di spring-projects/spring-data-commons
Issue simili
-
jules
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
open-learning-exchange/myplanet#18568 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
quarkiverse/quarkus-ironjacamar#264 · 7 commenti ·
-
[Bug]: Login shows only "Log-in failed" when Commons answers the login request with an API errorAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
commons-app/apps-android-commons#7295 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 85/100
hibernate/hibernate-reactive#4485 ·
I maintainer di solito rispondono entro 1 giorno