From 5fc665ae2919bfd1e9dea1d0e015329f6742cfdf Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 9 Feb 2021 11:44:16 +0100 Subject: [PATCH] =?UTF-8?q?Add=20negative-caching=20in=20PreferredConstruc?= =?UTF-8?q?tor.isConstructorParameter(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../springframework/data/mapping/PreferredConstructor.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/mapping/PreferredConstructor.java b/src/main/java/org/springframework/data/mapping/PreferredConstructor.java index 5cbd4e3c0..a779c6a2a 100644 --- a/src/main/java/org/springframework/data/mapping/PreferredConstructor.java +++ b/src/main/java/org/springframework/data/mapping/PreferredConstructor.java @@ -115,6 +115,10 @@ public class PreferredConstructor> { /** * Returns whether the given {@link PersistentProperty} is referenced in a constructor argument of the * {@link PersistentEntity} backing this {@link PreferredConstructor}. + *

+ * Results of this call are cached and reused on the next invocation. Calling this method for a + * {@link PersistentProperty} that was not yet added to its owning {@link PersistentEntity} will capture that state + * and return the same result after adding {@link PersistentProperty} to its entity. * * @param property must not be {@literal null}. * @return {@literal true} if the {@link PersistentProperty} is used in the constructor. @@ -132,12 +136,13 @@ public class PreferredConstructor> { boolean result = false; for (Parameter parameter : parameters) { if (parameter.maps(property)) { - isPropertyParameterCache.put(property, true); result = true; break; } } + isPropertyParameterCache.put(property, result); + return result; }