Skip to content
Permalink
Browse files

Current Chunk for Entity and Block Entities, counts by entity type

This enables us a fast reference to the entities current chunk instead
of having to look it up by hashmap lookups.

We also store counts by type to further enable other performance optimizations in later patches.
  • Loading branch information...
aikar committed Jul 4, 2018
1 parent f534210 commit 18c3716c49d53dbb02b1a9934ef6d2e017c51f70
Showing 323 changed files with 1,258 additions and 878 deletions.
@@ -1,4 +1,4 @@
From 64f62bc4e2c8b60a6679fbcc608d1a5ee8fe7944 Mon Sep 17 00:00:00 2001
From 0644771327501a754ae14c834e50fc97f6417085 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Mon, 29 Feb 2016 18:48:17 -0600
Subject: [PATCH] Timings v2
@@ -2296,6 +2296,47 @@ index 00000000..5edaba12
+ super.stopTiming();
+ }
+}
diff --git a/src/main/java/co/aikar/util/Counter.java b/src/main/java/co/aikar/util/Counter.java
new file mode 100644
index 00000000..23ac07f2
--- /dev/null
+++ b/src/main/java/co/aikar/util/Counter.java
@@ -0,0 +1,35 @@
+package co.aikar.util;
+
+import com.google.common.collect.ForwardingMap;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Counter <T> extends ForwardingMap<T, Long> {
+ private final Map<T, Long> counts = new HashMap<>();
+
+ public long decrement(T key) {
+ return increment(key, -1);
+ }
+ public long increment(T key) {
+ return increment(key, 1);
+ }
+ public long decrement(T key, long amount) {
+ return decrement(key, -amount);
+ }
+ public long increment(T key, long amount) {
+ Long count = this.getCount(key);
+ count += amount;
+ this.counts.put(key, count);
+ return count;
+ }
+
+ public long getCount(T key) {
+ return this.counts.getOrDefault(key, 0L);
+ }
+
+ @Override
+ protected Map<T, Long> delegate() {
+ return this.counts;
+ }
+}
diff --git a/src/main/java/co/aikar/util/JSONUtil.java b/src/main/java/co/aikar/util/JSONUtil.java
new file mode 100644
index 00000000..96274975
@@ -3032,7 +3073,7 @@ index 00000000..fd452bce
+ }
+}
diff --git a/src/main/java/org/bukkit/command/Command.java b/src/main/java/org/bukkit/command/Command.java
index 08a9739f..347d2189 100644
index 7ca5be84..86c78098 100644
--- a/src/main/java/org/bukkit/command/Command.java
+++ b/src/main/java/org/bukkit/command/Command.java
@@ -32,7 +32,8 @@ public abstract class Command {
@@ -3610,7 +3651,7 @@ index 80c6a72e..759c4617 100644
eventSet.add(new TimedRegisteredListener(listener, executor, eh.priority(), plugin, eh.ignoreCancelled()));
} else {
diff --git a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
index d7d5ac7b..a657fce5 100644
index e43db9da..ca9c7796 100644
--- a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
@@ -23,7 +23,8 @@ import org.bukkit.plugin.PluginDescriptionFile;
@@ -3866,5 +3907,5 @@ index 8d982974..7e89b97b 100644
- }
}
--
2.15.1.windows.2
2.18.0

@@ -0,0 +1,34 @@
From 99dd52878fbaede269af3c5cf38c179688506099 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Wed, 4 Jul 2018 02:25:48 -0400
Subject: [PATCH] Entity#getChunk API

Get the chunk the entity is currently registered to

diff --git a/src/main/java/org/bukkit/entity/Entity.java b/src/main/java/org/bukkit/entity/Entity.java
index 3ae4de7a..e2a2b78c 100644
--- a/src/main/java/org/bukkit/entity/Entity.java
+++ b/src/main/java/org/bukkit/entity/Entity.java
@@ -1,5 +1,6 @@
package org.bukkit.entity;

+import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.EntityEffect;
import org.bukkit.Nameable;
@@ -514,5 +515,12 @@ public interface Entity extends Metadatable, CommandSender, Nameable {
* @return True if entity spawned from a mob spawner
*/
boolean fromMobSpawner();
+
+ /**
+ * Gets the latest chunk an entity is currently or was in.
+ *
+ * @return The current, or most recent chunk if the entity is invalid (which may load the chunk)
+ */
+ Chunk getChunk();
// Paper end
}
--
2.18.0

