From ea3ed7e31d02551530af2739da88226b9fb63620 Mon Sep 17 00:00:00 2001 From: extio-de Date: Wed, 17 Jun 2026 14:33:10 +0200 Subject: [PATCH] feat(G2DDrawFont): implement caching for TextLayout to improve performance chore(pom.xml): bump version to 1.0.21 and add Caffeine cache dependency --- pom.xml | 6 +++++- .../g2d/bo/rendering/G2DDrawFont.java | 20 +++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 7b8aaaa..e88b8b9 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 de.extio game-engine - 1.0.20 + 1.0.21 25 @@ -39,6 +39,10 @@ spring-boot-starter-jackson + + com.github.ben-manes.caffeine + caffeine + com.github.luben zstd-jni diff --git a/src/main/java/de/extio/game_engine/renderer/g2d/bo/rendering/G2DDrawFont.java b/src/main/java/de/extio/game_engine/renderer/g2d/bo/rendering/G2DDrawFont.java index 97db77c..c31972a 100644 --- a/src/main/java/de/extio/game_engine/renderer/g2d/bo/rendering/G2DDrawFont.java +++ b/src/main/java/de/extio/game_engine/renderer/g2d/bo/rendering/G2DDrawFont.java @@ -8,10 +8,15 @@ import java.awt.Shape; import java.awt.font.TextAttribute; import java.awt.font.TextLayout; import java.text.AttributedString; +import java.time.Duration; import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; + import de.extio.game_engine.renderer.model.RenderingBo; import de.extio.game_engine.renderer.model.RenderingBoLayer; import de.extio.game_engine.renderer.model.bo.DrawFontRenderingBo; @@ -37,6 +42,11 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende private static final AtomicReference cachedFallbackFontRef = new AtomicReference<>(); private static Font baseFont; + + private static final Cache textLayoutCache = Caffeine.newBuilder() + .maximumSize(250) + .expireAfterAccess(Duration.ofMinutes(1)) + .build(); static { final Map attributes = new HashMap<>(); @@ -250,8 +260,11 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende } private static TextLayout createTextLayout(final String text, final Graphics graphics, final int size_, final double scaleFactor) { - final var attributedString = createAttributedString(text, size_, scaleFactor); - return new TextLayout(attributedString.getIterator(), ((Graphics2D) graphics).getFontRenderContext()); + final var cacheKey = new TextLayoutParameters(text, size_, scaleFactor); + return textLayoutCache.get(cacheKey, key -> { + final var attributedString = createAttributedString(key.text, key.size_, key.scaleFactor); + return new TextLayout(attributedString.getIterator(), ((Graphics2D) graphics).getFontRenderContext()); + }); } private static AttributedString createAttributedString(final String text, final int size_, final double scaleFactor) { @@ -276,5 +289,8 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende } return attributedString; } + + private record TextLayoutParameters(String text, int size_, double scaleFactor) { + } }