Skip to content

Commit 5fc665a

Browse files
committed
Add negative-caching in PreferredConstructor.isConstructorParameter(…).
We now cache the negative outcome of PreferredConstructor.isConstructorParameter(…) to avoid iterations and iterator allocations which roughly improves typical converter usage by roughly 32%. Before: Benchmark Mode Cnt Score Error Units TypicalEntityReaderBenchmark.simpleEntityGeneratedConstructorAndField thrpt 10 6848447,474 ± 554354,377 ops/s After Benchmark Mode Cnt Score Error Units TypicalEntityReaderBenchmark.simpleEntityGeneratedConstructorAndField thrpt 10 9071099,879 ± 1423166,087 ops/s Closes #2295.
1 parent 64e8ba5 commit 5fc665a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Diff for: src/main/java/org/springframework/data/mapping/PreferredConstructor.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ public boolean isExplicitlyAnnotated() {
115115
/**
116116
* Returns whether the given {@link PersistentProperty} is referenced in a constructor argument of the
117117
* {@link PersistentEntity} backing this {@link PreferredConstructor}.
118+
* <p>
119+
* Results of this call are cached and reused on the next invocation. Calling this method for a
120+
* {@link PersistentProperty} that was not yet added to its owning {@link PersistentEntity} will capture that state
121+
* and return the same result after adding {@link PersistentProperty} to its entity.
118122
*
119123
* @param property must not be {@literal null}.
120124
* @return {@literal true} if the {@link PersistentProperty} is used in the constructor.
@@ -132,12 +136,13 @@ public boolean isConstructorParameter(PersistentProperty<?> property) {
132136
boolean result = false;
133137
for (Parameter<?, P> parameter : parameters) {
134138
if (parameter.maps(property)) {
135-
isPropertyParameterCache.put(property, true);
136139
result = true;
137140
break;
138141
}
139142
}
140143

144+
isPropertyParameterCache.put(property, result);
145+
141146
return result;
142147
}
143148

0 commit comments

Comments
 (0)