Skip to content

Commit 1174432

Browse files
committed
Some cleanups
1 parent 7edd8ec commit 1174432

File tree

3 files changed

+49
-50
lines changed

3 files changed

+49
-50
lines changed

src/main/java/com/ishland/vmp/common/chunkwatching/AreaPlayerChunkWatchingManager.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,7 @@ public class AreaPlayerChunkWatchingManager extends PlayerChunkWatchingManager {
2222
.reduce(0, Math::max) / 16.0
2323
);
2424

25-
private final AreaMap<ServerPlayerEntity> playerAreaMap = new AreaMap<>(
26-
(object, x, z) -> {
27-
if (this.addListener != null) {
28-
this.addListener.accept(object, x, z);
29-
}
30-
},
31-
(object, x, z) -> {
32-
if (this.removeListener != null) {
33-
this.removeListener.accept(object, x, z);
34-
}
35-
},
36-
true);
25+
private final AreaMap<ServerPlayerEntity> playerAreaMap;
3726
private final AreaMap<ServerPlayerEntity> generalPlayerAreaMap = new AreaMap<>();
3827
private final Object2LongOpenHashMap<ServerPlayerEntity> positions = new Object2LongOpenHashMap<>();
3928
private final PlayerChunkSendingSystem playerChunkSendingSystem;
@@ -57,8 +46,21 @@ public AreaPlayerChunkWatchingManager(Listener addListener, Listener removeListe
5746
);
5847
this.addListener = null;
5948
this.removeListener = null;
49+
this.playerAreaMap = new AreaMap<>(null, null, false);
6050
} else {
6151
this.playerChunkSendingSystem = null;
52+
this.playerAreaMap = new AreaMap<>(
53+
(object, x, z) -> {
54+
if (this.addListener != null) {
55+
this.addListener.accept(object, x, z);
56+
}
57+
},
58+
(object, x, z) -> {
59+
if (this.removeListener != null) {
60+
this.removeListener.accept(object, x, z);
61+
}
62+
},
63+
true);
6264
}
6365
}
6466

