From 36dfd3d1b55d04c31c87713d8383dcce37032787 Mon Sep 17 00:00:00 2001 From: Matthew Khouzam Date: Fri, 26 Feb 2016 13:48:00 -0500 Subject: [PATCH] ss: cache TmfStateValue String creation 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 Reviewed-on: https://git.eclipse.org/r/67455 Reviewed-by: Hudson CI Reviewed-by: Alexandre Montplaisir Tested-by: Alexandre Montplaisir --- .../statesystem/core/statevalue/TmfStateValue.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/TmfStateValue.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/TmfStateValue.java index c724410d21..edf8b0f3bc 100644 --- a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/TmfStateValue.java +++ b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/statevalue/TmfStateValue.java @@ -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; } // ------------------------------------------------------------------------ -- 2.34.1