2019-03-16 03:25:32 +00:00
|
|
|
From e77fa35159c7442a3db6d0d2720d79251f546ab5 Mon Sep 17 00:00:00 2001
|
2018-09-16 04:01:48 +00:00
|
|
|
From: Aikar <aikar@aikar.co>
|
|
|
|
Date: Sun, 16 Sep 2018 00:00:16 -0400
|
2018-09-20 15:43:32 +00:00
|
|
|
Subject: [PATCH] Optimize and Fix ExpiringMap Issues
|
2018-09-16 04:01:48 +00:00
|
|
|
|
2018-09-20 15:43:32 +00:00
|
|
|
computeIfAbsent would leak as the entry was never
|
|
|
|
registered into the ttl map, as well as some other
|
|
|
|
methods were at risk, so they were added.
|
2018-09-16 04:01:48 +00:00
|
|
|
|
2018-09-20 15:43:32 +00:00
|
|
|
This also synchronizes all access make the map thread safe.
|
|
|
|
|
|
|
|
This also redesigns cleaning to not run on every
|
|
|
|
manipulation, and instead to run clean
|
|
|
|
once per tick per active expiring map.
|
2018-09-16 04:01:48 +00:00
|
|
|
|
2018-11-04 01:23:08 +00:00
|
|
|
diff --git a/src/main/java/net/minecraft/server/ChunkGeneratorAbstract.java b/src/main/java/net/minecraft/server/ChunkGeneratorAbstract.java
|
2019-03-16 03:25:32 +00:00
|
|
|
index a2376844..835a2aae 100644
|
2018-11-04 01:23:08 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/ChunkGeneratorAbstract.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/ChunkGeneratorAbstract.java
|
2019-01-01 03:15:55 +00:00
|
|
|
@@ -142,13 +142,13 @@ public abstract class ChunkGeneratorAbstract<C extends GeneratorSettings> implem
|
2018-11-04 01:23:08 +00:00
|
|
|
|
|
|
|
public Long2ObjectMap<StructureStart> getStructureStartCache(StructureGenerator<? extends WorldGenFeatureConfiguration> structuregenerator) {
|
2019-01-01 03:15:55 +00:00
|
|
|
return (Long2ObjectMap) this.d.computeIfAbsent(structuregenerator, (structuregenerator1) -> {
|
|
|
|
- return Long2ObjectMaps.synchronize(new ExpiringMap<>(8192, 10000));
|
|
|
|
+ return new ExpiringMap<>(8192, 10000); // Paper - already synchronized
|
2018-11-04 01:23:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public Long2ObjectMap<LongSet> getStructureCache(StructureGenerator<? extends WorldGenFeatureConfiguration> structuregenerator) {
|
2019-01-01 03:15:55 +00:00
|
|
|
return (Long2ObjectMap) this.e.computeIfAbsent(structuregenerator, (structuregenerator1) -> {
|
|
|
|
- return Long2ObjectMaps.synchronize(new ExpiringMap<>(8192, 10000));
|
|
|
|
+ return new ExpiringMap<>(8192, 10000); // Paper - already synchronized
|
2018-11-04 01:23:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-16 04:01:48 +00:00
|
|
|
diff --git a/src/main/java/net/minecraft/server/ExpiringMap.java b/src/main/java/net/minecraft/server/ExpiringMap.java
|
2019-03-16 03:25:32 +00:00
|
|
|
index bd363647..bf609513 100644
|
2018-09-16 04:01:48 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/ExpiringMap.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/ExpiringMap.java
|
2019-01-01 03:15:55 +00:00
|
|
|
@@ -2,87 +2,211 @@ package net.minecraft.server;
|
2018-09-20 15:43:32 +00:00
|
|
|
|
|
|
|
import it.unimi.dsi.fastutil.longs.Long2LongLinkedOpenHashMap;
|
|
|
|
import it.unimi.dsi.fastutil.longs.Long2LongMap;
|
|
|
|
+import it.unimi.dsi.fastutil.longs.Long2ObjectMaps;
|
|
|
|
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
|
|
|
-import it.unimi.dsi.fastutil.longs.Long2LongMap.Entry;
|
2018-12-17 05:18:06 +00:00
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectCollection;
|
2018-09-16 04:01:48 +00:00
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectIterator;
|
|
|
|
import java.util.Map;
|
2018-09-16 05:04:40 +00:00
|
|
|
+import java.util.function.BiFunction;
|
|
|
|
+import java.util.function.Function;
|
2018-11-04 01:23:08 +00:00
|
|
|
import java.util.function.LongFunction;
|
2018-09-16 04:01:48 +00:00
|
|
|
|
2018-09-20 15:43:32 +00:00
|
|
|
-public class ExpiringMap<T> extends Long2ObjectOpenHashMap<T> {
|
2018-11-04 01:23:08 +00:00
|
|
|
-
|
2018-09-20 15:43:32 +00:00
|
|
|
+public class ExpiringMap<T> extends Long2ObjectMaps.SynchronizedMap<T> { // paper - synchronize accesss
|
2018-09-16 04:01:48 +00:00
|
|
|
private final int a;
|
|
|
|
- private final Long2LongMap b = new Long2LongLinkedOpenHashMap();
|
|
|
|
+ private final Long2LongMap ttl = new Long2LongLinkedOpenHashMap(); // Paper
|
2018-10-07 18:58:53 +00:00
|
|
|
+ private static final boolean DEBUG_EXPIRING_MAP = Boolean.getBoolean("debug.expiringmap");
|
2018-09-16 04:01:48 +00:00
|
|
|
|
|
|
|
public ExpiringMap(int i, int j) {
|
2018-09-20 15:43:32 +00:00
|
|
|
- super(i);
|
|
|
|
+ super(new Long2ObjectOpenHashMap<>(i)); // Paper
|
2018-09-16 04:01:48 +00:00
|
|
|
this.a = j;
|
|
|
|
}
|
|
|
|
|
2018-09-22 01:43:50 +00:00
|
|
|
+ // Paper start
|
|
|
|
+ private void setAccess(long i) { a(i); } // Paper - OBFHELPER
|
|
|
|
private void a(long i) {
|
2018-12-17 05:18:06 +00:00
|
|
|
- long j = SystemUtils.getMonotonicMillis();
|
2018-09-22 01:43:50 +00:00
|
|
|
+ synchronized (this.sync) {
|
|
|
|
+ long j = System.currentTimeMillis(); // Paper
|
|
|
|
+ this.ttl.put(i, j);
|
|
|
|
+ if (!registered) {
|
|
|
|
+ registered = true;
|
|
|
|
+ MinecraftServer.getServer().expiringMaps.add(this);
|
2018-11-04 01:23:08 +00:00
|
|
|
+ }
|
2018-09-16 04:01:48 +00:00
|
|
|
+ }
|
|
|
|
+ }
|
2018-09-22 01:43:50 +00:00
|
|
|
|
2018-11-04 01:23:08 +00:00
|
|
|
- this.b.put(i, j);
|
|
|
|
- // CraftBukkit start
|
2019-01-01 03:15:55 +00:00
|
|
|
- cleanup();
|
2018-09-16 04:01:48 +00:00
|
|
|
+ @Override
|
2018-09-16 05:04:40 +00:00
|
|
|
+ public T compute(long l, BiFunction<? super Long, ? super T, ? extends T> biFunction) {
|
|
|
|
+ setAccess(l);
|
|
|
|
+ return super.compute(l, biFunction);
|
2018-11-04 01:23:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- public void cleanup() {
|
2018-12-17 05:18:06 +00:00
|
|
|
- long j = SystemUtils.getMonotonicMillis();
|
2019-01-01 03:15:55 +00:00
|
|
|
- // CraftBukkit end
|
|
|
|
- ObjectIterator objectiterator = this.b.long2LongEntrySet().iterator();
|
2018-09-16 05:04:40 +00:00
|
|
|
+ @Override
|
|
|
|
+ public T putIfAbsent(long l, T t) {
|
|
|
|
+ setAccess(l);
|
|
|
|
+ return super.putIfAbsent(l, t);
|
|
|
|
+ }
|
2018-11-04 01:23:08 +00:00
|
|
|
|
|
|
|
- while (objectiterator.hasNext()) {
|
|
|
|
- Long2LongMap.Entry entry = (Long2LongMap.Entry) objectiterator.next(); // CraftBukkit - decompile error
|
2019-01-01 03:15:55 +00:00
|
|
|
- T t0 = super.get(entry.getLongKey());
|
2018-09-16 05:04:40 +00:00
|
|
|
+ @Override
|
|
|
|
+ public T computeIfPresent(long l, BiFunction<? super Long, ? super T, ? extends T> biFunction) {
|
|
|
|
+ setAccess(l);
|
|
|
|
+ return super.computeIfPresent(l, biFunction);
|
|
|
|
+ }
|
2018-11-04 01:23:08 +00:00
|
|
|
|
|
|
|
- if (j - entry.getLongValue() <= (long) this.a) {
|
|
|
|
- break;
|
|
|
|
- }
|
2018-09-16 05:04:40 +00:00
|
|
|
+ @Override
|
|
|
|
+ public T computeIfAbsent(long l, LongFunction<? extends T> longFunction) {
|
2018-09-16 04:01:48 +00:00
|
|
|
+ setAccess(l);
|
|
|
|
+ return super.computeIfAbsent(l, longFunction);
|
|
|
|
+ }
|
2018-11-04 01:23:08 +00:00
|
|
|
|
2019-01-01 03:15:55 +00:00
|
|
|
- if (t0 != null && this.a(t0)) {
|
2018-11-04 01:23:08 +00:00
|
|
|
- super.remove(entry.getLongKey());
|
|
|
|
- objectiterator.remove();
|
|
|
|
- }
|
2019-01-01 03:15:55 +00:00
|
|
|
+
|
2018-09-16 05:04:40 +00:00
|
|
|
+ @Override
|
|
|
|
+ public boolean replace(long l, T t, T v1) {
|
|
|
|
+ setAccess(l);
|
|
|
|
+ return super.replace(l, t, v1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public T replace(long l, T t) {
|
|
|
|
+ setAccess(l);
|
|
|
|
+ return super.replace(l, t);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public T putIfAbsent(Long aLong, T t) {
|
|
|
|
+ setAccess(aLong);
|
|
|
|
+ return super.putIfAbsent(aLong, t);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean replace(Long aLong, T t, T v1) {
|
|
|
|
+ setAccess(aLong);
|
|
|
|
+ return super.replace(aLong, t, v1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public T replace(Long aLong, T t) {
|
|
|
|
+ setAccess(aLong);
|
|
|
|
+ return super.replace(aLong, t);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public T computeIfAbsent(Long aLong, Function<? super Long, ? extends T> function) {
|
|
|
|
+ setAccess(aLong);
|
|
|
|
+ return super.computeIfAbsent(aLong, function);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public T computeIfPresent(Long aLong, BiFunction<? super Long, ? super T, ? extends T> biFunction) {
|
|
|
|
+ setAccess(aLong);
|
|
|
|
+ return super.computeIfPresent(aLong, biFunction);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public T compute(Long aLong, BiFunction<? super Long, ? super T, ? extends T> biFunction) {
|
|
|
|
+ setAccess(aLong);
|
2018-09-16 06:57:22 +00:00
|
|
|
+ return super.compute(aLong, biFunction);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
2018-09-22 01:43:50 +00:00
|
|
|
+ public void clear() {
|
|
|
|
+ synchronized (this.sync) {
|
|
|
|
+ ttl.clear();
|
|
|
|
+ super.clear();
|
|
|
|
}
|
2018-09-16 05:04:40 +00:00
|
|
|
+ }
|
2018-09-22 01:43:50 +00:00
|
|
|
|
2018-09-16 04:01:48 +00:00
|
|
|
+ private boolean registered = false;
|
|
|
|
+
|
|
|
|
+ // Break clean to its own method to be ticked
|
2018-09-22 01:43:50 +00:00
|
|
|
+ boolean clean() {
|
|
|
|
+ synchronized (this.sync) {
|
|
|
|
+ long now = System.currentTimeMillis();
|
|
|
|
+ ObjectIterator<Long2LongMap.Entry> objectiterator = this.ttl.long2LongEntrySet().iterator(); // Paper
|
|
|
|
+
|
|
|
|
+ while (objectiterator.hasNext()) {
|
|
|
|
+ Long2LongMap.Entry entry = objectiterator.next(); // Paper
|
|
|
|
+ T object = super.get(entry.getLongKey()); // Paper
|
|
|
|
+ if (now - entry.getLongValue() <= (long) this.a) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (object != null && this.a(object)) {
|
|
|
|
+ super.remove(entry.getLongKey());
|
|
|
|
+ objectiterator.remove();
|
|
|
|
+ }
|
2018-09-16 04:01:48 +00:00
|
|
|
+ }
|
2018-09-22 01:43:50 +00:00
|
|
|
+ int ttlSize = this.ttl.size();
|
|
|
|
+ int thisSize = this.size();
|
|
|
|
+ if (ttlSize < thisSize) {
|
2018-10-07 18:58:53 +00:00
|
|
|
+ if (DEBUG_EXPIRING_MAP) {
|
|
|
|
+ MinecraftServer.LOGGER.warn("WARNING: ExpiringMap desync (ttl:" + ttlSize + " < actual:" + thisSize + ")");
|
2018-09-16 06:57:22 +00:00
|
|
|
+ }
|
2018-09-22 01:43:50 +00:00
|
|
|
+ try {
|
|
|
|
+ for (Entry<T> entry : this.long2ObjectEntrySet()) {
|
|
|
|
+ ttl.putIfAbsent(entry.getLongKey(), now);
|
|
|
|
+ }
|
2018-10-07 18:58:53 +00:00
|
|
|
+ } catch (Exception ignored) {
|
2018-09-22 01:43:50 +00:00
|
|
|
+ } // Ignore any como's
|
|
|
|
+ } else if (ttlSize > this.size()) {
|
2018-10-07 18:58:53 +00:00
|
|
|
+ if (DEBUG_EXPIRING_MAP) {
|
|
|
|
+ MinecraftServer.LOGGER.warn("WARNING: ExpiringMap desync (ttl:" + ttlSize + " > actual:" + thisSize + ")");
|
|
|
|
+ }
|
2018-09-22 01:43:50 +00:00
|
|
|
+ try {
|
|
|
|
+ this.ttl.long2LongEntrySet().removeIf(entry -> !this.containsKey(entry.getLongKey()));
|
2018-10-07 18:58:53 +00:00
|
|
|
+ } catch (Exception ignored) {
|
2018-09-22 01:43:50 +00:00
|
|
|
+ } // Ignore any como's
|
|
|
|
+ }
|
|
|
|
+ if (isEmpty()) {
|
|
|
|
+ registered = false;
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
2018-09-16 04:01:48 +00:00
|
|
|
+ }
|
|
|
|
+ // Paper end
|
|
|
|
}
|
|
|
|
|
2018-11-04 01:23:08 +00:00
|
|
|
- protected boolean a(T t0) {
|
|
|
|
+ protected boolean a(T var1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
- public T put(long i, T t0) {
|
|
|
|
+ public T put(long i, T object) {
|
|
|
|
this.a(i);
|
|
|
|
- return super.put(i, t0);
|
|
|
|
+ return (T)super.put(i, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
- public T put(Long olong, T t0) {
|
|
|
|
+ public T put(Long olong, T object) {
|
2018-12-08 10:09:55 +00:00
|
|
|
this.a(olong);
|
|
|
|
- return super.put(olong, t0);
|
2018-11-04 01:23:08 +00:00
|
|
|
+ return (T)super.put(olong, object);
|
2018-09-16 07:21:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public T get(long i) {
|
|
|
|
- this.a(i);
|
2018-11-04 01:23:08 +00:00
|
|
|
- return super.get(i);
|
2018-09-20 15:43:32 +00:00
|
|
|
+ // Paper start - don't setAccess unless a hit
|
|
|
|
+ T t = super.get(i);
|
|
|
|
+ if (t != null) {
|
|
|
|
+ this.setAccess(i);
|
|
|
|
+ }
|
|
|
|
+ return t;
|
|
|
|
+ // Paper end
|
2018-09-16 07:21:52 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 01:23:08 +00:00
|
|
|
- public void putAll(Map<? extends Long, ? extends T> map) {
|
|
|
|
+ public void putAll(Map<? extends Long, ? extends T> var1) {
|
|
|
|
throw new RuntimeException("Not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
- public T remove(long i) {
|
|
|
|
+ public T remove(long var1) {
|
|
|
|
throw new RuntimeException("Not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
- public T remove(Object object) {
|
|
|
|
+ public T remove(Object var1) {
|
|
|
|
throw new RuntimeException("Not implemented");
|
|
|
|
}
|
2019-01-01 03:15:55 +00:00
|
|
|
|
|
|
|
+ // Paper start
|
|
|
|
+ /*
|
|
|
|
// CraftBukkit start
|
|
|
|
@Override
|
|
|
|
public T computeIfAbsent(long l, LongFunction<? extends T> lf) {
|
2018-12-17 05:18:06 +00:00
|
|
|
- this.b.put(l, SystemUtils.getMonotonicMillis());
|
2019-01-01 03:15:55 +00:00
|
|
|
+ this.ttl.put(l, SystemUtils.getMonotonicMillis()); // Paper
|
|
|
|
return super.computeIfAbsent(l, lf);
|
|
|
|
}
|
|
|
|
|
|
|
|
@@ -92,4 +216,5 @@ public class ExpiringMap<T> extends Long2ObjectOpenHashMap<T> {
|
|
|
|
return super.values();
|
|
|
|
}
|
|
|
|
// CraftBukkit end
|
|
|
|
+ */ // Paper end
|
2018-11-04 01:23:08 +00:00
|
|
|
}
|
2018-09-16 04:01:48 +00:00
|
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
2019-03-16 03:25:32 +00:00
|
|
|
index 38c0201a..ae271a57 100644
|
2018-09-16 04:01:48 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
2019-01-01 03:15:55 +00:00
|
|
|
@@ -150,6 +150,7 @@ public abstract class MinecraftServer implements IAsyncTaskHandler, IMojangStati
|
2018-09-16 04:01:48 +00:00
|
|
|
public int autosavePeriod;
|
|
|
|
public File bukkitDataPackFolder;
|
|
|
|
public CommandDispatcher vanillaCommandDispatcher;
|
2018-11-04 01:23:08 +00:00
|
|
|
+ public List<ExpiringMap> expiringMaps = java.util.Collections.synchronizedList(new java.util.ArrayList<>()); // Paper
|
2018-09-16 04:01:48 +00:00
|
|
|
// CraftBukkit end
|
|
|
|
// Spigot start
|
|
|
|
public static final int TPS = 20;
|
2019-01-06 17:15:21 +00:00
|
|
|
@@ -995,6 +996,7 @@ public abstract class MinecraftServer implements IAsyncTaskHandler, IMojangStati
|
2018-12-17 05:18:06 +00:00
|
|
|
this.methodProfiler.exit();
|
2018-09-16 04:01:48 +00:00
|
|
|
org.spigotmc.WatchdogThread.tick(); // Spigot
|
|
|
|
PaperLightingQueue.processQueue(startTime); // Paper
|
|
|
|
+ expiringMaps.removeIf(ExpiringMap::clean); // Paper
|
|
|
|
this.slackActivityAccountant.tickEnded(l); // Spigot
|
|
|
|
co.aikar.timings.TimingsManager.FULL_SERVER_TICK.stopTiming(); // Paper
|
|
|
|
}
|
2018-11-04 01:23:08 +00:00
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/generator/CustomChunkGenerator.java b/src/main/java/org/bukkit/craftbukkit/generator/CustomChunkGenerator.java
|
2019-03-16 03:25:32 +00:00
|
|
|
index 4cca6566..9c917370 100644
|
2018-11-04 01:23:08 +00:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/generator/CustomChunkGenerator.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/generator/CustomChunkGenerator.java
|
|
|
|
@@ -144,7 +144,7 @@ public class CustomChunkGenerator extends InternalChunkGenerator<GeneratorSettin
|
|
|
|
@Override
|
|
|
|
public Long2ObjectMap<StructureStart> getStructureStartCache(StructureGenerator<? extends WorldGenFeatureConfiguration> structuregenerator) {
|
|
|
|
return (Long2ObjectMap) this.structureStartCache.computeIfAbsent(structuregenerator, (s) -> {
|
|
|
|
- return Long2ObjectMaps.synchronize(new ExpiringMap(8192, 10000));
|
|
|
|
+ return new ExpiringMap(8192, 10000); // Paper - already synchronized
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@@ -154,7 +154,7 @@ public class CustomChunkGenerator extends InternalChunkGenerator<GeneratorSettin
|
|
|
|
@Override
|
|
|
|
public Long2ObjectMap<LongSet> getStructureCache(StructureGenerator<? extends WorldGenFeatureConfiguration> structuregenerator) {
|
|
|
|
return (Long2ObjectMap) this.structureCache.computeIfAbsent(structuregenerator, (s) -> {
|
|
|
|
- return Long2ObjectMaps.synchronize(new ExpiringMap(8192, 10000));
|
|
|
|
+ return new ExpiringMap(8192, 10000); // Paper - already synchronized
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-16 04:01:48 +00:00
|
|
|
--
|
2019-01-01 03:15:55 +00:00
|
|
|
2.20.1
|
2018-09-16 04:01:48 +00:00
|
|
|
|