Author: trustin
Date: Thu Nov  8 19:42:48 2007
New Revision: 593416

URL: http://svn.apache.org/viewvc?rev=593416&view=rev
Log:
More strict shrink policy

Modified:
    mina/trunk/core/src/main/java/org/apache/mina/util/CircularQueue.java

Modified: mina/trunk/core/src/main/java/org/apache/mina/util/CircularQueue.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/util/CircularQueue.java?rev=593416&r1=593415&r2=593416&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/util/CircularQueue.java 
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/util/CircularQueue.java Thu 
Nov  8 19:42:48 2007
@@ -44,6 +44,7 @@
     private int first = 0;
     private int last = 0;
     private boolean full;
+    private int shrinkThreshold;
 
     /**
      * Construct a new, empty queue.
@@ -57,6 +58,7 @@
         items = new Object[actualCapacity];
         mask = actualCapacity - 1;
         this.initialCapacity = actualCapacity;
+        this.shrinkThreshold = 0;
     }
 
     private static int normalizeCapacity(int initialCapacity) {
@@ -181,7 +183,8 @@
         if (full) {
             // expand queue
             final int oldLen = items.length;
-            Object[] tmp = new Object[oldLen << 1];
+            final int newLen = oldLen << 1;
+            Object[] tmp = new Object[newLen];
     
             if (first < last) {
                 System.arraycopy(items, first, tmp, 0, last - first);
@@ -194,15 +197,26 @@
             last = oldLen;
             items = tmp;
             mask = tmp.length - 1;
+            if (newLen >>> 3 > initialCapacity) {
+                shrinkThreshold = newLen >>> 3;
+            }
         }
     }
     
     private void shrinkIfNeeded() {
         int size = size();
-        if (size < (capacity() >>> 1)) {
+        if (size < shrinkThreshold) {
             // shrink queue
             final int oldLen = items.length;
             int newLen = normalizeCapacity(size);
+            if (size == newLen) {
+                newLen <<= 1;
+            }
+            
+            if (newLen >= oldLen) {
+                return;
+            }
+            
             if (newLen < initialCapacity) {
                 if (oldLen == initialCapacity) {
                     return;
@@ -224,6 +238,7 @@
             last = size;
             items = tmp;
             mask = tmp.length - 1;
+            shrinkThreshold = 0;
         }
     }
 


Reply via email to