From 70820d7be6f47a57fb1b3dfbf39b1fa574137a06 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sat, 25 Aug 2012 14:44:52 +0000
Subject: [PATCH]  * SDSCache: Reduce min and increase max size  *
 SimpleByteCache: Change from LBQ to ABQ to reduce object churn

---
 core/java/src/net/i2p/data/SDSCache.java      |  4 ++--
 .../src/net/i2p/util/SimpleByteCache.java     | 20 ++++++++++++++++---
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/core/java/src/net/i2p/data/SDSCache.java b/core/java/src/net/i2p/data/SDSCache.java
index 11b730664a..474e964791 100644
--- a/core/java/src/net/i2p/data/SDSCache.java
+++ b/core/java/src/net/i2p/data/SDSCache.java
@@ -45,8 +45,8 @@ public class SDSCache<V extends SimpleDataStructure> {
     //private static final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(SDSCache.class);
 
     private static final Class[] conArg = new Class[] { byte[].class };
-    private static final double MIN_FACTOR = 0.25;
-    private static final double MAX_FACTOR = 3.0;
+    private static final double MIN_FACTOR = 0.20;
+    private static final double MAX_FACTOR = 5.0;
     private static final double FACTOR;
     static {
         long maxMemory = Runtime.getRuntime().maxMemory();
diff --git a/core/java/src/net/i2p/util/SimpleByteCache.java b/core/java/src/net/i2p/util/SimpleByteCache.java
index 4aae936504..01d116abea 100644
--- a/core/java/src/net/i2p/util/SimpleByteCache.java
+++ b/core/java/src/net/i2p/util/SimpleByteCache.java
@@ -2,6 +2,7 @@ package net.i2p.util;
 
 import java.util.Map;
 import java.util.Queue;
+import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.LinkedBlockingQueue;
 
@@ -19,6 +20,9 @@ public final class SimpleByteCache {
 
     private static final int DEFAULT_SIZE = 16;
 
+    /** up to this, use ABQ to minimize object churn and for performance; above this, use LBQ for two locks */
+    private static final int MAX_FOR_ABQ = 64;
+
     /**
      * Get a cache responsible for arrays of the given size
      *
@@ -58,11 +62,11 @@ public final class SimpleByteCache {
     /** list of available and available entries */
     private Queue<byte[]> _available;
     private int _maxCached;
-    private int _entrySize;
+    private final int _entrySize;
     
     private SimpleByteCache(int maxCachedEntries, int entrySize) {
-        _available = new LinkedBlockingQueue(maxCachedEntries);
         _maxCached = maxCachedEntries;
+        _available = createQueue();
         _entrySize = entrySize;
     }
     
@@ -70,13 +74,23 @@ public final class SimpleByteCache {
         if (_maxCached >= maxCachedEntries) return;
         _maxCached = maxCachedEntries;
         // make a bigger one, move the cached items over
-        Queue<byte[]> newLBQ = new LinkedBlockingQueue(maxCachedEntries);
+        Queue<byte[]> newLBQ = createQueue();
         byte[] ba;
         while ((ba = _available.poll()) != null)
             newLBQ.offer(ba);
         _available = newLBQ;
     }
     
+    /**
+     *  @return LBQ or ABQ
+     *  @since 0.9.2
+     */
+    private Queue<byte[]> createQueue() {
+        if (_maxCached <= MAX_FOR_ABQ)
+            return new ArrayBlockingQueue(_maxCached);
+        return new LinkedBlockingQueue(_maxCached);
+    }
+
     /**
      * Get the next available array, either from the cache or a brand new one
      */
-- 
GitLab