`ReflectionFallbackPersistentPropertyAccessorFactory` re-evaluates `isSupported(…)` on every `getPropertyAccessor(…)` call in native images
@mp911de がすでに取り組んでいます。
2026年11月9日 から。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 領域
- backend, performance
調査の方向性
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 の本文から書いたものです。
説明
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);
}
}
}
- 主要言語
- Java
- スター
- 842
- フォーク
- 745
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
spring-projects/spring-data-commons のほかの issue
-
type: task
難易度 3/5 1〜2日 初心者へのやさしさ 30/100
-
type: task
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
-
type: task
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
-
type: enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
spring-projects/spring-data-commons#3548 · コメント 1 件 ·
-
Avoid allocations when rewriting queries without value expressions再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンtype: enhancement
難易度 3/5 1〜2日 初心者へのやさしさ 64/100
spring-projects/spring-data-commons#3536 · コメント 1 件 ·
spring-projects/spring-data-commons の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
メンテナーはふだん 1 日以内に返信
-
Type: bug
難易度 1/5 1時間未満 初心者へのやさしさ 83/100
apache/parquet-java#3941 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
objectionary/sabj25#47 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
jakartaee/expression-language#501 ·
メンテナーはふだん 7 日以内に返信