Skip to content

Commit 8f2e356

Browse files
committed
Add cache for first entry hash in HashMaps
1 parent fcd3f7b commit 8f2e356

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

convex-core/src/main/java/convex/core/data/AHashMap.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ public AVector<K> getKeys() {
175175
return Vectors.wrap(keys);
176176
}
177177

178+
/**
179+
* Gets the Hash for the first entry. Useful for prefix comparisons etc.
180+
* @return
181+
*/
182+
protected abstract Hash getFirstHash();
183+
178184
@Override
179185
public HashSet<Entry<K, V>> entrySet() {
180186
int len = size();

convex-core/src/main/java/convex/core/data/MapLeaf.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,4 +758,9 @@ public MapLeaf<K, V> slice(long start, long end) {
758758
return new MapLeaf<K,V>(nrefs);
759759
}
760760

761+
@Override
762+
protected Hash getFirstHash() {
763+
return entries[0].getKeyHash();
764+
}
765+
761766
}

convex-core/src/main/java/convex/core/data/MapTree.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,5 +933,14 @@ public boolean isCVMValue() {
933933
return true;
934934
}
935935

936+
// Cache of first hash, we don't want to descend tree repeatedly to find this
937+
private Hash firstHash;
938+
939+
@Override
940+
protected Hash getFirstHash() {
941+
if (firstHash==null) firstHash=children[0].getValue().getFirstHash();
942+
return firstHash;
943+
}
944+
936945

937946
}

0 commit comments

Comments
 (0)