ss: cache TmfStateValue String creation
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Fri, 26 Feb 2016 18:48:00 +0000 (13:48 -0500)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 7 Jul 2016 18:44:07 +0000 (14:44 -0400)
This patch adds a direct mapped cache to the TmfStateValue#newValueString
method. This method is used heavily in the Lttng Kernel State Provider.

Using KernelAnalysisBenchmark, this patch increases the performance by 5-10%

Change-Id: Ib0bece8eb606186086cbb4843944e438cf5fbf98
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/67455
Reviewed-by: Hudson CI
Reviewed-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Tested-by: Alexandre Montplaisir <alexmonthy@efficios.com>
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/TmfStateValue.java

index c724410d21706952447b59424e33a6d8411b5bf1..edf8b0f3bc5a993628005e82066fd80181459054 100644 (file)
@@ -37,10 +37,12 @@ public abstract class TmfStateValue implements ITmfStateValue {
     private static final int INT_CACHE_SIZE = 128;
     private static final int LONG_CACHE_SIZE = 128;
     private static final int DOUBLE_CACHE_SIZE = 128;
+    private static final int STRING_CACHE_SIZE = 512;
 
     private static final IntegerStateValue intCache[] = new IntegerStateValue[INT_CACHE_SIZE];
     private static final LongStateValue longCache[] = new LongStateValue[LONG_CACHE_SIZE];
     private static final DoubleStateValue doubleCache[] = new DoubleStateValue[DOUBLE_CACHE_SIZE];
+    private static final StringStateValue STRING_CACHE[] = new StringStateValue[STRING_CACHE_SIZE];
 
     // ------------------------------------------------------------------------
     // Factory methods to instantiate new state values
@@ -140,6 +142,11 @@ public abstract class TmfStateValue implements ITmfStateValue {
         if (strValue == null) {
             return nullValue();
         }
+        int offset = strValue.hashCode() & (LONG_CACHE_SIZE - 1);
+        StringStateValue cached = STRING_CACHE[offset];
+        if (cached != null && cached.unboxStr().equals(strValue)) {
+            return cached;
+        }
         /*
          * Make sure the String does not contain "weird" things, like ISO
          * control characters.
@@ -150,7 +157,9 @@ public abstract class TmfStateValue implements ITmfStateValue {
                 throw new IllegalArgumentException();
             }
         }
-        return new StringStateValue(strValue);
+        StringStateValue newValue = new StringStateValue(strValue);
+        STRING_CACHE[offset] = newValue;
+        return newValue;
     }
 
     // ------------------------------------------------------------------------
This page took 0.026331 seconds and 5 git commands to generate.