3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-11-16 21:10:17 +01:00

Updated SoftMap to be concurrent.

Dieser Commit ist enthalten in:
Raphfrk 2011-03-10 22:13:47 +00:00 committet von Erik Broes
Ursprung 9464602720
Commit 0b05cbb998
2 geänderte Dateien mit 97 neuen und 28 gelöschten Zeilen

Datei anzeigen

@ -13,11 +13,11 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.craftbukkit.block.CraftBlock;
import org.bukkit.entity.Entity;
import org.bukkit.craftbukkit.util.SoftMap;
import org.bukkit.craftbukkit.util.ConcurrentSoftMap;
public class CraftChunk implements Chunk {
private WeakReference<net.minecraft.server.Chunk> weakChunk;
private final SoftMap<Integer, Block> cache = new SoftMap<Integer, Block>();
private final ConcurrentSoftMap<Integer, Block> cache = new ConcurrentSoftMap<Integer, Block>();
private WorldServer worldServer;
private int x;
private int z;
@ -63,8 +63,13 @@ public class CraftChunk implements Chunk {
int pos = (x & 0xF) << 11 | (z & 0xF) << 7 | (y & 0x7F);
Block block = this.cache.get( pos );
if (block == null) {
block = new CraftBlock( this, (getX() << 4) | (x & 0xF), y & 0x7F, (getZ() << 4) | (z & 0xF) );
this.cache.put( pos, block );
Block newBlock = new CraftBlock( this, (getX() << 4) | (x & 0xF), y & 0x7F, (getZ() << 4) | (z & 0xF) );
Block oldBlock = this.cache.put( pos, newBlock );
if(oldBlock == null) {
block = newBlock;
} else {
block = oldBlock;
}
}
return block;
}

Datei anzeigen

@ -3,7 +3,7 @@ package org.bukkit.craftbukkit.util;
import java.util.Map;
import java.util.AbstractMap;
import java.util.LinkedList;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Set;
import java.util.Collection;
import java.util.Iterator;
@ -28,37 +28,39 @@ import java.lang.ref.SoftReference;
* @author raphfrk
*/
public class SoftMap<K,V> {
public class ConcurrentSoftMap<K,V> {
private final HashMap<K,SoftMapReference<K,V>> map = new HashMap<K,SoftMapReference<K,V>>();
private final ConcurrentHashMap<K,SoftMapReference<K,V>> map = new ConcurrentHashMap<K,SoftMapReference<K,V>>();
private final ReferenceQueue<SoftMapReference> queue = new ReferenceQueue<SoftMapReference>();
private final LinkedList<V> strongReferenceQueue = new LinkedList<V>();
private final int strongReferenceSize;
public SoftMap() {
public ConcurrentSoftMap() {
this(20);
}
public SoftMap(int size) {
public ConcurrentSoftMap(int size) {
strongReferenceSize = size;
}
// When a soft reference is deleted by the garbage collector, it is set to reference null
// and added to the queue
//
// However, these null references still exist in the HashMap as keys. This method removes these keys.
// However, these null references still exist in the ConcurrentHashMap as keys. This method removes these keys.
//
// It is called whenever there is a method call of the map.
private void emptyQueue() {
SoftMapReference ref;
while((ref=(SoftMapReference)queue.poll()) != null) {
while ((ref=(SoftMapReference) queue.poll()) != null) {
map.remove(ref.key);
}
}
public void clear() {
synchronized(strongReferenceQueue) {
strongReferenceQueue.clear();
}
map.clear();
emptyQueue();
}
@ -105,11 +107,13 @@ public class SoftMap<K,V> {
}
V value = ref.get();
if (value!=null) {
synchronized(strongReferenceQueue) {
strongReferenceQueue.addFirst(value);
if (strongReferenceQueue.size() > strongReferenceSize) {
strongReferenceQueue.removeLast();
}
}
}
return value;
}
@ -145,20 +149,69 @@ public class SoftMap<K,V> {
private void fastPut(K key, V value) {
map.put(key, new SoftMapReference<K,V>(key, value, queue));
synchronized(strongReferenceQueue) {
strongReferenceQueue.addFirst(value);
if (strongReferenceQueue.size() > strongReferenceSize) {
strongReferenceQueue.removeLast();
}
}
}
public V putIfAbsent(K key, V value) {
emptyQueue();
return fastPutIfAbsent(key, value);
}
private V fastPutIfAbsent(K key, V value) {
V ret = null;
if (map.containsKey(key)) {
SoftMapReference<K,V> current = map.get(key);
if (current != null) {
ret = current.get();
}
}
if (ret == null) {
SoftMapReference<K,V> newValue = new SoftMapReference<K,V>(key, value, queue);
boolean success = false;
while (!success) {
SoftMapReference<K,V> oldValue = map.putIfAbsent(key, newValue);
if (oldValue == null) { // put was successful (key didn't exist)
ret = null;
success = true;
} else {
ret = oldValue.get();
if (ret == null) { // key existed, but referenced null
success = map.replace(key, oldValue, newValue); // try to swap old for new
} else { // key existed, and referenced a valid object
success = true;
}
}
}
}
if (ret == null) {
synchronized(strongReferenceQueue) {
strongReferenceQueue.addFirst(value);
if (strongReferenceQueue.size() > strongReferenceSize) {
strongReferenceQueue.removeLast();
}
}
}
return ret;
}
// Adds the mappings to the map
public void putAll(Map other) {
emptyQueue();
Iterator<K> itr = other.keySet().iterator();
while(itr.hasNext()) {
while (itr.hasNext()) {
K key = itr.next();
fastPut(key, (V)other.get(key));
fastPut(key, (V) other.get(key));
}
}
@ -195,6 +248,17 @@ public class SoftMap<K,V> {
super(value, queue);
this.key = key;
}
}
@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (!(o instanceof SoftMapReference)) {
return false;
}
SoftMapReference other = (SoftMapReference) o;
return other.get() == get();
}
}
}