Skip to content

Commit a371d55

Browse files
committed
refactor: optimize RenderQueue by replacing copy-and-clear logic with atomic set swap
1 parent f73ca9e commit a371d55

1 file changed

Lines changed: 4 additions & 17 deletions

File tree

src/main/java/me/daoge/allaymap/render/RenderQueue.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.allaymc.api.utils.hash.HashUtils;
55
import org.allaymc.api.world.Dimension;
66

7-
import java.util.HashSet;
7+
import java.util.Collections;
88
import java.util.Map;
99
import java.util.Set;
1010
import java.util.concurrent.ConcurrentHashMap;
@@ -45,24 +45,11 @@ public void markBlockDirty(Dimension dimension, int blockX, int blockZ) {
4545

4646
/**
4747
* Get and clear all dirty chunks for a dimension.
48-
* This method is thread-safe - uses copy-and-clear to avoid losing concurrent adds.
48+
* Atomically swaps the set with a new empty one to avoid race conditions.
4949
*/
5050
public Set<Long> pollDirtyChunks(Dimension dimension) {
51-
Set<Long> chunks = this.dirtyChunks.get(dimension);
52-
if (chunks == null || chunks.isEmpty()) {
53-
return Set.of();
54-
}
55-
56-
// Copy current contents and clear atomically per-element
57-
// Using HashSet for the copy since we just need to iterate over it
58-
Set<Long> result = new HashSet<>();
59-
for (Long key : chunks) {
60-
if (chunks.remove(key)) {
61-
result.add(key);
62-
}
63-
}
64-
65-
return result;
51+
var result = this.dirtyChunks.put(dimension, ConcurrentHashMap.newKeySet());
52+
return result == null ? Collections.emptySet() : result;
6653
}
6754

6855
public void removeDimension(Dimension dimension) {

0 commit comments

Comments
 (0)