feat(G2DDrawFont): implement caching for TextLayout to improve performance
chore(pom.xml): bump version to 1.0.21 and add Caffeine cache dependency
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>de.extio</groupId>
|
<groupId>de.extio</groupId>
|
||||||
<artifactId>game-engine</artifactId>
|
<artifactId>game-engine</artifactId>
|
||||||
<version>1.0.20</version>
|
<version>1.0.21</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>25</java.version>
|
<java.version>25</java.version>
|
||||||
@@ -39,6 +39,10 @@
|
|||||||
<artifactId>spring-boot-starter-jackson</artifactId>
|
<artifactId>spring-boot-starter-jackson</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||||
|
<artifactId>caffeine</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.luben</groupId>
|
<groupId>com.github.luben</groupId>
|
||||||
<artifactId>zstd-jni</artifactId>
|
<artifactId>zstd-jni</artifactId>
|
||||||
|
|||||||
@@ -8,10 +8,15 @@ import java.awt.Shape;
|
|||||||
import java.awt.font.TextAttribute;
|
import java.awt.font.TextAttribute;
|
||||||
import java.awt.font.TextLayout;
|
import java.awt.font.TextLayout;
|
||||||
import java.text.AttributedString;
|
import java.text.AttributedString;
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
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.RenderingBo;
|
||||||
import de.extio.game_engine.renderer.model.RenderingBoLayer;
|
import de.extio.game_engine.renderer.model.RenderingBoLayer;
|
||||||
import de.extio.game_engine.renderer.model.bo.DrawFontRenderingBo;
|
import de.extio.game_engine.renderer.model.bo.DrawFontRenderingBo;
|
||||||
@@ -37,6 +42,11 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende
|
|||||||
private static final AtomicReference<Font> cachedFallbackFontRef = new AtomicReference<>();
|
private static final AtomicReference<Font> cachedFallbackFontRef = new AtomicReference<>();
|
||||||
|
|
||||||
private static Font baseFont;
|
private static Font baseFont;
|
||||||
|
|
||||||
|
private static final Cache<TextLayoutParameters, TextLayout> textLayoutCache = Caffeine.newBuilder()
|
||||||
|
.maximumSize(250)
|
||||||
|
.expireAfterAccess(Duration.ofMinutes(1))
|
||||||
|
.build();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
final Map<TextAttribute, Object> attributes = new HashMap<>();
|
final Map<TextAttribute, Object> 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) {
|
private static TextLayout createTextLayout(final String text, final Graphics graphics, final int size_, final double scaleFactor) {
|
||||||
final var attributedString = createAttributedString(text, size_, scaleFactor);
|
final var cacheKey = new TextLayoutParameters(text, size_, scaleFactor);
|
||||||
return new TextLayout(attributedString.getIterator(), ((Graphics2D) graphics).getFontRenderContext());
|
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) {
|
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;
|
return attributedString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private record TextLayoutParameters(String text, int size_, double scaleFactor) {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user