From 8f2e356db0525fb6674b6639a671f0a423cfec65 Mon Sep 17 00:00:00 2001 From: mikera Date: Tue, 8 Oct 2024 06:15:23 +0100 Subject: [PATCH] Add cache for first entry hash in HashMaps --- convex-core/src/main/java/convex/core/data/AHashMap.java | 6 ++++++ convex-core/src/main/java/convex/core/data/MapLeaf.java | 5 +++++ convex-core/src/main/java/convex/core/data/MapTree.java | 9 +++++++++ 3 files changed, 20 insertions(+) diff --git a/convex-core/src/main/java/convex/core/data/AHashMap.java b/convex-core/src/main/java/convex/core/data/AHashMap.java index 4472eb928..7e71cb0f6 100644 --- a/convex-core/src/main/java/convex/core/data/AHashMap.java +++ b/convex-core/src/main/java/convex/core/data/AHashMap.java @@ -175,6 +175,12 @@ public AVector 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> entrySet() { int len = size(); diff --git a/convex-core/src/main/java/convex/core/data/MapLeaf.java b/convex-core/src/main/java/convex/core/data/MapLeaf.java index 80917042a..b19047e7d 100644 --- a/convex-core/src/main/java/convex/core/data/MapLeaf.java +++ b/convex-core/src/main/java/convex/core/data/MapLeaf.java @@ -758,4 +758,9 @@ public MapLeaf slice(long start, long end) { return new MapLeaf(nrefs); } + @Override + protected Hash getFirstHash() { + return entries[0].getKeyHash(); + } + } diff --git a/convex-core/src/main/java/convex/core/data/MapTree.java b/convex-core/src/main/java/convex/core/data/MapTree.java index df15f3556..db3131ebf 100644 --- a/convex-core/src/main/java/convex/core/data/MapTree.java +++ b/convex-core/src/main/java/convex/core/data/MapTree.java @@ -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; + } + }