Skip to content

Commit 6fe2c3a

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 eabcfe2 commit 6fe2c3a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/main/java/org/springframework/data/mapping/PreferredConstructor.java

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

145+
isPropertyParameterCache.put(property, result);
146+
142147
return result;
143148
}
144149

0 commit comments

Comments
 (0)