Avoid double Map search

This commit is contained in:
nicksavers 2014-08-03 18:31:22 +02:00
parent dfd2e26c6e
commit 31fddb591b
1 changed files with 12 additions and 13 deletions

View File

@ -7,18 +7,16 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import org.ethereum.db.ByteArrayWrapper; import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP; import org.ethereum.util.RLP;
import org.ethereum.util.Utils; import org.ethereum.util.Utils;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
import org.ethereum.util.LRUMap; import org.ethereum.util.LRUMap;
public class HashUtil { public class HashUtil {
private static final int MAX_ENTRIES = 1000; // Should contain most commonly hashed values private static final int MAX_ENTRIES = 100; // Should contain most commonly hashed values
private static LRUMap<ByteArrayWrapper, byte[]> sha3Cache = new LRUMap<>(0, MAX_ENTRIES); private static LRUMap<ByteArrayWrapper, byte[]> sha3Cache = new LRUMap<>(0, MAX_ENTRIES);
public static final byte[] EMPTY_DATA_HASH = HashUtil.sha3(new byte[0]); public static final byte[] EMPTY_DATA_HASH = Hex.decode("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");
private static final MessageDigest sha256digest; private static final MessageDigest sha256digest;
@ -36,9 +34,10 @@ public class HashUtil {
public static byte[] sha3(byte[] input) { public static byte[] sha3(byte[] input) {
ByteArrayWrapper inputByteArray = new ByteArrayWrapper(input); ByteArrayWrapper inputByteArray = new ByteArrayWrapper(input);
if(sha3Cache.keySet().contains(inputByteArray)) byte[] result = sha3Cache.get(inputByteArray);
return sha3Cache.get(inputByteArray); if(result != null)
byte[] result = SHA3Helper.sha3(input); return result;
result = SHA3Helper.sha3(input);
sha3Cache.put(inputByteArray, result); sha3Cache.put(inputByteArray, result);
return result; return result;
} }
@ -62,7 +61,7 @@ public class HashUtil {
byte[] encSender = RLP.encodeElement(addr); byte[] encSender = RLP.encodeElement(addr);
byte[] encNonce = RLP.encodeElement(nonce); byte[] encNonce = RLP.encodeElement(nonce);
byte[] newAddress = HashUtil.sha3omit12(RLP.encodeList(encSender, encNonce)); byte[] newAddress = sha3omit12(RLP.encodeList(encSender, encNonce));
return newAddress; return newAddress;
} }