Author: mrglavas
Date: Tue Aug  9 14:51:31 2011
New Revision: 1155386

URL: http://svn.apache.org/viewvc?rev=1155386&view=rev
Log:
JIRA Issue #1521: http://issues.apache.org/jira/browse/XERCESJ-1521. Compact 
the SoftReferenceSymbolTable if after cleaning out cleared SoftReferences the 
number of symbols drops below 25% of the table's load factor threshold.

Modified:
    xerces/java/trunk/src/org/apache/xerces/util/SoftReferenceSymbolTable.java

Modified: 
xerces/java/trunk/src/org/apache/xerces/util/SoftReferenceSymbolTable.java
URL: 
http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/util/SoftReferenceSymbolTable.java?rev=1155386&r1=1155385&r2=1155386&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/util/SoftReferenceSymbolTable.java 
(original)
+++ xerces/java/trunk/src/org/apache/xerces/util/SoftReferenceSymbolTable.java 
Tue Aug  9 14:51:31 2011
@@ -203,12 +203,26 @@ public class SoftReferenceSymbolTable ex
      * and load factor. 
      */
     protected void rehash() {
-
-        int oldCapacity = fBuckets.length;
-        SREntry[] oldTable = fBuckets;
-
-        int newCapacity = oldCapacity * 2 + 1;
-        SREntry[] newTable = new SREntry[newCapacity];
+        rehashCommon(fBuckets.length * 2 + 1);
+    }
+    
+    /**
+     * Reduces the capacity of and internally reorganizes this 
+     * SymbolTable, in order to accommodate and access its entries in
+     * a more memory efficient way. This method is called automatically when 
+     * the number of keys in the SymbolTable drops below 25% of this
+     * hashtable's load factor (as a result of SoftReferences which have
+     * been cleared).
+     */
+    protected void compact() {
+        rehashCommon(((int) (fCount / fLoadFactor)) * 2 + 1);
+    }
+    
+    private void rehashCommon(final int newCapacity) {
+        
+        final int oldCapacity = fBuckets.length;
+        final SREntry[] oldTable = fBuckets;
+        final SREntry[] newTable = new SREntry[newCapacity];
 
         fThreshold = (int)(newCapacity * fLoadFactor);
         fBuckets = newTable;
@@ -321,9 +335,17 @@ public class SoftReferenceSymbolTable ex
      */
     private void clean() {
         SREntry entry = (SREntry)fReferenceQueue.poll();
-        while (entry != null) {
-            removeEntry(entry);
-            entry = (SREntry)fReferenceQueue.poll();
+        if (entry != null) {
+            do {
+                removeEntry(entry);
+                entry = (SREntry)fReferenceQueue.poll();
+            }
+            while (entry != null);
+            // Reduce the number of buckets if the number of items
+            // in the table has dropped below 25% of the threshold.
+            if (fCount < (fThreshold >> 2)) {
+                compact();
+            }
         }
     }
         



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@xerces.apache.org
For additional commands, e-mail: commits-h...@xerces.apache.org

Reply via email to