better digest pool size (smaller by default but unlimited)

pull/1/head
Michael Christen 13 years ago
parent 1f4afb4dc0
commit e13441b069

@ -41,6 +41,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import net.yacy.cora.document.UTF8; import net.yacy.cora.document.UTF8;
@ -50,18 +51,7 @@ import net.yacy.kelondro.logging.Log;
public class Digest { public class Digest {
private final static int digestThreads = Runtime.getRuntime().availableProcessors() * 4; public static BlockingQueue<MessageDigest> digestPool = new LinkedBlockingDeque<MessageDigest>();
public static BlockingQueue<MessageDigest> digestPool = new ArrayBlockingQueue<MessageDigest>(digestThreads);
static {
for (int i = 0; i < digestThreads; i++)
try {
final MessageDigest digest = MessageDigest.getInstance("MD5");
digest.reset();
digestPool.add(digest);
} catch (final NoSuchAlgorithmException e) {
Log.logException(e);
}
}
public static String encodeHex(final long in, final int length) { public static String encodeHex(final long in, final int length) {
String s = Long.toHexString(in); String s = Long.toHexString(in);
@ -114,44 +104,25 @@ public class Digest {
public static byte[] encodeMD5Raw(final String key) { public static byte[] encodeMD5Raw(final String key) {
MessageDigest digest = null; MessageDigest digest = null;
boolean fromPool = true; digest = digestPool.poll();
if (digestPool.size() == 0) { if (digest == null) {
// if there are no digest objects left, create some on the fly // if there are no digest objects left, create some on the fly
// this is not the most effective way but if we wouldn't do that the encoder would block // this is not the most effective way but if we wouldn't do that the encoder would block
try { try {
digest = MessageDigest.getInstance("MD5"); digest = MessageDigest.getInstance("MD5");
digest.reset(); digest.reset();
fromPool = false;
} catch (final NoSuchAlgorithmException e) { } catch (final NoSuchAlgorithmException e) {
} }
} }
if (digest == null) try {
digest = digestPool.take();
} catch (final InterruptedException e) {
Log.logWarning("Digest", "using generic instead of pooled digest");
try {
digest = MessageDigest.getInstance("MD5");
} catch (final NoSuchAlgorithmException e1) {
Log.logException(e1);
}
digest.reset();
fromPool = false;
}
byte[] keyBytes; byte[] keyBytes;
keyBytes = UTF8.getBytes(key); keyBytes = UTF8.getBytes(key);
digest.update(keyBytes); digest.update(keyBytes);
final byte[] result = digest.digest(); final byte[] result = digest.digest();
digest.reset(); digest.reset();
if (fromPool) { try {
returntopool: while (true) { digestPool.put(digest);
try { //System.out.println("Digest Pool size = " + digestPool.size());
digestPool.put(digest); } catch ( InterruptedException e ) {
break returntopool;
} catch (final InterruptedException e) {
// we MUST return that digest to the pool
continue returntopool;
}
}
} }
return result; return result;
} }
@ -250,6 +221,7 @@ public class Digest {
} }
} }
@Override
public MessageDigest call() { public MessageDigest call() {
try { try {
filechunk c; filechunk c;

Loading…
Cancel
Save