Skip to content

Commit

Permalink
only registers cleaner in native map when needed
Browse files Browse the repository at this point in the history
Registering cleaners takes up resources and currently bumps into a
global lock. The native map code is registering a cleaner for iterators,
even in the case where the native code did not allocate an iterator.
This code is called very frequently by scan threads.

This change does not register the cleaner in the case where the native
code did not allocate a native iterator.
  • Loading branch information
keith-turner committed Sep 19, 2024
1 parent b93939d commit 8325287
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,13 @@ private class NMIterator implements Iterator<Map.Entry<Key,Value>> {
hasNext = nmiPointer != 0;

nmiPtr.set(nmiPointer);
cleanableNMI = NativeMapCleanerUtil.deleteNMIterator(this, nmiPtr);
if (nmiPointer == 0) {
// registering a cleaner takes resources and may lock, so only bother registering if there
// is something to delete
cleanableNMI = null;
} else {
cleanableNMI = NativeMapCleanerUtil.deleteNMIterator(this, nmiPtr);
}
}

// delete is synchronized on a per iterator basis want to ensure only one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,14 @@ public void testEmpty() {
assertEquals(0, nm.size());
assertEquals(0, nm.getMemoryUsed());

var iter1 = nm.iterator();
assertFalse(iter1.hasNext());
assertThrows(NoSuchElementException.class, () -> iter1.next());

var iter2 = nm.iterator(new Key("abc"));
assertFalse(iter2.hasNext());
assertThrows(NoSuchElementException.class, () -> iter2.next());

nm.delete();
}

Expand Down

0 comments on commit 8325287

Please sign in to comment.