Skip to content

Commit e2f743d

Browse files
authored
Fix PalettedContainer synchronization (#7663)
1 parent e7d928a commit e2f743d

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

patches/server/0663-Synchronize-PalettedContainer-instead-of-ReentrantLo.patch renamed to patches/server/0663-Synchronize-PalettedContainer-instead-of-ThreadingDe.patch

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Aikar <aikar@aikar.co>
33
Date: Fri, 29 May 2020 20:29:02 -0400
4-
Subject: [PATCH] Synchronize PalettedContainer instead of ReentrantLock
4+
Subject: [PATCH] Synchronize PalettedContainer instead of
5+
ThreadingDetector/Semaphore
56

67
Mojang has flaws in their logic about chunks being concurrently
78
wrote to. So we constantly see crashes around multiple threads writing.
89

910
Additionally, java has optimized synchronization so well that its
10-
in many times faster than trying to manage read wrote locks for low
11+
in many times faster than trying to manage read write locks for low
1112
contention situations.
1213

1314
And this is extremely a low contention situation.
1415

1516
diff --git a/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java b/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java
16-
index 45a969f97b26e328e34f3d576557999c32b954a2..83dc9550f4ed2a138d4ece2765273e555cda994a 100644
17+
index 45a969f97b26e328e34f3d576557999c32b954a2..277b75940d0424051919889b2d0045f313027234 100644
1718
--- a/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java
1819
+++ b/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java
19-
@@ -35,11 +35,11 @@ public class PalettedContainer<T> implements PaletteResize<T> {
20-
private final ThreadingDetector threadingDetector = new ThreadingDetector("PalettedContainer");
20+
@@ -32,14 +32,14 @@ public class PalettedContainer<T> implements PaletteResize<T> {
21+
private final T @org.jetbrains.annotations.Nullable [] presetValues; // Paper - Anti-Xray - Add preset values
22+
private volatile PalettedContainer.Data<T> data;
23+
private final PalettedContainer.Strategy strategy;
24+
- private final ThreadingDetector threadingDetector = new ThreadingDetector("PalettedContainer");
25+
+ // private final ThreadingDetector threadingDetector = new ThreadingDetector("PalettedContainer"); // Paper - unused
2126

2227
public void acquire() {
2328
- this.threadingDetector.checkAndLock();
@@ -30,24 +35,33 @@ index 45a969f97b26e328e34f3d576557999c32b954a2..83dc9550f4ed2a138d4ece2765273e55
3035
}
3136

3237
// Paper start - Anti-Xray - Add preset values
33-
@@ -143,7 +143,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
38+
@@ -113,7 +113,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
3439
}
35-
// Paper end
3640

37-
- public T getAndSet(int x, int y, int z, T value) {
38-
+ public synchronized T getAndSet(int x, int y, int z, T value) { // Paper - synchronize
39-
this.acquire();
41+
@Override
42+
- public int onResize(int newBits, T object) {
43+
+ public synchronized int onResize(int newBits, T object) { // Paper - synchronize
44+
PalettedContainer.Data<T> data = this.data;
4045

41-
Object var5;
42-
@@ -166,7 +166,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
43-
return this.data.palette.valueFor(j);
46+
// Paper start - Anti-Xray - Add preset values
47+
@@ -160,7 +160,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
48+
return this.getAndSet(this.strategy.getIndex(x, y, z), value);
4449
}
4550

46-
- public void set(int x, int y, int z, T value) {
47-
+ public synchronized void set(int x, int y, int z, T value) { // Paper - synchronize
48-
this.acquire();
51+
- private T getAndSet(int index, T value) {
52+
+ private synchronized T getAndSet(int index, T value) { // Paper - synchronize
53+
int i = this.data.palette.idFor(value);
54+
int j = this.data.storage.getAndSet(index, i);
55+
return this.data.palette.valueFor(j);
56+
@@ -177,7 +177,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
4957

50-
try {
58+
}
59+
60+
- private void set(int index, T value) {
61+
+ private synchronized void set(int index, T value) { // Paper - synchronize
62+
int i = this.data.palette.idFor(value);
63+
this.data.storage.set(index, i);
64+
}
5165
@@ -200,7 +200,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
5266
});
5367
}
@@ -66,15 +80,6 @@ index 45a969f97b26e328e34f3d576557999c32b954a2..83dc9550f4ed2a138d4ece2765273e55
6680
this.acquire();
6781

6882
try {
69-
@@ -236,7 +236,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
70-
71-
}
72-
73-
- private static <T> DataResult<PalettedContainer<T>> read(IdMap<T> idList, PalettedContainer.Strategy provider, PalettedContainer.DiscData<T> serialized, T defaultValue, T @org.jetbrains.annotations.Nullable [] presetValues) { // Paper - Anti-Xray - Add preset values
74-
+ private synchronized static <T> DataResult<PalettedContainer<T>> read(IdMap<T> idList, PalettedContainer.Strategy provider, PalettedContainer.DiscData<T> serialized, T defaultValue, T @org.jetbrains.annotations.Nullable [] presetValues) { // Paper - Anti-Xray - Add preset values // Paper - synchronize
75-
List<T> list = serialized.paletteEntries();
76-
int i = provider.size();
77-
int j = provider.calculateBitsForSerialization(idList, list.size());
7883
@@ -275,7 +275,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
7984
return DataResult.success(new PalettedContainer<>(idList, provider, configuration, bitStorage, list, defaultValue, presetValues)); // Paper - Anti-Xray - Add preset values
8085
}

0 commit comments

Comments
 (0)