aboutsummaryrefslogtreecommitdiffhomepage
path: root/CraftBukkit-Patches/0010-LongHash-Tweaks.patch
blob: dc9ee60b390c97a3aa79fa829cc537ab742d91a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
From 0759031a2d3ef71799dbb5ac0b5b7f5cff54fd4f Mon Sep 17 00:00:00 2001
From: md_5 <md_5@live.com.au>
Date: Fri, 21 Jun 2013 17:13:47 +1000
Subject: [PATCH] LongHash Tweaks

Tweaks the LongHash algorithm and provides a large array based map to look up values centered around the origin, ie the access normally seen on a Minecraft server.

diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHash.java b/src/main/java/org/bukkit/craftbukkit/util/LongHash.java
index 691cafd..9d54472 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/LongHash.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/LongHash.java
@@ -10,6 +10,6 @@ public class LongHash {
     }
 
     public static int lsw(long l) {
-        return (int) (l & 0xFFFFFFFF) + Integer.MIN_VALUE;
+        return (int) (l) + Integer.MIN_VALUE; // Spigot - remove redundant &
     }
 }
diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java b/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java
index 22c96c5..7f659b7 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java
@@ -31,6 +31,7 @@ public class LongHashSet {
     private int elements;
     private long[] values;
     private int modCount;
+    private org.spigotmc.FlatMap<Boolean> flat = new org.spigotmc.FlatMap<Boolean>(); // Spigot
 
     public LongHashSet() {
         this(INITIAL_SIZE);
@@ -56,10 +57,30 @@ public class LongHashSet {
     }
 
     public boolean contains(int msw, int lsw) {
+        // Spigot start
+        if ( elements == 0 )
+        {
+            return false;
+        }
+        if ( flat.contains( msw, lsw ) )
+        {
+            return true;
+        }
+        // Spigot end
         return contains(LongHash.toLong(msw, lsw));
     }
 
     public boolean contains(long value) {
+        // Spigot start
+        if ( elements == 0 )
+        {
+            return false;
+        }
+        if ( flat.contains( value ) )
+        {
+            return true;
+        }
+        // Spigot end
         int hash = hash(value);
         int index = (hash & 0x7FFFFFFF) % values.length;
         int offset = 1;
@@ -82,6 +103,7 @@ public class LongHashSet {
     }
 
     public boolean add(long value) {
+        flat.put( value, Boolean.TRUE ); // Spigot
         int hash = hash(value);
         int index = (hash & 0x7FFFFFFF) % values.length;
         int offset = 1;
@@ -125,10 +147,18 @@ public class LongHashSet {
     }
 
     public void remove(int msw, int lsw) {
-        remove(LongHash.toLong(msw, lsw));
+        // Spigot start
+        flat.remove(msw, lsw);
+        remove0(LongHash.toLong(msw, lsw));
     }
 
     public boolean remove(long value) {
+        flat.remove(value);
+        return remove0(value);
+    }
+
+    private boolean remove0(long value) {
+        // Spigot end
         int hash = hash(value);
         int index = (hash & 0x7FFFFFFF) % values.length;
         int offset = 1;
@@ -161,6 +191,7 @@ public class LongHashSet {
 
         freeEntries = values.length;
         modCount++;
+        flat = new org.spigotmc.FlatMap<Boolean>();
     }
 
     public long[] toArray() {
diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongObjectHashMap.java b/src/main/java/org/bukkit/craftbukkit/util/LongObjectHashMap.java
index 01861cc..2e5b436 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/LongObjectHashMap.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/LongObjectHashMap.java
@@ -28,6 +28,7 @@ public class LongObjectHashMap<V> implements Cloneable, Serializable {
     private transient V[][]    values;
     private transient int      modCount;
     private transient int      size;
+    private transient org.spigotmc.FlatMap<V> flat = new org.spigotmc.FlatMap<V>(); // Spigot
 
     public LongObjectHashMap() {
         initialize();
@@ -61,6 +62,17 @@ public class LongObjectHashMap<V> implements Cloneable, Serializable {
     }
 
     public V get(long key) {
+        // Spigot start
+        if ( size == 0 )
+        {
+            return null;
+        }
+        V val = flat.get( key );
+        if ( val != null )
+        {
+            return val;
+        }
+        // Spigot end
         int index = (int) (keyIndex(key) & (BUCKET_SIZE - 1));
         long[] inner = keys[index];
         if (inner == null) return null;
@@ -78,6 +90,7 @@ public class LongObjectHashMap<V> implements Cloneable, Serializable {
     }
 
     public V put(long key, V value) {
+        flat.put(key, value); // Spigot
         int index = (int) (keyIndex(key) & (BUCKET_SIZE - 1));
         long[] innerKeys = keys[index];
         V[] innerValues = values[index];
@@ -124,6 +137,7 @@ public class LongObjectHashMap<V> implements Cloneable, Serializable {
     }
 
     public V remove(long key) {
+        flat.remove(key); // Spigot
         int index = (int) (keyIndex(key) & (BUCKET_SIZE - 1));
         long[] inner = keys[index];
         if (inner == null) {
@@ -174,6 +188,7 @@ public class LongObjectHashMap<V> implements Cloneable, Serializable {
         size = 0;
         Arrays.fill(keys, null);
         Arrays.fill(values, null);
+        flat = new org.spigotmc.FlatMap<V>();
     }
 
     public Set<Long> keySet() {
diff --git a/src/main/java/org/spigotmc/FlatMap.java b/src/main/java/org/spigotmc/FlatMap.java
new file mode 100644
index 0000000..9416f6e
--- /dev/null
+++ b/src/main/java/org/spigotmc/FlatMap.java
@@ -0,0 +1,64 @@
+package org.spigotmc;
+
+import org.bukkit.craftbukkit.util.LongHash;
+
+public class FlatMap<V>
+{
+
+    private static final int FLAT_LOOKUP_SIZE = 512;
+    private final Object[][] flatLookup = new Object[ FLAT_LOOKUP_SIZE * 2 ][ FLAT_LOOKUP_SIZE * 2 ];
+
+    public void put(long msw, long lsw, V value)
+    {
+        long acx = Math.abs( msw );
+        long acz = Math.abs( lsw );
+        if ( acx < FLAT_LOOKUP_SIZE && acz < FLAT_LOOKUP_SIZE )
+        {
+            flatLookup[(int) ( msw + FLAT_LOOKUP_SIZE )][(int) ( lsw + FLAT_LOOKUP_SIZE )] = value;
+        }
+    }
+
+    public void put(long key, V value)
+    {
+        put( LongHash.msw( key ), LongHash.lsw( key ), value );
+
+    }
+
+    public void remove(long key)
+    {
+        put( key, null );
+    }
+
+    public void remove(long msw, long lsw)
+    {
+        put( msw, lsw, null );
+    }
+
+    public boolean contains(long msw, long lsw)
+    {
+        return get( msw, lsw ) != null;
+    }
+
+    public boolean contains(long key)
+    {
+        return get( key ) != null;
+    }
+
+    public V get(long msw, long lsw)
+    {
+        long acx = Math.abs( msw );
+        long acz = Math.abs( lsw );
+        if ( acx < FLAT_LOOKUP_SIZE && acz < FLAT_LOOKUP_SIZE )
+        {
+            return (V) flatLookup[(int) ( msw + FLAT_LOOKUP_SIZE )][(int) ( lsw + FLAT_LOOKUP_SIZE )];
+        } else
+        {
+            return null;
+        }
+    }
+
+    public V get(long key)
+    {
+        return get( LongHash.msw( key ), LongHash.lsw( key ) );
+    }
+}
-- 
1.9.1