From edc1303af16e8dea9243818dc744f521310b3942 Mon Sep 17 00:00:00 2001 From: Autumn Lamonte Date: Fri, 4 Feb 2022 18:25:57 -0600 Subject: [PATCH] cache kitty --- src/jexer/backend/ECMA48Terminal.java | 37 ++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/jexer/backend/ECMA48Terminal.java b/src/jexer/backend/ECMA48Terminal.java index 7dfadf8..c2a2b52 100644 --- a/src/jexer/backend/ECMA48Terminal.java +++ b/src/jexer/backend/ECMA48Terminal.java @@ -272,6 +272,11 @@ public class ECMA48Terminal extends LogicalScreen */ private boolean kittyImages = false; + /** + * The Kitty post-rendered string cache. + */ + private ImageCache kittyCache = null; + /** * The number of threads for image rendering. */ @@ -1873,6 +1878,10 @@ public class ECMA48Terminal extends LogicalScreen if (jexerCache == null) { jexerCache = new ImageCache(height * width * 10); } + } else if (kittyImages) { + if (kittyCache == null) { + kittyCache = new ImageCache(height * width * 10); + } } else { if (sixelCache == null) { sixelCache = new ImageCache(height * width * 10); @@ -4240,7 +4249,28 @@ public class ECMA48Terminal extends LogicalScreen assert (cells.size() > 0); assert (cells.get(0).getImage() != null); - // We don't cache Kitty images at this time. + // Save and get rows to/from the cache that do NOT have inverted + // cells. + boolean saveInCache = true; + for (Cell cell: cells) { + if (cell.isInvertedImage()) { + saveInCache = false; + break; + } + // Compute the hashcode so that the cell image hash is available + // for looking up in the image cache. + cell.hashCode(); + } + if (saveInCache) { + String cachedResult = kittyCache.get(cells); + if (cachedResult != null) { + // System.err.println("CACHE HIT"); + sb.append(gotoXY(x, y)); + sb.append(cachedResult); + return sb.toString(); + } + // System.err.println("CACHE MISS"); + } BufferedImage image = cellsToImage(cells); int fullHeight = image.getHeight(); @@ -4289,6 +4319,11 @@ public class ECMA48Terminal extends LogicalScreen } } + if (saveInCache) { + // This row is OK to save into the cache. + kittyCache.put(cells, sb.toString()); + } + return (gotoXY(x, y) + sb.toString()); }