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 org.ethereum.db.ByteArrayWrapper;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.ethereum.util.Utils;
import org.spongycastle.util.encoders.Hex;
import org.ethereum.util.LRUMap;
public class HashUtil {
private static final int MAX_ENTRIES = 1000; // Should contain most commonly hashed values
private static LRUMap<ByteArrayWrapper, byte[]> sha3Cache = new LRUMap<>(0, MAX_ENTRIES);
public static final byte[] EMPTY_DATA_HASH = HashUtil.sha3(new byte[0]);
private static final int MAX_ENTRIES = 100; // Should contain most commonly hashed values
private static LRUMap<ByteArrayWrapper, byte[]> sha3Cache = new LRUMap<>(0, MAX_ENTRIES);
public static final byte[] EMPTY_DATA_HASH = Hex.decode("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");
private static final MessageDigest sha256digest;
@ -35,14 +33,15 @@ public class HashUtil {
}
public static byte[] sha3(byte[] input) {
ByteArrayWrapper inputByteArray = new ByteArrayWrapper(input);
if(sha3Cache.keySet().contains(inputByteArray))
return sha3Cache.get(inputByteArray);
byte[] result = SHA3Helper.sha3(input);
sha3Cache.put(inputByteArray, result);
return result;
ByteArrayWrapper inputByteArray = new ByteArrayWrapper(input);
byte[] result = sha3Cache.get(inputByteArray);
if(result != null)
return result;
result = SHA3Helper.sha3(input);
sha3Cache.put(inputByteArray, result);
return result;
}
/**
* Calculates RIGTMOST160(SHA3(input)). This is used in address calculations.
*/
@ -62,7 +61,7 @@ public class HashUtil {
byte[] encSender = RLP.encodeElement(addr);
byte[] encNonce = RLP.encodeElement(nonce);
byte[] newAddress = HashUtil.sha3omit12(RLP.encodeList(encSender, encNonce));
byte[] newAddress = sha3omit12(RLP.encodeList(encSender, encNonce));
return newAddress;
}