Paper/patches/server/0228-Optimize-IntIdentityHashBiMiap-nextId.patch

66 lines
2.6 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Andrew Steinborn <git@steinborn.me>
Date: Mon, 23 Jul 2018 13:08:19 -0400
2021-06-12 23:31:35 +02:00
Subject: [PATCH] Optimize IntIdentityHashBiMiap#nextId()
Optimizes CrudeIncrementalIntIdentityHashBiMap#nextId()
2021-06-11 14:02:28 +02:00
This is a frequent hotspot for world loading/saving.
diff --git a/src/main/java/net/minecraft/util/CrudeIncrementalIntIdentityHashBiMap.java b/src/main/java/net/minecraft/util/CrudeIncrementalIntIdentityHashBiMap.java
2021-11-23 16:04:41 +01:00
index dc7528b41aa9a55807a2b3e33d5668e1be681e79..a002218caed08bf4a4072d934e1094c22ee35534 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/util/CrudeIncrementalIntIdentityHashBiMap.java
+++ b/src/main/java/net/minecraft/util/CrudeIncrementalIntIdentityHashBiMap.java
2021-11-23 16:04:41 +01:00
@@ -16,11 +16,13 @@ public class CrudeIncrementalIntIdentityHashBiMap<K> implements IdMap<K> {
2021-06-11 14:02:28 +02:00
private K[] byId;
private int nextId;
private int size;
+ private java.util.BitSet usedIds; // Paper
2021-11-23 16:04:41 +01:00
private CrudeIncrementalIntIdentityHashBiMap(int size) {
2021-06-12 23:31:35 +02:00
this.keys = (K[])(new Object[size]);
2021-06-11 14:02:28 +02:00
this.values = new int[size];
2021-06-12 23:31:35 +02:00
this.byId = (K[])(new Object[size]);
2021-06-11 14:02:28 +02:00
+ this.usedIds = new java.util.BitSet(); // Paper
}
2021-11-23 16:04:41 +01:00
public static <A> CrudeIncrementalIntIdentityHashBiMap<A> create(int expectedSize) {
@@ -57,9 +59,13 @@ public class CrudeIncrementalIntIdentityHashBiMap<K> implements IdMap<K> {
2021-06-11 14:02:28 +02:00
}
private int nextId() {
2021-06-12 23:31:35 +02:00
+ /* // Paper start
while(this.nextId < this.byId.length && this.byId[this.nextId] != null) {
++this.nextId;
2021-06-11 14:02:28 +02:00
}
+ */
+ this.nextId = this.usedIds.nextClearBit(0);
+ // Paper end
return this.nextId;
}
2021-11-23 16:04:41 +01:00
@@ -80,6 +86,7 @@ public class CrudeIncrementalIntIdentityHashBiMap<K> implements IdMap<K> {
this.byId = crudeIncrementalIntIdentityHashBiMap.byId;
this.nextId = crudeIncrementalIntIdentityHashBiMap.nextId;
this.size = crudeIncrementalIntIdentityHashBiMap.size;
2021-06-11 14:02:28 +02:00
+ this.usedIds.clear(); // Paper
2021-11-23 16:04:41 +01:00
}
2021-06-11 14:02:28 +02:00
2021-11-23 16:04:41 +01:00
public void addMapping(K value, int id) {
@@ -96,6 +103,7 @@ public class CrudeIncrementalIntIdentityHashBiMap<K> implements IdMap<K> {
2021-06-11 14:02:28 +02:00
this.keys[k] = value;
this.values[k] = id;
this.byId[id] = value;
+ this.usedIds.set(id); // Paper
++this.size;
if (id == this.nextId) {
++this.nextId;
2021-11-23 16:04:41 +01:00
@@ -157,6 +165,7 @@ public class CrudeIncrementalIntIdentityHashBiMap<K> implements IdMap<K> {
2021-06-12 23:31:35 +02:00
Arrays.fill(this.byId, (Object)null);
2021-06-11 14:02:28 +02:00
this.nextId = 0;
this.size = 0;
+ this.usedIds.clear(); // Paper
}
2021-11-23 16:04:41 +01:00
@Override