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

オープン
#3,575 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

@mp911de がすでに取り組んでいます。

2026年11月9日 から。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
java, spring

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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);
        }
    }
}
主要言語
Java
スター
842
フォーク
745
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

spring-projects/spring-data-commons のほかの issue

spring-projects/spring-data-commons の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。