@@ -0,0 +1,101 @@
From 471ff10805ee8ef1b93d19bd05a91b9f372417c9 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Wed, 4 Jul 2018 01:40:13 -0400
Subject: [PATCH] Add MinecraftKey Information to Objects

Stores the reference to the objects respective MinecraftKey

diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
index ed39b122e..3a8902bf1 100644
--- a/src/main/java/net/minecraft/server/Entity.java
+++ b/src/main/java/net/minecraft/server/Entity.java
@@ -41,7 +41,7 @@ import org.bukkit.event.entity.EntityPortalEvent;
import org.bukkit.plugin.PluginManager;
// CraftBukkit end

-public abstract class Entity implements ICommandListener {
+public abstract class Entity implements ICommandListener, KeyedObject { // Paper

// CraftBukkit start
private static final int CURRENT_LEVEL = 2;
@@ -1702,11 +1702,23 @@ public abstract class Entity implements ICommandListener {
return true;
}

+ // Paper start
+ public final MinecraftKey entityKey = EntityTypes.getKey(this);
+ public final String entityKeyString = entityKey != null ? entityKey.toString() : null;
+
+ @Override
+ public MinecraftKey getMinecraftKey() {
+ return entityKey;
+ }
+
+ @Override
+ public String getMinecraftKeyString() {
+ return entityKeyString;
+ }
@Nullable
public final String getSaveID() {
- MinecraftKey minecraftkey = EntityTypes.a(this);
-
- return minecraftkey == null ? null : minecraftkey.toString();
+ return entityKeyString;
+ // Paper end
}

protected abstract void a(NBTTagCompound nbttagcompound);
diff --git a/src/main/java/net/minecraft/server/KeyedObject.java b/src/main/java/net/minecraft/server/KeyedObject.java
new file mode 100644
index 000000000..61c2b993c
--- /dev/null
+++ b/src/main/java/net/minecraft/server/KeyedObject.java
@@ -0,0 +1,8 @@
+package net.minecraft.server;
+
+public interface KeyedObject {
+ MinecraftKey getMinecraftKey();
+ default String getMinecraftKeyString() {
+ return getMinecraftKey().toString();
+ }
+}
diff --git a/src/main/java/net/minecraft/server/TileEntity.java b/src/main/java/net/minecraft/server/TileEntity.java
index 5a5a588e7..672ba3134 100644
--- a/src/main/java/net/minecraft/server/TileEntity.java
+++ b/src/main/java/net/minecraft/server/TileEntity.java
@@ -7,7 +7,7 @@ import org.apache.logging.log4j.Logger;
import org.spigotmc.CustomTimingsHandler; // Spigot
import org.bukkit.inventory.InventoryHolder; // CraftBukkit

-public abstract class TileEntity {
+public abstract class TileEntity implements KeyedObject {

public CustomTimingsHandler tickTimer = org.bukkit.craftbukkit.SpigotTimings.getTileEntityTimings(this); // Spigot
private static final Logger a = LogManager.getLogger();
@@ -27,8 +27,21 @@ public abstract class TileEntity {
TileEntity.f.a(new MinecraftKey(s), oclass);
}

- @Nullable
- public static MinecraftKey a(Class<? extends TileEntity> oclass) {
+ // Paper start
+ public final MinecraftKey tileEntityKey = getKey(this.getClass());
+ public final String tileEntityKeyString = tileEntityKey != null ? tileEntityKey.toString() : null;
+
+ @Override
+ public MinecraftKey getMinecraftKey() {
+ return tileEntityKey;
+ }
+
+ @Override
+ public String getMinecraftKeyString() {
+ return tileEntityKeyString;
+ }
+ @Nullable public static MinecraftKey getKey(Class<? extends TileEntity> oclass) { return a(oclass); } // Paper - OBFHELPER
+ @Nullable public static MinecraftKey a(Class<? extends TileEntity> oclass) {
return (MinecraftKey) TileEntity.f.b(oclass);
}

--
2.18.0

@@ -0,0 +1,137 @@
From b55307c8ea1237c0893145e14f48e2e98a68fc5b Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Wed, 4 Jul 2018 02:10:36 -0400
Subject: [PATCH] Store reference to current Chunk for Entity and Block
Entities

This enables us a fast reference to the entities current chunk instead
of having to look it up by hashmap lookups.

diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
index 4bbebb25a..b40e60942 100644
--- a/src/main/java/net/minecraft/server/Chunk.java
+++ b/src/main/java/net/minecraft/server/Chunk.java
@@ -33,6 +33,30 @@ public class Chunk {
private boolean m;
public final Map<BlockPosition, TileEntity> tileEntities;
public final List<Entity>[] entitySlices; // Spigot
+ // Paper start
+ private class TileEntityHashMap extends java.util.HashMap<BlockPosition, TileEntity> {
+ @Override
+ public TileEntity put(BlockPosition key, TileEntity value) {
+ TileEntity replaced = super.put(key, value);
+ if (replaced != null) {
+ replaced.setCurrentChunk(null);
+ }
+ if (value != null) {
+ value.setCurrentChunk(Chunk.this);
+ }
+ return replaced;
+ }
+
+ @Override
+ public TileEntity remove(Object key) {
+ TileEntity removed = super.remove(key);
+ if (removed != null) {
+ removed.setCurrentChunk(null);
+ }
+ return removed;
+ }
+ }
+ // Paper end
private boolean done;
private boolean lit;
private boolean r;
@@ -80,7 +104,7 @@ public class Chunk {
this.g = new byte[256];
this.h = new int[256];
this.i = new boolean[256];
- this.tileEntities = Maps.newHashMap();
+ this.tileEntities = new TileEntityHashMap(); // Paper
this.x = 4096;
this.y = Queues.newConcurrentLinkedQueue();
this.entitySlices = (List[]) (new List[16]); // Spigot
@@ -624,6 +648,9 @@ public class Chunk {
this.entityCount.adjustOrPutValue( creatureType.a(), 1, 1 );
}
}
+ // Paper start
+ entity.setCurrentChunk(this);
+ // Paper end
// Spigot end
}

@@ -656,6 +683,9 @@ public class Chunk {
this.entityCount.adjustValue( creatureType.a(), -1 );
}
}
+ // Paper start
+ entity.setCurrentChunk(null);
+ // Paper end
// Spigot end
}

diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
index 3a8902bf1..3829c8c40 100644
--- a/src/main/java/net/minecraft/server/Entity.java
+++ b/src/main/java/net/minecraft/server/Entity.java
@@ -1703,6 +1703,13 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper
}

// Paper start
+ private java.lang.ref.WeakReference<Chunk> currentChunk = null;
+ public Chunk getCurrentChunk() {
+ return currentChunk != null ? currentChunk.get() : null;
+ }
+ public void setCurrentChunk(Chunk chunk) {
+ this.currentChunk = chunk != null ? new java.lang.ref.WeakReference<>(chunk) : null;
+ }
public final MinecraftKey entityKey = EntityTypes.getKey(this);
public final String entityKeyString = entityKey != null ? entityKey.toString() : null;

diff --git a/src/main/java/net/minecraft/server/TileEntity.java b/src/main/java/net/minecraft/server/TileEntity.java
index 672ba3134..88cd5734d 100644
--- a/src/main/java/net/minecraft/server/TileEntity.java
+++ b/src/main/java/net/minecraft/server/TileEntity.java
@@ -28,6 +28,13 @@ public abstract class TileEntity implements KeyedObject {
}

// Paper start
+ private java.lang.ref.WeakReference<Chunk> currentChunk = null;
+ public Chunk getCurrentChunk() {
+ return currentChunk != null ? currentChunk.get() : null;
+ }
+ public void setCurrentChunk(Chunk chunk) {
+ this.currentChunk = chunk != null ? new java.lang.ref.WeakReference<>(chunk) : null;
+ }
public final MinecraftKey tileEntityKey = getKey(this.getClass());
public final String tileEntityKeyString = tileEntityKey != null ? tileEntityKey.toString() : null;

diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
index c5a194ffe..833e3111d 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
@@ -9,6 +9,7 @@ import java.util.UUID;

import net.minecraft.server.*;

+import org.bukkit.Chunk;
import org.bukkit.EntityEffect;
import org.bukkit.Location;
import org.bukkit.Server;
@@ -39,6 +40,12 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
this.entity = entity;
}

+ @Override
+ public Chunk getChunk() {
+ net.minecraft.server.Chunk currentChunk = entity.getCurrentChunk();
+ return currentChunk != null ? currentChunk.bukkitChunk : getLocation().getChunk();
+ }
+
public static CraftEntity getEntity(CraftServer server, Entity entity) {
/**
* Order is *EXTREMELY* important -- keep it right! =D
--
2.18.0

0 comments on commit 18c3716

Please sign in to comment.
You can’t perform that action at this time.