Skip to content
This repository has been archived by the owner on Jan 26, 2019. It is now read-only.

Removed FlatMap to improve performance #407

Merged
merged 1 commit into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 9 additions & 117 deletions src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@

package org.bukkit.craftbukkit.util;

import java.util.Collection;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ConcurrentModificationException;
import java.util.NoSuchElementException;
import java.util.Set;

public class LongHashSet implements Set<Long> {
public class LongHashSet {
private final static int INITIAL_SIZE = 3;
private final static double LOAD_FACTOR = 0.75;

Expand All @@ -33,7 +32,6 @@ public class LongHashSet implements Set<Long> {
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);
Expand All @@ -46,46 +44,23 @@ public LongHashSet(int size) {
modCount = 0;
}

@Override
public Iterator<Long> iterator() {
public Iterator iterator() {
return new Itr();
}

@Override
public int size() {
return elements;
}

@Override
public boolean isEmpty() {
return elements == 0;
}

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;
Expand All @@ -108,7 +83,6 @@ public boolean add(int msw, int lsw) {
}

public boolean add(long value) {
flat.put( value, Boolean.TRUE ); // Spigot
int hash = hash(value);
int index = (hash & 0x7FFFFFFF) % values.length;
int offset = 1;
Expand Down Expand Up @@ -152,18 +126,10 @@ public boolean add(long value) {
}

public void remove(int msw, int lsw) {
// Spigot start
flat.remove(msw, lsw);
remove0(LongHash.toLong(msw, lsw));
remove(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;
Expand All @@ -188,7 +154,6 @@ private boolean remove0(long value) {
}
}

@Override
public void clear() {
elements = 0;
for (int ix = 0; ix < values.length; ix++) {
Expand All @@ -197,12 +162,11 @@ public void clear() {

freeEntries = values.length;
modCount++;
flat = new org.spigotmc.FlatMap<Boolean>();
}

public long[] toPrimitiveArray() {
public long[] toArray() {
long[] result = new long[elements];
long[] values = Java15Compat.Arrays_copyOf(this.values, this.values.length);
long[] values = Arrays.copyOf(this.values, this.values.length);
int pos = 0;

for (long value : values) {
Expand All @@ -214,26 +178,6 @@ public long[] toPrimitiveArray() {
return result;
}

@Override
public Long[] toArray() {
Long[] result = new Long[elements];
long[] values = Java15Compat.Arrays_copyOf(this.values, this.values.length);
int pos = 0;

for (long value : values) {
if (value != FREE && value != REMOVED) {
result[pos++] = value;
}
}

return result;
}

@Override
public <T> T[] toArray(T[] arg0) {
throw new UnsupportedOperationException();
}

public long popFirst() {
for (long value : values) {
if (value != FREE && value != REMOVED) {
Expand All @@ -246,7 +190,7 @@ public long popFirst() {
}

public long[] popAll() {
long[] ret = toPrimitiveArray();
long[] ret = toArray();
clear();
return ret;
}
Expand Down Expand Up @@ -299,7 +243,7 @@ private void rehash(int newCapacity) {
freeEntries = values.length - elements;
}

private class Itr implements Iterator<Long> {
private class Itr implements Iterator {
private int index;
private int lastReturned = -1;
private int expectedModCount;
Expand All @@ -311,12 +255,10 @@ public Itr() {
expectedModCount = modCount;
}

@Override
public boolean hasNext() {
return index != values.length;
}

@Override
public Long next() {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
Expand All @@ -340,7 +282,6 @@ public Long next() {
}
}

@Override
public void remove() {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
Expand All @@ -358,53 +299,4 @@ public void remove() {
}
}
}

@Override
public boolean add(Long value) {
return add(value.longValue());
}

@Override
public boolean addAll(Collection<? extends Long> collection) {
boolean result = false;
for (Long value : collection) result |= add(value.longValue());
return result;
}

@Override
public boolean contains(Object o) {
return o instanceof Long ? contains(((Long) o).longValue()) : false;
}

@Override
public boolean containsAll(Collection<?> collection) {
for (Object value : collection) if (!contains(value)) return false;
return true;
}

@Override
public boolean remove(Object o) {
return o instanceof Long ? remove(((Long) o).longValue()) : false;
}

@Override
public boolean removeAll(Collection<?> collection) {
boolean result = false;
for (Object value : collection) result |= remove(value);
return result;
}

@Override
public boolean retainAll(Collection<?> collection) {
boolean result = false;
Iterator<Long> iterator = iterator();
while(iterator.hasNext()) {
Long l = iterator.next();
if (!collection.contains(l)) {
iterator.remove();
result = true;
}
}
return result;
}
}
}
76 changes: 17 additions & 59 deletions src/main/java/org/bukkit/craftbukkit/util/LongObjectHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import java.util.NoSuchElementException;
import java.util.Set;

import static org.bukkit.craftbukkit.util.Java15Compat.Arrays_copyOf;

@SuppressWarnings("unchecked")
public class LongObjectHashMap<V> implements Cloneable, Serializable {
static final long serialVersionUID = 2841537710170573815L;
Expand All @@ -28,7 +26,6 @@ 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();
Expand Down Expand Up @@ -62,17 +59,6 @@ public boolean containsValue(V value) {
}

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;
Expand All @@ -90,7 +76,6 @@ public V get(long key) {
}

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];
Expand Down Expand Up @@ -125,9 +110,9 @@ public V put(long key, V value) {
}

// chain is full, resize it and add our new entry
keys[index] = innerKeys = Arrays_copyOf(innerKeys, i << 1);
keys[index] = innerKeys = Arrays.copyOf(innerKeys, i << 1);
Arrays.fill(innerKeys, i, innerKeys.length, EMPTY_KEY);
values[index] = innerValues = Arrays_copyOf(innerValues, i << 1);
values[index] = innerValues = Arrays.copyOf(innerValues, i << 1);
innerKeys[i] = key;
innerValues[i] = value;
size++;
Expand All @@ -137,7 +122,6 @@ public V put(long key, V value) {
}

public V remove(long key) {
flat.remove(key); // Spigot
int index = (int) (keyIndex(key) & (BUCKET_SIZE - 1));
long[] inner = keys[index];
if (inner == null) {
Expand Down Expand Up @@ -188,7 +172,6 @@ public void clear() {
size = 0;
Arrays.fill(keys, null);
Arrays.fill(values, null);
flat = new org.spigotmc.FlatMap<V>();
}

public Set<Long> keySet() {
Expand All @@ -205,9 +188,16 @@ public Collection<V> values() {
* this reason it should be avoided if at all possible.
*
* @return Set of Entry objects
* @deprecated
*/
@Deprecated
public Set<Map.Entry<Long, V>> entrySet() {
return new EntrySet();
HashSet<Map.Entry<Long, V>> set = new HashSet<Map.Entry<Long, V>>();
for (long key : keySet()) {
set.add(new Entry(key, get(key)));
}

return set;
}

public Object clone() throws CloneNotSupportedException {
Expand Down Expand Up @@ -406,9 +396,14 @@ public Iterator<V> iterator() {


private class Entry implements Map.Entry<Long, V> {
private Long key;
private final Long key;
private V value;

Entry(long k, V v) {
key = k;
value = v;
}

public Long getKey() {
return key;
}
Expand All @@ -423,42 +418,5 @@ public V setValue(V v) {
put(key, v);
return old;
}

private void bind(long key, V value) {
this.key = key;
this.value = value;
}
}

private class EntrySet extends AbstractSet<Map.Entry<Long, V>> {
@Override
public Iterator<Map.Entry<Long, V>> iterator() {
return new Iterator<Map.Entry<Long, V>>() {
final Entry entry = new Entry();
final ValueIterator valueIterator = new ValueIterator();

@Override
public boolean hasNext() {
return valueIterator.hasNext();
}

@Override
public LongObjectHashMap<V>.Entry next() {
V value = valueIterator.next();
entry.bind(valueIterator.prevKey, value);
return entry;
}

@Override
public void remove() {
valueIterator.remove();
}
};
}

@Override
public int size() {
return LongObjectHashMap.this.size;
}
}
}
}
Loading