Improve performance matchingNibbleLength

This commit is contained in:
nicksavers 2014-08-03 21:32:33 +02:00
parent a5802b9398
commit 5db586b911
1 changed files with 6 additions and 3 deletions

View File

@ -400,14 +400,18 @@ public class Trie implements TrieFacade {
*******************************/
// Returns the amount of nibbles that match each other from 0 ...
private int matchingNibbleLength(byte[] a, byte[] b) {
public static int matchingNibbleLength(byte[] a, byte[] b) {
int i = 0;
while (Arrays.equals(copyOfRange(a, 0, i+1), copyOfRange(b, 0, i+1)) && i < b.length) {
int length = a.length < b.length ? a.length : b.length;
while (i < length) {
if (a[i] != b[i])
break;
i++;
}
return i;
}
// Created an array of empty elements of requred length
private Object[] emptyStringSlice(int l) {
Object[] slice = new Object[l];
@ -418,7 +422,6 @@ public class Trie implements TrieFacade {
}
public byte[] getRootHash() {
Object root = this.getRoot();
if (root == null
|| (root instanceof byte[] && ((byte[]) root).length == 0)
|| (root instanceof String && "".equals((String) root))) {