* SDSCache: Use weak refs; increase size for pub keys

This commit is contained in:
zzz
2011-09-06 12:13:33 +00:00
parent 5030a86311
commit fdc3af97aa
3 changed files with 10 additions and 5 deletions

View File

@ -21,7 +21,7 @@ import java.io.IOException;
*/ */
public class PublicKey extends SimpleDataStructure { public class PublicKey extends SimpleDataStructure {
public final static int KEYSIZE_BYTES = 256; public final static int KEYSIZE_BYTES = 256;
private static final int CACHE_SIZE = 256; private static final int CACHE_SIZE = 1024;
private static final SDSCache<PublicKey> _cache = new SDSCache(PublicKey.class, KEYSIZE_BYTES, CACHE_SIZE); private static final SDSCache<PublicKey> _cache = new SDSCache(PublicKey.class, KEYSIZE_BYTES, CACHE_SIZE);

View File

@ -3,6 +3,7 @@ package net.i2p.data;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -56,7 +57,7 @@ public class SDSCache<V extends SimpleDataStructure> {
} }
/** the LRU cache */ /** the LRU cache */
private final Map<Integer, V> _cache; private final Map<Integer, WeakReference<V>> _cache;
/** the byte array length for the class we are caching */ /** the byte array length for the class we are caching */
private final int _datalen; private final int _datalen;
/** the constructor for the class we are caching */ /** the constructor for the class we are caching */
@ -111,7 +112,11 @@ public class SDSCache<V extends SimpleDataStructure> {
V rv; V rv;
Integer key = hashCodeOf(data); Integer key = hashCodeOf(data);
synchronized(_cache) { synchronized(_cache) {
rv = _cache.get(key); WeakReference<V> ref = _cache.get(key);
if (ref != null)
rv = ref.get();
else
rv = null;
if (rv != null && DataHelper.eq(data, rv.getData())) { if (rv != null && DataHelper.eq(data, rv.getData())) {
// found it, we don't need the data passed in any more // found it, we don't need the data passed in any more
SimpleByteCache.release(data); SimpleByteCache.release(data);
@ -127,7 +132,7 @@ public class SDSCache<V extends SimpleDataStructure> {
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
throw new RuntimeException("SDSCache error", e); throw new RuntimeException("SDSCache error", e);
} }
_cache.put(key, rv); _cache.put(key, new WeakReference(rv));
found = 0; found = 0;
} }
} }

View File

@ -22,7 +22,7 @@ import java.io.IOException;
*/ */
public class SigningPublicKey extends SimpleDataStructure { public class SigningPublicKey extends SimpleDataStructure {
public final static int KEYSIZE_BYTES = 128; public final static int KEYSIZE_BYTES = 128;
private static final int CACHE_SIZE = 256; private static final int CACHE_SIZE = 1024;
private static final SDSCache<SigningPublicKey> _cache = new SDSCache(SigningPublicKey.class, KEYSIZE_BYTES, CACHE_SIZE); private static final SDSCache<SigningPublicKey> _cache = new SDSCache(SigningPublicKey.class, KEYSIZE_BYTES, CACHE_SIZE);