`ReflectionFallbackPersistentPropertyAccessorFactory` re-evaluates `isSupported(…)` on every `getPropertyAccessor(…)` call in native images

Aperta
#3,575 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@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
Stack tecnologico
java, spring

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

type: regression
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.isSupported in 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 isSupported or ClassNotFoundException.
  • 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

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di spring-projects/spring-data-commons

Tutte le issue di spring-projects/spring-data-commons

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.