Skip to content

Commit d46d752

Browse files
authored
fix: we should just always clone the region in ApplyTask (#3249)
- fixes #3231
1 parent 0af0f55 commit d46d752

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/implementation/ApplyTask.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ApplyTask<F extends Filter> extends RecursiveAction implements Runnable {
2323
private static final int SHIFT_REDUCTION = 1;
2424

2525
private final CommonState<F> commonState;
26+
private final Region region;
2627
private final ApplyTask<F> before;
2728
private final int minChunkX;
2829
private final int minChunkZ;
@@ -39,7 +40,6 @@ public void run() {
3940

4041
private record CommonState<F extends Filter>(
4142
F originalFilter,
42-
Region region,
4343
ParallelQueueExtent parallelQueueExtent,
4444
ConcurrentMap<Thread, ThreadState<F>> stateCache,
4545
boolean full,
@@ -69,12 +69,12 @@ private ThreadState(SingleThreadQueueExtent queue, F filter) {
6969
) {
7070
this.commonState = new CommonState<>(
7171
filter,
72-
region.clone(), // clone only once, assuming the filter doesn't modify that clone
7372
parallelQueueExtent,
7473
new ConcurrentHashMap<>(),
7574
full,
7675
faweExceptionReasonsUsed
7776
);
77+
this.region = region.clone();
7878
this.before = null;
7979
final BlockVector3 minimumPoint = region.getMinimumPoint();
8080
this.minChunkX = minimumPoint.x() >> 4;
@@ -88,6 +88,7 @@ private ThreadState(SingleThreadQueueExtent queue, F filter) {
8888

8989
private ApplyTask(
9090
final CommonState<F> commonState,
91+
final Region region,
9192
final ApplyTask<F> before,
9293
final int minChunkX,
9394
final int maxChunkX,
@@ -96,6 +97,7 @@ private ApplyTask(
9697
final int higherShift
9798
) {
9899
this.commonState = commonState;
100+
this.region = region.clone();
99101
this.minChunkX = minChunkX;
100102
this.maxChunkX = maxChunkX;
101103
this.minChunkZ = minChunkZ;
@@ -120,14 +122,15 @@ protected void compute() {
120122
processRegion(regionX, regionZ, this.shift);
121123
continue;
122124
}
123-
if (this.shift == 0 && !this.commonState.region.containsChunk(regionX, regionZ)) {
125+
if (this.shift == 0 && !this.region.containsChunk(regionX, regionZ)) {
124126
// if shift == 0, region coords are chunk coords
125127
continue; // chunks not intersecting with the region don't need a task
126128
}
127129

128130
// creating more tasks will likely help parallelism as other threads aren't *that* busy
129131
subtask = new ApplyTask<>(
130132
this.commonState,
133+
this.region,
131134
subtask,
132135
regionX << this.shift,
133136
((regionX + 1) << this.shift) - 1,
@@ -166,7 +169,7 @@ private void processRegion(int regionX, int regionZ, int shift) {
166169
try {
167170
for (int chunkX = regionX << shift; chunkX <= ((regionX + 1) << shift) - 1; chunkX++) {
168171
for (int chunkZ = regionZ << shift; chunkZ <= ((regionZ + 1) << shift) - 1; chunkZ++) {
169-
if (!this.commonState.region.containsChunk(chunkX, chunkZ)) {
172+
if (!this.region.containsChunk(chunkX, chunkZ)) {
170173
continue; // chunks not intersecting with the region must not be processed
171174
}
172175
applyChunk(chunkX, chunkZ, state);
@@ -204,7 +207,7 @@ private void applyChunk(int chunkX, int chunkZ, ThreadState<F> state) {
204207
state.block = state.queue.apply(
205208
state.block,
206209
state.filter,
207-
this.commonState.region,
210+
this.region,
208211
chunkX,
209212
chunkZ,
210213
this.commonState.full

worldedit-core/src/main/java/com/sk89q/worldedit/regions/ConvexPolyhedralRegion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ public ConvexPolyhedralRegion(@Nullable World world) {
8989
public ConvexPolyhedralRegion(ConvexPolyhedralRegion region) {
9090
this(region.world);
9191
vertices.addAll(region.vertices);
92-
triangles.addAll(region.triangles);
92+
region.triangles.forEach(triangle -> triangles.add(triangle.clone()));
9393
vertexBacklog.addAll(region.vertexBacklog);
9494

9595
minimumPoint = region.minimumPoint;
9696
maximumPoint = region.maximumPoint;
9797
centerAccum = region.centerAccum;
98-
lastTriangle = region.lastTriangle;
98+
lastTriangle = lastTriangle == null ? null : region.lastTriangle.clone();
9999
}
100100

101101
/**

worldedit-core/src/main/java/com/sk89q/worldedit/regions/polyhedron/Triangle.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public Triangle tag(String tag) {
105105
return this;
106106
}
107107

108+
@Override
109+
public Triangle clone() {
110+
return new Triangle(vertices[0], vertices[1], vertices[2]);
111+
}
112+
108113
@Override
109114
public String toString() {
110115
return tag + "(" + this.vertices[0] + "," + this.vertices[1] + "," + this.vertices[2] + ")";

0 commit comments

Comments
 (0)