nicktelford commented on code in PR #24189:
URL: https://github.com/apache/kafka/pull/24189#discussion_r4540127319


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/NamedCache.java:
##########
@@ -365,10 +367,23 @@ synchronized void close() {
     synchronized void clear() {
         head = tail = null;
         currentSizeBytes = 0;
-        dirtyKeys.clear();
+        resetDirtyKeys();
         cache.clear();
     }
 
+    // HashMap#clear() nulls every slot of a table that never shrinks, so 
clearing a set that once held
+    // many keys costs as much as that peak. Replace the set once the load 
drops well below the peak.
+    private void resetDirtyKeys() {
+        final int size = dirtyKeys.size();
+        dirtyKeysPeak = Math.max(dirtyKeysPeak, size);
+        if (size < dirtyKeysPeak / 4) {

Review Comment:
   @bbejeck 4 is the textbook shrink threshold for a dynamic table: grow when 
full, shrink at 1/4 occupancy. The gap between the two limits stops the table 
from growing and shrinking repeatedly.
   
   Here, the reset clears in place only while the flush holds at least a 
quarter of the peak keys. That limits the slots it zeroes, and the table it 
keeps on the heap, to a small multiple of the current load.
   
   The gap also absorbs normal fluctuation. In the benchmark, flush sizes that 
varied by ±50% never caused a replacement, so a stable high-cardinality store 
keeps its table and avoids a large allocation on each flush. A divisor of 2 
would replace often under that fluctuation, and 8 would double the wasted work 
and memory.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to