perf: use LRUCache for color cache

This commit is contained in:
CrazyboyQCD 2025-09-02 11:11:49 +08:00
parent c4c7649c82
commit c431779d25
2 changed files with 12 additions and 4 deletions

View file

@ -24,6 +24,7 @@ export const overlayCache = new LRUCache<string, any>(500);
export const imageDecodeCache = new LRUCache<string, HTMLImageElement>(64);
export const paletteDetectionCache = new LRUCache<string, boolean>(200);
export const baseMinifyCache = new LRUCache<string, ImageData>(100);
export const colorCaches = new LRUCache<string, Map<number, [number, number, number]>>(500);
export const tooLargeOverlays = new Set<string>();
export function clearOverlayCache() {
@ -31,5 +32,6 @@ export function clearOverlayCache() {
imageDecodeCache.clear();
paletteDetectionCache.clear();
baseMinifyCache.clear();
colorCaches.clear();
tooLargeOverlays.clear();
}

View file

@ -1,7 +1,8 @@
/// <reference types="tampermonkey" />
import { WPLACE_FREE, WPLACE_PAID, WPLACE_NAMES, DEFAULT_FREE_KEYS } from '../core/palette';
import { createCanvas } from '../core/canvas';
import { config, saveConfig } from '../core/store';
import { colorCaches } from '../core/cache';
import { config, saveConfig, type OverlayItem } from '../core/store';
import { MAX_OVERLAY_DIM } from '../core/constants';
import { ensureHook } from '../core/hook';
import { clearOverlayCache, paletteDetectionCache } from '../core/cache';
@ -38,7 +39,7 @@ type CCState = {
selectedPaid: Set<string>;
realtime: boolean;
overlay: any | null;
overlay: OverlayItem | null;
lastColorCounts: Record<string, number>;
isStale: boolean;
};
@ -191,7 +192,7 @@ export function buildCCModal() {
renderPaletteGrid();
}
export function openCCModal(overlay: any) {
export function openCCModal(overlay: OverlayItem) {
if (!cc) return;
cc.overlay = overlay;
@ -266,7 +267,12 @@ function processImage() {
const palette = getActivePalette();
const counts: Record<string, number> = {};
const colorCache: Map<number, [number, number, number]> = new Map();
const id = cc.overlay.id;
let colorCache = colorCaches.get(id);
if (!colorCache) {
colorCache = new Map();
colorCaches.set(id, colorCache);
}
for (let i = 0; i < src.length; i += 4) {
const r = src[i], g = src[i+1], b = src[i+2], a = src[i+3];