Remove unnecessary hash cache from PatienceDiffIndex

PatienceDiff always uses a HashedSequence, which promises to provide
constant time access for hash codes during the equals method and
aborts fast if the hash codes don't match.  Therefore we don't need
to cache the hash codes inside of the index, saving us memory.

Change-Id: I80bf1e95094b7670e6c0acc26546364a1012d60e
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
This commit is contained in:
Shawn O. Pearce 2010-09-21 13:53:56 -07:00
parent a67afbfee1
commit e84d826eb6
1 changed files with 15 additions and 40 deletions

View File

@ -100,9 +100,6 @@ final class PatienceDiffIndex<S extends Sequence> {
// arrays. This permits us to get 3 values per entry, without paying // arrays. This permits us to get 3 values per entry, without paying
// the penalty for an object header on each entry. // the penalty for an object header on each entry.
/** Cached hash value for an element as returned by {@link #cmp}. */
private final int[] hash;
/** /**
* A matched (or partially examined) element from the two sequences. * A matched (or partially examined) element from the two sequences.
* *
@ -160,26 +157,15 @@ final class PatienceDiffIndex<S extends Sequence> {
this.pBegin = pIdx; this.pBegin = pIdx;
this.pEnd = pCnt; this.pEnd = pCnt;
final int blockCnt = region.getLengthB(); final int sz = region.getLengthB();
if (blockCnt < 1) { table = new int[tableSize(sz)];
table = new int[] {}; tableMask = table.length - 1;
tableMask = 0;
hash = new int[] {}; // As we insert elements we preincrement so that 0 is never a
ptrs = new long[] {}; // valid entry. Therefore we have to allocate one extra space.
next = new int[] {}; //
ptrs = new long[1 + sz];
} else { next = new int[ptrs.length];
table = new int[tableSize(blockCnt)];
tableMask = table.length - 1;
// As we insert elements we preincrement so that 0 is never a
// valid entry. Therefore we have to allocate one extra space.
//
hash = new int[1 + blockCnt];
ptrs = new long[hash.length];
next = new int[hash.length];
}
} }
/** /**
@ -201,8 +187,7 @@ private void scanB() {
final int end = region.endB; final int end = region.endB;
int pIdx = pBegin; int pIdx = pBegin;
SCAN: while (ptr < end) { SCAN: while (ptr < end) {
final int key = cmp.hash(b, ptr); final int tIdx = cmp.hash(b, ptr) & tableMask;
final int tIdx = key & tableMask;
if (pIdx < pEnd) { if (pIdx < pEnd) {
final long priorRec = pCommon[pIdx]; final long priorRec = pCommon[pIdx];
@ -210,7 +195,7 @@ private void scanB() {
// We know this region is unique from a prior pass. // We know this region is unique from a prior pass.
// Insert the start point, and skip right to the end. // Insert the start point, and skip right to the end.
// //
insertB(key, tIdx, ptr); insertB(tIdx, ptr);
pIdx++; pIdx++;
ptr = aOfRaw(priorRec); ptr = aOfRaw(priorRec);
continue SCAN; continue SCAN;
@ -222,9 +207,6 @@ private void scanB() {
// was already a different entry present. // was already a different entry present.
// //
for (int eIdx = table[tIdx]; eIdx != 0; eIdx = next[eIdx]) { for (int eIdx = table[tIdx]; eIdx != 0; eIdx = next[eIdx]) {
if (hash[eIdx] != key)
continue;
final long rec = ptrs[eIdx]; final long rec = ptrs[eIdx];
if (cmp.equals(b, ptr, b, bOf(rec))) { if (cmp.equals(b, ptr, b, bOf(rec))) {
ptrs[eIdx] = rec | B_DUPLICATE; ptrs[eIdx] = rec | B_DUPLICATE;
@ -233,14 +215,13 @@ private void scanB() {
} }
} }
insertB(key, tIdx, ptr); insertB(tIdx, ptr);
ptr++; ptr++;
} }
} }
private void insertB(final int key, final int tIdx, int ptr) { private void insertB(final int tIdx, int ptr) {
final int eIdx = ++entryCnt; final int eIdx = ++entryCnt;
hash[eIdx] = key;
ptrs[eIdx] = ((long) ptr) << B_SHIFT; ptrs[eIdx] = ((long) ptr) << B_SHIFT;
next[eIdx] = table[tIdx]; next[eIdx] = table[tIdx];
table[tIdx] = eIdx; table[tIdx] = eIdx;
@ -263,13 +244,13 @@ private void scanA() {
final int end = region.endA; final int end = region.endA;
int pLast = pBegin - 1; int pLast = pBegin - 1;
SCAN: while (ptr < end) { SCAN: while (ptr < end) {
final int key = cmp.hash(a, ptr); final int tIdx = cmp.hash(a, ptr) & tableMask;
final int tIdx = key & tableMask;
for (int eIdx = table[tIdx]; eIdx != 0; eIdx = next[eIdx]) { for (int eIdx = table[tIdx]; eIdx != 0; eIdx = next[eIdx]) {
final long rec = ptrs[eIdx]; final long rec = ptrs[eIdx];
final int bs = bOf(rec);
if (isDuplicate(rec) || hash[eIdx] != key) if (isDuplicate(rec) || !cmp.equals(a, ptr, b, bs))
continue; continue;
final int aPtr = aOfRaw(rec); final int aPtr = aOfRaw(rec);
@ -280,12 +261,6 @@ private void scanA() {
continue SCAN; continue SCAN;
} }
final int bs = bOf(rec);
if (!cmp.equals(a, ptr, b, bs)) {
ptr++;
continue SCAN;
}
// This element is both common and unique. Link the // This element is both common and unique. Link the
// two sequences together at this point. // two sequences together at this point.
// //