From 3d24b444a27453894f61c65410391913b493a87f Mon Sep 17 00:00:00 2001 From: arcturus2 <39325530+arcturus2@users.noreply.github.com> Date: Thu, 25 Mar 2021 00:35:18 +1100 Subject: [PATCH 1/2] Trying using the Caffeine cache --- CoreDependencies/nbproject/project.xml | 17 +++++++++++++---- CoreDependencies/src/ivy.xml | 1 + .../opengl/utilities/glyphs/GlyphManagerBI.java | 14 +++++++++++--- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CoreDependencies/nbproject/project.xml b/CoreDependencies/nbproject/project.xml index 81dde6fcd9..df7272b063 100644 --- a/CoreDependencies/nbproject/project.xml +++ b/CoreDependencies/nbproject/project.xml @@ -73,6 +73,8 @@ com.fasterxml.jackson.databind.ser.std com.fasterxml.jackson.databind.type com.fasterxml.jackson.databind.util + com.github.benmanes.caffeine.cache + com.github.benmanes.caffeine.cache.stats com.google.common.annotations com.google.common.base com.google.common.base.internal @@ -1192,6 +1194,8 @@ org.assertj.core.util.diff.myers org.assertj.core.util.introspection org.assertj.core.util.xml + org.checkerframework.checker.builder.qual + org.checkerframework.checker.calledmethods.qual org.checkerframework.checker.compilermsgs.qual org.checkerframework.checker.fenum.qual org.checkerframework.checker.formatter @@ -1217,6 +1221,7 @@ org.checkerframework.checker.units org.checkerframework.checker.units.qual org.checkerframework.common.aliasing.qual + org.checkerframework.common.initializedfields.qual org.checkerframework.common.reflection.qual org.checkerframework.common.returnsreceiver.qual org.checkerframework.common.subtyping.qual @@ -2307,8 +2312,12 @@ release/modules/ext/bigint-0.7.1-jar.jar - ext/checker-qual-3.5.0-jar.jar - release/modules/ext/checker-qual-3.5.0-jar.jar + ext/caffeine-3.0.1-jar.jar + release/modules/ext/caffeine-3.0.1-jar.jar + + + ext/checker-qual-3.11.0-jar.jar + release/modules/ext/checker-qual-3.11.0-jar.jar ext/commons-codec-1.13-jar.jar @@ -2403,8 +2412,8 @@ release/modules/ext/ejml-zdense-0.40-jar.jar - ext/error_prone_annotations-2.3.4-jar.jar - release/modules/ext/error_prone_annotations-2.3.4-jar.jar + ext/error_prone_annotations-2.5.1-jar.jar + release/modules/ext/error_prone_annotations-2.5.1-jar.jar ext/failureaccess-1.0.1-bundle.jar diff --git a/CoreDependencies/src/ivy.xml b/CoreDependencies/src/ivy.xml index 5df1e39de2..a60e0adb63 100644 --- a/CoreDependencies/src/ivy.xml +++ b/CoreDependencies/src/ivy.xml @@ -83,6 +83,7 @@ + diff --git a/CoreOpenGLDisplay/src/au/gov/asd/tac/constellation/visual/opengl/utilities/glyphs/GlyphManagerBI.java b/CoreOpenGLDisplay/src/au/gov/asd/tac/constellation/visual/opengl/utilities/glyphs/GlyphManagerBI.java index 286afb18be..db1e6c2399 100644 --- a/CoreOpenGLDisplay/src/au/gov/asd/tac/constellation/visual/opengl/utilities/glyphs/GlyphManagerBI.java +++ b/CoreOpenGLDisplay/src/au/gov/asd/tac/constellation/visual/opengl/utilities/glyphs/GlyphManagerBI.java @@ -15,6 +15,8 @@ */ package au.gov.asd.tac.constellation.visual.opengl.utilities.glyphs; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; @@ -38,6 +40,7 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Optional; +import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; @@ -124,7 +127,8 @@ public LigatureContext(final List glyphRectangles, final int lef * Cache the bulk of the work renderTextAsLigature does to greatly improve * performance. */ - private static Map cache; +// private static Map cache; + private static LoadingCache cache; /** * A default no-op GlyphStream to use when the user specifies null. @@ -181,7 +185,11 @@ public GlyphManagerBI(final FontInfo[] fontsInfo, final int textureBufferSize, f drawIndividual = false; drawCombined = false; - cache = new HashMap<>(); +// cache = new HashMap<>(); + cache = Caffeine.newBuilder() + .expireAfterWrite(1, TimeUnit.HOURS) + .build(key -> buildLigature(key)); + } public BufferedImage getImage() { @@ -358,7 +366,7 @@ public void renderTextAsLigatures(final String text, GlyphStream glyphStream, Gl // time. Guava caching was attempted though it was slower and negating // the performance improvements of caching. // - if (!cache.containsKey(text)) { + if (cache.get(text)!=null) { cache.put(text, buildLigature(text)); } final LigatureContext ligature = cache.get(text); From 419979e3db61bd0e114f1a318e25372ab01cbd19 Mon Sep 17 00:00:00 2001 From: arcturus2 <39325530+arcturus2@users.noreply.github.com> Date: Thu, 25 Mar 2021 00:47:30 +1100 Subject: [PATCH 2/2] Fix a typo and remove old code --- .../opengl/utilities/glyphs/GlyphManagerBI.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/CoreOpenGLDisplay/src/au/gov/asd/tac/constellation/visual/opengl/utilities/glyphs/GlyphManagerBI.java b/CoreOpenGLDisplay/src/au/gov/asd/tac/constellation/visual/opengl/utilities/glyphs/GlyphManagerBI.java index db1e6c2399..d30db8af86 100644 --- a/CoreOpenGLDisplay/src/au/gov/asd/tac/constellation/visual/opengl/utilities/glyphs/GlyphManagerBI.java +++ b/CoreOpenGLDisplay/src/au/gov/asd/tac/constellation/visual/opengl/utilities/glyphs/GlyphManagerBI.java @@ -127,7 +127,6 @@ public LigatureContext(final List glyphRectangles, final int lef * Cache the bulk of the work renderTextAsLigature does to greatly improve * performance. */ -// private static Map cache; private static LoadingCache cache; /** @@ -185,9 +184,8 @@ public GlyphManagerBI(final FontInfo[] fontsInfo, final int textureBufferSize, f drawIndividual = false; drawCombined = false; -// cache = new HashMap<>(); cache = Caffeine.newBuilder() - .expireAfterWrite(1, TimeUnit.HOURS) + .expireAfterWrite(1, TimeUnit.HOURS) // TODO: make this configurable .build(key -> buildLigature(key)); } @@ -361,14 +359,8 @@ public void renderTextAsLigatures(final String text, GlyphStream glyphStream, Gl // Retrieve the LigatureContext from the cache to greatly speed up // building these ligatures which are built every time the graph is - // loaded or when the graph structure changes. Note that items are not - // purged from this cache so there is a small build up of memory over - // time. Guava caching was attempted though it was slower and negating - // the performance improvements of caching. + // loaded or when the graph structure changes. // - if (cache.get(text)!=null) { - cache.put(text, buildLigature(text)); - } final LigatureContext ligature = cache.get(text); // Add the background for this text.