From 6c75905008bffc9f09215cf9eaa6fa0d140b25eb Mon Sep 17 00:00:00 2001 From: Larry Booker Date: Thu, 20 Mar 2025 12:22:09 -0700 Subject: [PATCH 1/2] Added error messages instead of recurring while hashing. --- src/main/java/io/deephaven/hash/KHash.java | 16 ++++++++++++++++ .../java/io/deephaven/hash/KeyedObjectHash.java | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/main/java/io/deephaven/hash/KHash.java b/src/main/java/io/deephaven/hash/KHash.java index 5bc148b..acb02df 100644 --- a/src/main/java/io/deephaven/hash/KHash.java +++ b/src/main/java/io/deephaven/hash/KHash.java @@ -210,6 +210,22 @@ protected final void postInsertHook(boolean usedFreeSlot) { // if we've exhausted the free spots, rehash to the same capacity, // which will free up any stale removed slots for reuse. int newCapacity = _size > _maxSize ? PrimeFinder.nextPrime(capacity() << 1) : capacity(); + + // Before rehashing, make sure we have not reduced the capacity below the current size. + if (newCapacity < capacity()) { + throw new IllegalStateException( + "Internal error: newCapacity < capacity, newCapacity=" + + newCapacity + + ", capacity=" + + capacity() + + ", _free=" + + _free + + ", _size=" + + _size + + ", _maxSize=" + + _maxSize); + } + rehash(newCapacity); computeMaxSize(capacity()); } diff --git a/src/main/java/io/deephaven/hash/KeyedObjectHash.java b/src/main/java/io/deephaven/hash/KeyedObjectHash.java index 7e08add..97acfa9 100644 --- a/src/main/java/io/deephaven/hash/KeyedObjectHash.java +++ b/src/main/java/io/deephaven/hash/KeyedObjectHash.java @@ -192,6 +192,21 @@ protected void rehash(int newCapacity) { h.storage = (V[]) new Object[newCapacity]; for (V v : storage) { if (v != null && v != DELETED) { + // Before adding the next value, make sure we won't recurse by verifying h._free > 2 before + // the add + if (h._free <= 2) { + throw new IllegalStateException( + "Internal error: h._free <= 2, newCapacity=" + + newCapacity + + ", h.capacity=" + + h.capacity() + + ", h._free=" + + h._free + + ", h._size=" + + h._size + + ", h._maxSize=" + + h._maxSize); + } h.internalPut(v, NORMAL, null); } } From 608f3fa5db44bee19700b552408fbbcfc1cca337 Mon Sep 17 00:00:00 2001 From: Larry Booker Date: Thu, 20 Mar 2025 15:01:31 -0700 Subject: [PATCH 2/2] Improved error message to include current structure data. --- src/main/java/io/deephaven/hash/KeyedObjectHash.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/io/deephaven/hash/KeyedObjectHash.java b/src/main/java/io/deephaven/hash/KeyedObjectHash.java index 97acfa9..eb68d15 100644 --- a/src/main/java/io/deephaven/hash/KeyedObjectHash.java +++ b/src/main/java/io/deephaven/hash/KeyedObjectHash.java @@ -198,6 +198,14 @@ protected void rehash(int newCapacity) { throw new IllegalStateException( "Internal error: h._free <= 2, newCapacity=" + newCapacity + + ", capacity=" + + capacity() + + ", _free=" + + _free + + ", _size=" + + _size + + ", _maxSize=" + + _maxSize + ", h.capacity=" + h.capacity() + ", h._free="