Skip to content

Commit 9d482b0

Browse files
mhyeon-leemp911de
mhyeon-lee
authored andcommitted
DATACMNS-1706 - Improve locking in PreferredConstructor#isConstructorParameter.
Switch property parameter cache from HashMap and external locks to ConcurrentHashMap to improve multi-threaded locking behavior during isConstructorParameter(…) initialization. Original pull request: #437.
1 parent 7db7e1b commit 9d482b0

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
@@ -18,11 +18,9 @@
1818
import java.lang.annotation.Annotation;
1919
import java.lang.reflect.Constructor;
2020
import java.util.Arrays;
21-
import java.util.HashMap;
2221
import java.util.List;
2322
import java.util.Map;
24-
import java.util.concurrent.locks.Lock;
25-
import java.util.concurrent.locks.ReentrantReadWriteLock;
23+
import java.util.concurrent.ConcurrentHashMap;
2624

2725
import org.springframework.beans.factory.annotation.Value;
2826
import org.springframework.data.annotation.PersistenceConstructor;
@@ -42,16 +40,13 @@
4240
* @author Thomas Darimont
4341
* @author Christoph Strobl
4442
* @author Mark Paluch
43+
* @author Myeonghyeon Lee
4544
*/
4645
public class PreferredConstructor<T, P extends PersistentProperty<P>> {
4746

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

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

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

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

140-
} finally {
141-
read.unlock();
128+
if (cached != null) {
129+
return cached;
142130
}
143131

144-
try {
145-
146-
write.lock();
147-
148-
for (Parameter<?, P> parameter : parameters) {
149-
if (parameter.maps(property)) {
150-
isPropertyParameterCache.put(property, true);
151-
return true;
152-
}
132+
boolean result = false;
133+
for (Parameter<?, P> parameter : parameters) {
134+
if (parameter.maps(property)) {
135+
isPropertyParameterCache.put(property, true);
136+
result = true;
137+
break;
153138
}
154-
155-
isPropertyParameterCache.put(property, false);
156-
return false;
157-
158-
} finally {
159-
write.unlock();
160139
}
140+
141+
return result;
161142
}
162143

163144
/**

0 commit comments

Comments
 (0)