Skip to content

Commit

Permalink
Add cache for first entry hash in HashMaps
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Oct 8, 2024
1 parent fcd3f7b commit 8f2e356
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions convex-core/src/main/java/convex/core/data/AHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ public AVector<K> getKeys() {
return Vectors.wrap(keys);
}

/**
* Gets the Hash for the first entry. Useful for prefix comparisons etc.
* @return
*/
protected abstract Hash getFirstHash();

@Override
public HashSet<Entry<K, V>> entrySet() {
int len = size();
Expand Down
5 changes: 5 additions & 0 deletions convex-core/src/main/java/convex/core/data/MapLeaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -758,4 +758,9 @@ public MapLeaf<K, V> slice(long start, long end) {
return new MapLeaf<K,V>(nrefs);
}

@Override
protected Hash getFirstHash() {
return entries[0].getKeyHash();
}

}
9 changes: 9 additions & 0 deletions convex-core/src/main/java/convex/core/data/MapTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -933,5 +933,14 @@ public boolean isCVMValue() {
return true;
}

// Cache of first hash, we don't want to descend tree repeatedly to find this
private Hash firstHash;

@Override
protected Hash getFirstHash() {
if (firstHash==null) firstHash=children[0].getValue().getFirstHash();
return firstHash;
}


}

0 comments on commit 8f2e356

Please sign in to comment.