You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/06/05 16:54:55 UTC

[GitHub] [pulsar] merlimat commented on a diff in pull request #15936: Support shrink for TripleLongPriorityQueue

merlimat commented on code in PR #15936:
URL: https://github.com/apache/pulsar/pull/15936#discussion_r889712113


##########
pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/TripleLongPriorityQueue.java:
##########
@@ -152,6 +174,21 @@ private void increaseCapacity() {
         buffer.capacity(this.capacity * TUPLE_SIZE);
     }
 
+    private void shrinkCapacity() {
+        if (capacity > initialCapacity &&  size < capacity * shrinkFactor) {

Review Comment:
   We could cache the value of `capacity * shrinkFactor` to avoid computing it each time we're removing an item



##########
pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/TripleLongPriorityQueue.java:
##########
@@ -152,6 +174,21 @@ private void increaseCapacity() {
         buffer.capacity(this.capacity * TUPLE_SIZE);
     }
 
+    private void shrinkCapacity() {
+        if (capacity > initialCapacity &&  size < capacity * shrinkFactor) {
+            int decreasingSize = (int) (capacity * shrinkFactor * RESERVATION_FACTOR);
+            if (decreasingSize <= 0) {
+                return;
+            }
+            if (capacity - decreasingSize <= initialCapacity) {
+                this.capacity = initialCapacity;
+            } else {
+                this.capacity = capacity - decreasingSize;
+            }
+            buffer.capacity(this.capacity * TUPLE_SIZE);

Review Comment:
   I think this will truncate the buffer, but it will not release the underlying memory back to the pool: https://netty.io/4.1/api/io/netty/buffer/ByteBuf.html#capacity-int- 
   
   We'd probably have to allocate a new buffer and copy the content into it.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org