Browse Source

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.
2.4.x
Mark Paluch 5 years ago
parent
commit
5fc665ae29
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 7
      src/main/java/org/springframework/data/mapping/PreferredConstructor.java

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

@ -115,6 +115,10 @@ public class PreferredConstructor<T, P extends PersistentProperty<P>> { @@ -115,6 +115,10 @@ public class PreferredConstructor<T, P extends PersistentProperty<P>> {
/**
* Returns whether the given {@link PersistentProperty} is referenced in a constructor argument of the
* {@link PersistentEntity} backing this {@link PreferredConstructor}.
* <p>
* 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<T, P extends PersistentProperty<P>> { @@ -132,12 +136,13 @@ public class PreferredConstructor<T, P extends PersistentProperty<P>> {
boolean result = false;
for (Parameter<?, P> parameter : parameters) {
if (parameter.maps(property)) {
isPropertyParameterCache.put(property, true);
result = true;
break;
}
}
isPropertyParameterCache.put(property, result);
return result;
}

Loading…
Cancel
Save