Skip to content

Commit 85e8cfd

Browse files
improve(syncmap): lock on the node instead of node.lock
1 parent a33e4ac commit 85e8cfd

File tree

1 file changed

+11
-12
lines changed
  • collections/src/main/java/space/vectrix/collections

1 file changed

+11
-12
lines changed

collections/src/main/java/space/vectrix/collections/SyncMap.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ public V computeIfAbsent(final @NotNull K key, final @NotNull Function<? super @
751751
// completing the transfer.
752752
this.helpTransfer();
753753
} else {
754-
synchronized(node.lock) {
754+
synchronized(node) {
755755
if(SyncMap.getNode(table, index) == node) {
756756
for(; ; ) {
757757
if(node.hash == hash && (node.key == key || node.key.equals(key))) {
@@ -824,7 +824,7 @@ public V computeIfPresent(final @NotNull K key, final @NotNull BiFunction<? supe
824824
// Only proceed here if the node did not exist in the immutable table.
825825
final int index;
826826
if((node = SyncMap.getNode(table, index = (length - 1) & hash)) != null) {
827-
synchronized(node.lock) {
827+
synchronized(node) {
828828
if(SyncMap.getNode(table, index) == node) {
829829
for(Node<K, V> previousNode = null; ; ) {
830830
if(node.hash == hash && (node.key == key || node.key.equals(key))) {
@@ -967,7 +967,7 @@ public V compute(final @NotNull K key, final @NotNull BiFunction<? super @NotNul
967967
// completion the transfer.
968968
this.helpTransfer();
969969
} else {
970-
synchronized(node.lock) {
970+
synchronized(node) {
971971
if(SyncMap.getNode(table, index) == node) {
972972
for(Node<K, V> previousNode = null; ; ) {
973973
if(node.hash == hash && (node.key == key || node.key.equals(key))) {
@@ -1091,7 +1091,7 @@ public V putIfAbsent(final @NotNull K key, final @NotNull V value) {
10911091
// completing the transfer.
10921092
this.helpTransfer();
10931093
} else {
1094-
synchronized(node.lock) {
1094+
synchronized(node) {
10951095
if(SyncMap.getNode(table, index) == node) {
10961096
for(; ; ) {
10971097
if(node.hash == hash && (node.key == key || node.key.equals(key))) {
@@ -1189,7 +1189,7 @@ public V putIfAbsent(final @NotNull K key, final @NotNull V value) {
11891189
// completing the transfer.
11901190
this.helpTransfer();
11911191
} else {
1192-
synchronized(node.lock) {
1192+
synchronized(node) {
11931193
if(SyncMap.getNode(table, index) == node) {
11941194
for(; ; ) {
11951195
if(node.hash == hash && (node.key == key || node.key.equals(key))) {
@@ -1242,7 +1242,7 @@ public V putIfAbsent(final @NotNull K key, final @NotNull V value) {
12421242
} else if(node.hash == SyncMap.NODE_MOVED) {
12431243
this.helpTransfer();
12441244
} else {
1245-
synchronized(node.lock) {
1245+
synchronized(node) {
12461246
if(SyncMap.getNode(table, index) == node) {
12471247
for(; ; ) {
12481248
if(node.hash == hash && (node.key == key || node.key.equals(key))) {
@@ -1291,7 +1291,7 @@ public V remove(final @NotNull Object key) {
12911291
// Only proceed here if the node did not exist in the immutable table.
12921292
final int index;
12931293
if((node = SyncMap.getNode(table, index = (length - 1) & hash)) != null) {
1294-
synchronized(node.lock) {
1294+
synchronized(node) {
12951295
if(SyncMap.getNode(table, index) == node) {
12961296
for(Node<K, V> previousNode = null; ; ) {
12971297
if(node.hash == hash && (node.key == key || node.key.equals(key))) {
@@ -1360,7 +1360,7 @@ public boolean remove(final @NotNull Object key, final @NotNull Object value) {
13601360
// Only proceed here if the node did not exist in the immutable table.
13611361
final int index;
13621362
if((node = SyncMap.getNode(table, index = (length - 1) & hash)) != null) {
1363-
synchronized(node.lock) {
1363+
synchronized(node) {
13641364
if(SyncMap.getNode(table, index) == node) {
13651365
for(Node<K, V> previousNode = null; ; ) {
13661366
if(node.hash == hash && (node.key == key || node.key.equals(key))) {
@@ -1425,7 +1425,7 @@ public V replace(final @NotNull K key, final @NotNull V value) {
14251425
// Only proceed here if the node did not exist in the immutable table.
14261426
final int index;
14271427
if((node = SyncMap.getNode(table, index = (length - 1) & hash)) != null) {
1428-
synchronized(node.lock) {
1428+
synchronized(node) {
14291429
if(SyncMap.getNode(table, index) == node) {
14301430
for(; ; ) {
14311431
if(node.hash == hash && (node.key == key || node.key.equals(key))) {
@@ -1477,7 +1477,7 @@ public boolean replace(final @NotNull K key, final @NotNull V oldValue, final @N
14771477
// Only proceed here if the node did not exist in the immutable table.
14781478
final int index;
14791479
if((node = SyncMap.getNode(table, index = (length - 1) & hash)) != null) {
1480-
synchronized(node.lock) {
1480+
synchronized(node) {
14811481
if(SyncMap.getNode(table, index) == node) {
14821482
for(; ; ) {
14831483
if(node.hash == hash && (node.key == key || node.key.equals(key))) {
@@ -1836,7 +1836,7 @@ public void clear() {
18361836
} else if(node.hash == SyncMap.NODE_MOVED) {
18371837
advance = true;
18381838
} else {
1839-
synchronized(node.lock) {
1839+
synchronized(node) {
18401840
if(SyncMap.getNode(source, i) == node) {
18411841
Node<K, V> loHead = null, loTail = null;
18421842
Node<K, V> hiHead = null, hiTail = null;
@@ -1987,7 +1987,6 @@ public void clear() {
19871987
* @param <V> the value type
19881988
*/
19891989
/* package */ static class Node<K, V> {
1990-
/* package */ final Object lock = new Object();
19911990
/* package */ final int hash;
19921991
/* package */ final K key;
19931992
/* package */ volatile ValueReference<V> reference;

0 commit comments

Comments
 (0)