src/main/java/com/ishland/vmp/common/maps/AreaMap.java

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
import com.ishland.vmp.common.util.SimpleObjectPool;
44
import io.papermc.paper.util.MCUtil;
5-
import it.unimi.dsi.fastutil.Hash;
65
import it.unimi.dsi.fastutil.longs.Long2ObjectFunction;
76
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
87
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
98
import it.unimi.dsi.fastutil.longs.LongArrayList;
109
import it.unimi.dsi.fastutil.longs.LongComparators;
1110
import it.unimi.dsi.fastutil.longs.LongIterator;
12-
import it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap;
13-
import it.unimi.dsi.fastutil.objects.Object2LongOpenCustomHashMap;
14-
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenCustomHashSet;
1511
import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
1612
import it.unimi.dsi.fastutil.objects.Reference2LongOpenHashMap;
1713
import it.unimi.dsi.fastutil.objects.ReferenceLinkedOpenHashSet;
@@ -24,7 +20,15 @@ public class AreaMap<T> {
2420

2521
private static final Object[] EMPTY = new Object[0];
2622

27-
private final SimpleObjectPool<RawObjectLinkedOpenIdentityHashSet<T>> pooledHashSets = new SimpleObjectPool<>(unused -> new RawObjectLinkedOpenIdentityHashSet<>(), RawObjectLinkedOpenIdentityHashSet::clear, 8192);
23+
private final SimpleObjectPool<RawObjectLinkedOpenIdentityHashSet<T>> pooledHashSets =
24+
new SimpleObjectPool<>(unused -> new RawObjectLinkedOpenIdentityHashSet<>(),
25+
ReferenceLinkedOpenHashSet::clear,
26+
ts -> {
27+
ts.clear();
28+
ts.trim(4);
29+
},
30+
8192
31+
);
2832
private final Long2ObjectFunction<RawObjectLinkedOpenIdentityHashSet<T>> allocHashSet = unused -> pooledHashSets.alloc();
2933
private final Long2ObjectOpenHashMap<RawObjectLinkedOpenIdentityHashSet<T>> map = new Long2ObjectOpenHashMap<>();
3034
private final Reference2IntOpenHashMap<T> viewDistances = new Reference2IntOpenHashMap<>();
@@ -69,12 +73,7 @@ public void add(T object, int x, int z, int rawViewDistance) {
6973
set.add(MCUtil.getCoordinateKey(xx, zz));
7074
}
7175
}
72-
set.sort(LongComparators.asLongComparator(Comparator.comparingLong(l -> chebyshevDistance(x, z, MCUtil.getCoordinateX(l), MCUtil.getCoordinateZ(l)))));
73-
final LongIterator iterator = set.iterator();
74-
while (iterator.hasNext()) {
75-
final long pos = iterator.nextLong();
76-
addListener.accept(object, MCUtil.getCoordinateX(pos), MCUtil.getCoordinateZ(pos));
77-
}
76+
notifyListenersSorted(object, x, z, addListener, set);
7877
} else {
7978
for (int xx = x - viewDistance; xx <= x + viewDistance; xx++) {
8079
for (int zz = z - viewDistance; zz <= z + viewDistance; zz++) {
@@ -103,12 +102,7 @@ public void remove(T object) {
103102
set.add(MCUtil.getCoordinateKey(xx, zz));
104103
}
105104
}
106-
set.sort(LongComparators.asLongComparator(Comparator.comparingLong(l -> chebyshevDistance(x, z, MCUtil.getCoordinateX(l), MCUtil.getCoordinateZ(l)))));
107-
final LongIterator iterator = set.iterator();
108-
while (iterator.hasNext()) {
109-
final long pos = iterator.nextLong();
110-
removeListener.accept(object, MCUtil.getCoordinateX(pos), MCUtil.getCoordinateZ(pos));
111-
}
105+
notifyListenersSorted(object, x, z, removeListener, set);
112106
} else {
113107
for (int xx = x - viewDistance; xx <= x + viewDistance; xx++) {
114108
for (int zz = z - viewDistance; zz <= z + viewDistance; zz++) {
@@ -145,23 +139,18 @@ private void updateAdds(T object, int oldX, int oldZ, int oldViewDistance, int n
145139
if (this.sortListenerCalls && addListener != null) {
146140
final int length = 2 * newViewDistance + 1;
147141
LongArrayList set = new LongArrayList(length * length);
148-
for (int xx = newX - newViewDistance; xx <= newX + newViewDistance; xx ++) {
149-
for (int zz = newZ - newViewDistance; zz <= newZ + newViewDistance; zz ++) {
142+
for (int xx = newX - newViewDistance; xx <= newX + newViewDistance; xx++) {
143+
for (int zz = newZ - newViewDistance; zz <= newZ + newViewDistance; zz++) {
150144
if (!isInRange(xLower, xHigher, zLower, zHigher, xx, zz)) {
151145
add0(xx, zz, object, false);
152146
set.add(MCUtil.getCoordinateKey(xx, zz));
153147
}
154148
}
155149
}
156-
set.sort(LongComparators.asLongComparator(Comparator.comparingLong(l -> chebyshevDistance(newX, newZ, MCUtil.getCoordinateX(l), MCUtil.getCoordinateZ(l)))));
157-
final LongIterator iterator = set.iterator();
158-
while (iterator.hasNext()) {
159-
final long pos = iterator.nextLong();
160-
addListener.accept(object, MCUtil.getCoordinateX(pos), MCUtil.getCoordinateZ(pos));
161-
}
150+
notifyListenersSorted(object, newX, newZ, addListener, set);
162151
} else {
163-
for (int xx = newX - newViewDistance; xx <= newX + newViewDistance; xx ++) {
164-
for (int zz = newZ - newViewDistance; zz <= newZ + newViewDistance; zz ++) {
152+
for (int xx = newX - newViewDistance; xx <= newX + newViewDistance; xx++) {
153+
for (int zz = newZ - newViewDistance; zz <= newZ + newViewDistance; zz++) {
165154
if (!isInRange(xLower, xHigher, zLower, zHigher, xx, zz)) {
166155
add0(xx, zz, object, true);
167156
}
@@ -180,23 +169,18 @@ private void updateRemovals(T object, int oldX, int oldZ, int oldViewDistance, i
180169
if (this.sortListenerCalls && removeListener != null) {
181170
final int length = 2 * oldViewDistance + 1;
182171
LongArrayList set = new LongArrayList(length * length);
183-
for (int xx = oldX - oldViewDistance; xx <= oldX + oldViewDistance; xx ++) {
184-
for (int zz = oldZ - oldViewDistance; zz <= oldZ + oldViewDistance; zz ++) {
172+
for (int xx = oldX - oldViewDistance; xx <= oldX + oldViewDistance; xx++) {
173+
for (int zz = oldZ - oldViewDistance; zz <= oldZ + oldViewDistance; zz++) {
185174
if (!isInRange(xLower, xHigher, zLower, zHigher, xx, zz)) {
186175
remove0(xx, zz, object, false);
187176
set.add(MCUtil.getCoordinateKey(xx, zz));
188177
}
189178
}
190179
}
191-
set.sort(LongComparators.asLongComparator(Comparator.comparingLong(l -> chebyshevDistance(newX, newZ, MCUtil.getCoordinateX(l), MCUtil.getCoordinateZ(l)))));
192-
final LongIterator iterator = set.iterator();
193-
while (iterator.hasNext()) {
194-
final long pos = iterator.nextLong();
195-
removeListener.accept(object, MCUtil.getCoordinateX(pos), MCUtil.getCoordinateZ(pos));
196-
}
180+
notifyListenersSorted(object, newX, newZ, removeListener, set);
197181
} else {
198-
for (int xx = oldX - oldViewDistance; xx <= oldX + oldViewDistance; xx ++) {
199-
for (int zz = oldZ - oldViewDistance; zz <= oldZ + oldViewDistance; zz ++) {
182+
for (int xx = oldX - oldViewDistance; xx <= oldX + oldViewDistance; xx++) {
183+
for (int zz = oldZ - oldViewDistance; zz <= oldZ + oldViewDistance; zz++) {
200184
if (!isInRange(xLower, xHigher, zLower, zHigher, xx, zz)) {
201185
remove0(xx, zz, object, true);
202186
}
@@ -225,6 +209,15 @@ private void remove0(int xx, int zz, T object, boolean notifyListeners) {
225209
if (notifyListeners && this.removeListener != null) this.removeListener.accept(object, xx, zz);
226210
}
227211

212+
private void notifyListenersSorted(T object, int x, int z, Listener<T> addListener, LongArrayList set) {
213+
set.sort(LongComparators.asLongComparator(Comparator.comparingLong(l -> chebyshevDistance(x, z, MCUtil.getCoordinateX(l), MCUtil.getCoordinateZ(l)))));
214+
final LongIterator iterator = set.iterator();
215+
while (iterator.hasNext()) {
216+
final long pos = iterator.nextLong();
217+
addListener.accept(object, MCUtil.getCoordinateX(pos), MCUtil.getCoordinateZ(pos));
218+
}
219+
}
220+
228221
private static boolean isInRange(int xLower, int xHigher, int zLower, int zHigher, int x, int z) {
229222
return x >= xLower && x <= xHigher && z >= zLower && z <= zHigher;
230223
}

src/main/java/com/ishland/vmp/common/util/SimpleObjectPool.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ public class SimpleObjectPool<T> {
1010

1111
private final Function<SimpleObjectPool<T>, T> constructor;
1212
private final Consumer<T> initializer;
13+
private final Consumer<T> onRecycle;
1314
private final int size;
1415

1516
private final Object[] cachedObjects;
1617
private int allocatedCount = 0;
1718

18-
public SimpleObjectPool(Function<SimpleObjectPool<T>, T> constructor, Consumer<T> initializer, int size) {
19+
public SimpleObjectPool(Function<SimpleObjectPool<T>, T> constructor, Consumer<T> initializer, Consumer<T> onRecycle, int size) {
1920
this.constructor = Objects.requireNonNull(constructor);
2021
this.initializer = Objects.requireNonNull(initializer);
22+
this.onRecycle = Objects.requireNonNull(onRecycle);
2123
Preconditions.checkArgument(size > 0);
2224
this.cachedObjects = new Object[size];
2325
this.size = size;
@@ -50,6 +52,8 @@ public T alloc() {
5052
public void release(T object) {
5153
synchronized (this) {
5254
if (this.allocatedCount == 0) return; // pool is full
55+
56+
this.onRecycle.accept(object);
5357
this.cachedObjects[--this.allocatedCount] = object; // store the object into the pool
5458
}
5559
}

0 commit comments

Comments
 (0)