Skip to content

Commit 5027c98

Browse files
author
mhyeon-lee
committed
[DATACMNS-1706] PreferredConstructor#isConstructorParameter performance test
1 parent 302fa63 commit 5027c98

File tree

1 file changed

+14
-33
lines changed

1 file changed

+14
-33
lines changed

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

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
import java.lang.annotation.Annotation;
2121
import java.lang.reflect.Constructor;
2222
import java.util.Arrays;
23-
import java.util.HashMap;
2423
import java.util.List;
2524
import java.util.Map;
26-
import java.util.concurrent.locks.Lock;
27-
import java.util.concurrent.locks.ReentrantReadWriteLock;
25+
import java.util.concurrent.ConcurrentHashMap;
2826

2927
import org.springframework.beans.factory.annotation.Value;
3028
import org.springframework.data.annotation.PersistenceConstructor;
@@ -43,16 +41,13 @@
4341
* @author Thomas Darimont
4442
* @author Christoph Strobl
4543
* @author Mark Paluch
44+
* @author Myeonghyeon Lee
4645
*/
4746
public class PreferredConstructor<T, P extends PersistentProperty<P>> {
4847

4948
private final Constructor<T> constructor;
5049
private final List<Parameter<Object, P>> parameters;
51-
private final Map<PersistentProperty<?>, Boolean> isPropertyParameterCache = new HashMap<>();
52-
53-
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
54-
private final Lock read = lock.readLock();
55-
private final Lock write = lock.writeLock();
50+
private final Map<PersistentProperty<?>, Boolean> isPropertyParameterCache = new ConcurrentHashMap<>();
5651

5752
/**
5853
* Creates a new {@link PreferredConstructor} from the given {@link Constructor} and {@link Parameter}s.
@@ -129,36 +124,22 @@ public boolean isConstructorParameter(PersistentProperty<?> property) {
129124

130125
Assert.notNull(property, "Property must not be null!");
131126

132-
try {
133-
134-
read.lock();
135-
Boolean cached = isPropertyParameterCache.get(property);
136-
137-
if (cached != null) {
138-
return cached;
139-
}
127+
Boolean cached = isPropertyParameterCache.get(property);
140128

141-
} finally {
142-
read.unlock();
129+
if (cached != null) {
130+
return cached;
143131
}
144132

145-
try {
146-
147-
write.lock();
148-
149-
for (Parameter<?, P> parameter : parameters) {
150-
if (parameter.maps(property)) {
151-
isPropertyParameterCache.put(property, true);
152-
return true;
153-
}
133+
boolean result = false;
134+
for (Parameter<?, P> parameter : parameters) {
135+
if (parameter.maps(property)) {
136+
isPropertyParameterCache.put(property, true);
137+
result = true;
138+
break;
154139
}
155-
156-
isPropertyParameterCache.put(property, false);
157-
return false;
158-
159-
} finally {
160-
write.unlock();
161140
}
141+
142+
return result;
162143
}
163144

164145
/**

0 commit comments

Comments
 (0)