Skip to content

Commit 867d26e

Browse files
committed
Optimize deletion of the last-inserted key in CompactHashStore
* Notably frequent with Truffle::ThreadOperations.detect_pair_recursion.
1 parent 984b4a6 commit 867d26e

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/main/java/org/truffleruby/core/hash/library/CompactHashStore.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ void rehash(RubyHash hash,
332332
@ExportMessage
333333
boolean verify(RubyHash hash) {
334334
assert hash.store == this;
335-
assert kvStoreInsertionPos > 0;
335+
assert kvStoreInsertionPos >= 0;
336336
assert kvStoreInsertionPos <= kvStore.length;
337337
assert kvStoreInsertionPos % 2 == 0;
338338
assert kvStoreInsertionPos >= hash.size; // because deletes only decrease hash.size
@@ -411,10 +411,14 @@ private Object deleteKvAndGetV(RubyHash hash, int indexPos, int keyPos) {
411411

412412
Object deletedValue = kvStore[keyPos + 1];
413413

414-
// TODO: Instead of naively nulling out the key-value, which can produce long gaps of nulls in the kvStore,
414+
// TODO: Instead of naively nulling out the key-value, which can produce long gaps of tombstones in the kvStore,
415415
// See if we can annotate each gap with its length so that iteration code can "jump" over it
416416
kvStore[keyPos] = null;
417417
kvStore[keyPos + 1] = null;
418+
// If removing the last kvStore entry, reset kvStoreInsertionPos before it to avoid extra tombstones
419+
if (keyPos + 2 == kvStoreInsertionPos) {
420+
kvStoreInsertionPos = keyPos;
421+
}
418422

419423
hash.size--;
420424

0 commit comments

Comments
 (0)