Performance enhancement

- introduced byte[] - based ARC method for MapHeap which avoids a String generation each time the cache is accessed
- bugfixing in required class ComparableARC

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7110 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 15 years ago
parent ae07e11bc5
commit b2c9db48ea

@ -22,6 +22,7 @@
package net.yacy.cora.storage; package net.yacy.cora.storage;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@ -44,25 +45,57 @@ public final class ComparableARC<K, V> extends SimpleARC<K, V> implements Map<K,
this.limit = cacheSize; this.limit = cacheSize;
this.keys = new LinkedList<K>(); this.keys = new LinkedList<K>();
} }
public V put(K k, V v) { public synchronized V put(K k, V v) {
V r = super.put(k, v); V r = super.put(k, v);
keys.add(k); if (r == null) keys.add(k);
if (keys.size() > this.limit) { if (keys.size() > this.limit) {
K w = this.keys.removeFirst(); K w = this.keys.removeFirst();
assert w != null;
V t = super.remove(w); V t = super.remove(w);
assert t != null; assert t != null : "keys.size() = " + keys.size() + ", limit = " + this.limit;
} }
return r; return r;
} }
public V remove(Object k) { public void putAll(Map<? extends K, ? extends V> map) {
for (Map.Entry<? extends K, ? extends V> entry: map.entrySet()) put(entry.getKey(), entry.getValue());
}
public synchronized V remove(Object k) {
V r = super.remove(k); V r = super.remove(k);
this.keys.remove(k); if (r == null) return null;
@SuppressWarnings("unchecked")
boolean removed = removeFromKeys((K) k);
assert removed;
return r; return r;
} }
public void clear() { public synchronized Map.Entry<K,V> pollFirstEntry() {
Map.Entry<K,V> entry = super.pollFirstEntry();
boolean removed = removeFromKeys(entry.getKey());
assert removed;
return entry;
}
public synchronized Map.Entry<K,V> pollLastEntry() {
Map.Entry<K,V> entry = super.pollLastEntry();
boolean removed = removeFromKeys(entry.getKey());
assert removed;
return entry;
}
public synchronized void clear() {
super.clear(); super.clear();
this.keys.clear(); this.keys.clear();
} }
private boolean removeFromKeys(K k) {
assert k != null;
Iterator<K> i = keys.iterator();
K x;
while (i.hasNext()) {
x = i.next();
if (super.comparator().compare(k, x) == 0) {
i.remove();
return true;
}
}
return false;
}
} }
} }

@ -42,6 +42,11 @@ public final class ConcurrentARC<K, V> extends AbstractMap<K, V> implements Map<
private final int mask; private final int mask;
private final ARC<K, V> arc[]; private final ARC<K, V> arc[];
/**
* create a concurrent ARC based on a HashARC. The type of the key elements must implement a hashing function
* @param cacheSize the number of maximum entries
* @param partitions the number of partitions
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public ConcurrentARC(final int cacheSize, final int partitions) { public ConcurrentARC(final int cacheSize, final int partitions) {
int m = 1; int m = 1;
@ -52,6 +57,12 @@ public final class ConcurrentARC<K, V> extends AbstractMap<K, V> implements Map<
this.mask = m; this.mask = m;
} }
/**
* create a concurrent ARC based on a ComparableARC
* @param cacheSize the number of maximum entries
* @param partitions the number of partitions
* @param comparator a comparator for the key object which may be of type byte[]
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public ConcurrentARC(final int cacheSize, final int partitions, Comparator<? super K> comparator) { public ConcurrentARC(final int cacheSize, final int partitions, Comparator<? super K> comparator) {
int m = 1; int m = 1;

@ -55,7 +55,7 @@ import net.yacy.kelondro.util.kelondroException;
public class MapHeap implements Map<byte[], Map<String, String>> { public class MapHeap implements Map<byte[], Map<String, String>> {
private BLOB blob; private BLOB blob;
private ARC<String, Map<String, String>> cache; private ARC<byte[], Map<String, String>> cache;
private final char fillchar; private final char fillchar;
@ -67,7 +67,7 @@ public class MapHeap implements Map<byte[], Map<String, String>> {
final int cachesize, final int cachesize,
char fillchar) throws IOException { char fillchar) throws IOException {
this.blob = new Heap(heapFile, keylength, ordering, buffermax); this.blob = new Heap(heapFile, keylength, ordering, buffermax);
this.cache = new ConcurrentARC<String, Map<String, String>>(cachesize, Runtime.getRuntime().availableProcessors()); this.cache = new ConcurrentARC<byte[], Map<String, String>>(cachesize, Runtime.getRuntime().availableProcessors(), ordering);
this.fillchar = fillchar; this.fillchar = fillchar;
} }
@ -147,7 +147,7 @@ public class MapHeap implements Map<byte[], Map<String, String>> {
if (blob != null) blob.insert(key, sb); if (blob != null) blob.insert(key, sb);
// write map to cache // write map to cache
if (cache != null) cache.put(new String(key), newMap); if (cache != null) cache.put(key, newMap);
} }
} }
@ -176,7 +176,7 @@ public class MapHeap implements Map<byte[], Map<String, String>> {
synchronized (this) { synchronized (this) {
// remove from cache // remove from cache
cache.remove(new String(key)); cache.remove(key);
// remove from file // remove from file
blob.delete(key); blob.delete(key);
@ -208,7 +208,7 @@ public class MapHeap implements Map<byte[], Map<String, String>> {
byte[] key = normalizeKey((byte[]) k); byte[] key = normalizeKey((byte[]) k);
boolean h; boolean h;
synchronized (this) { synchronized (this) {
h = this.cache.containsKey(new String(key)) || this.blob.containsKey(key); h = this.cache.containsKey(key) || this.blob.containsKey(key);
} }
return h; return h;
} }
@ -262,8 +262,7 @@ public class MapHeap implements Map<byte[], Map<String, String>> {
Map<String, String> map; Map<String, String> map;
if (storeCache) { if (storeCache) {
synchronized (this) { synchronized (this) {
String keys = new String(key); map = cache.get(key);
map = cache.get(keys);
if (map != null) return map; if (map != null) return map;
// read object // read object
@ -276,7 +275,7 @@ public class MapHeap implements Map<byte[], Map<String, String>> {
} }
// write map to cache // write map to cache
cache.put(keys, map); cache.put(key, map);
} }
// return value // return value
@ -284,7 +283,7 @@ public class MapHeap implements Map<byte[], Map<String, String>> {
} else { } else {
byte[] b; byte[] b;
synchronized (this) { synchronized (this) {
map = cache.get(new String(key)); map = cache.get(key);
if (map != null) return map; if (map != null) return map;
b = blob.get(key); b = blob.get(key);
} }

Loading…
Cancel
Save