Heuristic:777genius Agent teams ai Render Cache
| Knowledge Sources | |
|---|---|
| Domains | Rendering, Optimization |
| Last Updated | 2026-10-22 00:00 GMT |
Overview
Pre-render expensive Canvas 2D operations (radial gradients, text measurements, color resolution) into cached sprites and lookup tables to avoid repeated computation per frame.
Description
The agent graph Canvas rendering uses multiple caches to avoid redundant work per frame: a glow sprite cache (pre-rendered radial gradients on offscreen canvases), a text width measurement cache (LRU with 2000 entries), a color resolution cache (named colors to hex), and a hex+alpha cache (quantized to 1/255 steps for cache hit rate). These caches are populated lazily on first use and reused across frames. The text cache evicts at 2000 entries; the hex+alpha cache clears entirely at 500 entries to prevent unbounded growth.
Usage
Use this heuristic for any Canvas 2D rendering that performs expensive operations per frame: gradient creation, text measurement, color parsing. Cache the results and reuse them. This applies to the agent graph rendering (agents, tasks, edges, effects).
The Insight (Rule of Thumb)
- Action: Cache pre-rendered sprites (offscreen canvases) for gradients. Cache `ctx.measureText()` results. Cache color resolution. Quantize alpha for better cache hit rate.
- Value: Text cache: 2000 entries (clear on overflow). Hex+alpha cache: 500 entries (clear on overflow). Glow sprites: unbounded (unique per color+radius combo).
- Trade-off: Memory usage increases with cache size. Glow sprites are offscreen canvases (each ~size² pixels). Clear-all on overflow is simpler than LRU eviction.
- Compatibility: Canvas 2D only. Not applicable to WebGL rendering.
Reasoning
Creating a `CanvasRenderingContext2D.createRadialGradient()` on every frame for every agent's glow effect would be extremely expensive — each call allocates a new gradient object and the GPU must upload it. Pre-rendering the gradient to an offscreen canvas and reusing it via `drawImage()` is dramatically faster. Similarly, `ctx.measureText()` is a layout operation that shouldn't repeat for the same text. The alpha quantization (rounding to 1/255 steps) increases cache hits because nearby float alpha values map to the same key.
Code Evidence
Glow sprite cache from `render-cache.ts:6-8`:
const glowCache = new Map<string, HTMLCanvasElement>();
const textCache = new Map<string, number>();
const TEXT_CACHE_LIMIT = 2000;
Text measurement cache from `render-cache.ts:123-136`:
export function measureTextCached(ctx: CanvasRenderingContext2D, font: string, text: string): number {
const key = `${font}|${text}`;
let w = textCache.get(key);
if (w !== undefined) return w;
if (textCache.size > TEXT_CACHE_LIMIT) textCache.clear();
const prevFont = ctx.font;
ctx.font = font;
w = ctx.measureText(text).width;
ctx.font = prevFont;
textCache.set(key, w);
return w;
}
Alpha quantization from `render-cache.ts:42-51`:
function hexWithAlpha(color: string, alpha: number): string {
// Quantize alpha to 1/255 steps for cache hit rate
const a = Math.round(Math.max(0, Math.min(1, alpha)) * 255);
const key = `${color}|${a}`;
let result = _hexAlphaCache.get(key);
if (result) return result;
result = ensureHex(color) + alphaHex(a / 255);
_hexAlphaCache.set(key, result);
if (_hexAlphaCache.size > 500) _hexAlphaCache.clear();
return result;
}