Redo longhash tweaks

This commit is contained in:
md_5 2013-06-21 17:14:11 +10:00
parent 8cbfcf995d
commit dce45d252c
2 changed files with 135 additions and 69 deletions

View File

@ -1,4 +1,4 @@
From b602c4203e83e437284f7716e1b6535e22a3af2b Mon Sep 17 00:00:00 2001
From b7bcbfba58901386aae82eb079dafc7d2bb6f17f Mon Sep 17 00:00:00 2001
From: md_5 <md_5@live.com.au>
Date: Sat, 23 Mar 2013 09:46:33 +1100
Subject: [PATCH] Merge tweaks and configuration
@ -69,11 +69,11 @@ index afe3e4d..97da2cd 100644
if (event != null && (event.isCancelled() || entity.dead)) {
entity.dead = true;
diff --git a/src/main/java/org/spigotmc/SpigotWorldConfig.java b/src/main/java/org/spigotmc/SpigotWorldConfig.java
index 899f86c..1101361 100644
index fcc2c88..58406e5 100644
--- a/src/main/java/org/spigotmc/SpigotWorldConfig.java
+++ b/src/main/java/org/spigotmc/SpigotWorldConfig.java
@@ -99,4 +99,18 @@ public class SpigotWorldConfig
wheatModifier = getInt( "growth.wheat-modifier", wheatModifier );
wheatModifier = getInt( "growth.wheat-modifier", 100 );
log( "Cactus Growth Modifier: " + cactusModifier + "%" );
}
+

View File

@ -1,119 +1,107 @@
From a11aa1e2aecca3e245bb003b7caf9e4287e14271 Mon Sep 17 00:00:00 2001
From aa15e3095f30c11c90f034e2b1b9ebbe08337c56 Mon Sep 17 00:00:00 2001
From: md_5 <md_5@live.com.au>
Date: Sat, 23 Mar 2013 09:29:43 +1100
Subject: [PATCH] LongHash Tweaks.
Date: Fri, 21 Jun 2013 17:13:47 +1000
Subject: [PATCH] LongHash Tweaks
This commit adds a flat array based cache to the LongHash(Set/Map) classes leading to excellent efficiency for servers where most activity is centered around the origin (0,0)
diff --git a/src/main/java/org/bukkit/craftbukkit/util/FlatMap.java b/src/main/java/org/bukkit/craftbukkit/util/FlatMap.java
new file mode 100644
index 0000000..e8a7725
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/util/FlatMap.java
@@ -0,0 +1,34 @@
+package org.bukkit.craftbukkit.util;
+
+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 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));
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java b/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java
index 22c96c5..331d570 100644
index 22c96c5..7c3005e 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java
@@ -31,6 +31,8 @@ public class LongHashSet {
@@ -31,6 +31,7 @@ public class LongHashSet {
private int elements;
private long[] values;
private int modCount;
+ private static final Object PRESENT = new Object();
+ private final FlatMap<Object> flat = new FlatMap<Object>();
+ private org.spigotmc.FlatMap<Boolean> flat = new org.spigotmc.FlatMap<Boolean>(); // Spigot
public LongHashSet() {
this(INITIAL_SIZE);
@@ -56,10 +58,12 @@ public class LongHashSet {
@@ -56,10 +57,22 @@ public class LongHashSet {
}
public boolean contains(int msw, int lsw) {
+ if (flat.get(msw, lsw) != null) return true; // Spigot
+ // Spigot start
+ if ( flat.contains( msw, lsw ) )
+ {
+ return true;
+ }
+ // Spigot end
return contains(LongHash.toLong(msw, lsw));
}
public boolean contains(long value) {
+ if (flat.get(value) != null) return true; // Spigot
+ // Spigot start
+ if ( flat.contains( value ) )
+ {
+ return true;
+ }
+ // Spigot end
int hash = hash(value);
int index = (hash & 0x7FFFFFFF) % values.length;
int offset = 1;
@@ -82,6 +86,7 @@ public class LongHashSet {
@@ -82,6 +95,7 @@ public class LongHashSet {
}
public boolean add(long value) {
+ flat.put(value, PRESENT); // Spigot
+ flat.put( value, Boolean.TRUE ); // Spigot
int hash = hash(value);
int index = (hash & 0x7FFFFFFF) % values.length;
int offset = 1;
@@ -125,10 +130,11 @@ public class LongHashSet {
@@ -125,10 +139,18 @@ public class LongHashSet {
}
public void remove(int msw, int lsw) {
+ flat.put(msw, lsw, null); // Spigot
remove(LongHash.toLong(msw, lsw));
- remove(LongHash.toLong(msw, lsw));
+ // Spigot start
+ flat.remove(msw, lsw);
+ remove0(LongHash.toLong(msw, lsw));
}
- public boolean remove(long value) {
+ private boolean remove(long value) { // Spigot
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 +183,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..dbd33fa 100644
index 01861cc..08b543b 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 final FlatMap<V> flat = new FlatMap<V>(); // Spigot
+ private transient org.spigotmc.FlatMap<V> flat = new org.spigotmc.FlatMap<V>(); // Spigot
public LongObjectHashMap() {
initialize();
@@ -61,6 +62,8 @@ public class LongObjectHashMap<V> implements Cloneable, Serializable {
@@ -61,6 +62,13 @@ public class LongObjectHashMap<V> implements Cloneable, Serializable {
}
public V get(long key) {
+ V val = flat.get(key); // Spigot
+ if (val != null) return val; // Spigot
+ // Spigot start
+ 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 +81,7 @@ public class LongObjectHashMap<V> implements Cloneable, Serializable {
@@ -78,6 +86,7 @@ public class LongObjectHashMap<V> implements Cloneable, Serializable {
}
public V put(long key, V value) {
@ -121,14 +109,92 @@ index 01861cc..dbd33fa 100644
int index = (int) (keyIndex(key) & (BUCKET_SIZE - 1));
long[] innerKeys = keys[index];
V[] innerValues = values[index];
@@ -124,6 +128,7 @@ public class LongObjectHashMap<V> implements Cloneable, Serializable {
@@ -124,6 +133,7 @@ public class LongObjectHashMap<V> implements Cloneable, Serializable {
}
public V remove(long key) {
+ flat.put(key, null); // Spigot
+ flat.remove(key); // Spigot
int index = (int) (keyIndex(key) & (BUCKET_SIZE - 1));
long[] inner = keys[index];
if (inner == null) {
@@ -174,6 +184,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.8.1.2