From 8b74992e66cdde28617e5930d8d7608d5110b859 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Sun, 24 Sep 2017 09:53:25 +0200 Subject: [PATCH] reduce memory allocations by 25% with a cache of formatted longs --- .../org/lucares/recommind/logs/Plotter.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/pdb-plotting/src/main/java/org/lucares/recommind/logs/Plotter.java b/pdb-plotting/src/main/java/org/lucares/recommind/logs/Plotter.java index f62d0eb..d550e50 100644 --- a/pdb-plotting/src/main/java/org/lucares/recommind/logs/Plotter.java +++ b/pdb-plotting/src/main/java/org/lucares/recommind/logs/Plotter.java @@ -38,6 +38,17 @@ public class Plotter { private static final Logger METRICS_LOGGER = LoggerFactory.getLogger("org.lucares.metrics.plotter"); private static final String DEFAULT_GROUP = ""; + + private static final int INT_TO_STRING_CACHE_SIZE= 1000; + private static final String[] INT_TO_STRING; + static { + + INT_TO_STRING = new String[INT_TO_STRING_CACHE_SIZE]; + + for (int i = 0; i < INT_TO_STRING_CACHE_SIZE; i++){ + INT_TO_STRING[i] = String.valueOf(i); + } + } private final PerformanceDb db; private final Path tmpBaseDir; @@ -234,7 +245,7 @@ public class Plotter { if (fromEpochMilli <= entry.getEpochMilli() && entry.getEpochMilli() <= toEpochMilli) { - final String value = String.valueOf(entry.getValue()); + final String value = longToString(entry.getValue()); final String formattedDate; if (useMillis){ @@ -261,4 +272,13 @@ public class Plotter { METRICS_LOGGER.debug("wrote {} values to csv in: {}ms (ignored {} values) use millis: {}", count, (System.nanoTime() - start) / 1_000_000.0, ignoredValues, Boolean.toString(useMillis)); return new CsvSummary(count, maxValue); } + + private static String longToString(final long value){ + // using pre-generated strings reduces memory allocation by up to 25% + + if (value < INT_TO_STRING_CACHE_SIZE){ + return INT_TO_STRING[(int) value]; + } + return String.valueOf(value); + } }