Skip to content

fix: replace naive FIFO with LRU cache eviction for WebGL retained geometry (fixes #8597)#8598

Open
MASTERsj01 wants to merge 1 commit intoprocessing:mainfrom
MASTERsj01:fix/webgl-geometry-cache-lru
Open

fix: replace naive FIFO with LRU cache eviction for WebGL retained geometry (fixes #8597)#8598
MASTERsj01 wants to merge 1 commit intoprocessing:mainfrom
MASTERsj01:fix/webgl-geometry-cache-lru

Conversation

@MASTERsj01
Copy link

Summary

Replaces the naive FIFO geometry cache eviction in p5.RendererGL.Retained.js with a proper LRU (Least Recently Used) strategy that prevents silent rendering corruption.

Resolves #8597

Problem

The retained geometry cache had a hard-coded 1000-entry limit (with a long-standing @TODO comment) that always evicted the oldest created geometry via Object.keys()[0], regardless of whether it was still being actively drawn. This caused shapes to silently disappear in complex scenes.

Changes

Change Detail
LRU eviction Replaced FIFO with least-recently-used strategy
Usage tracking Added _lastUsed timestamp via performance.now() in drawBuffers()
Smart eviction Now evicts geometry that hasn't been drawn recently, not just oldest created
FES migration Converted console.warnp5._friendlyError() in freeGeometry()

Before vs After

Before: Cache full → delete Object.keys()[0] (oldest created, possibly still drawn) → silent rendering failure

After: Cache full → find entry with smallest _lastUsed → delete least recently drawn → active shapes preserved

Notes

  • All existing tests pass (1913 passing)
  • 1 file changed: 33 insertions, 14 deletions

The retained geometry cache in p5.RendererGL.Retained.js had a
hard-coded 1000-entry limit with naive FIFO eviction that always
deleted the first (oldest created) geometry, regardless of whether
it was still being actively drawn. This could cause shapes to
silently disappear in complex scenes with many unique geometries.

Changes:
- Implement LRU (Least Recently Used) cache eviction strategy
- Track geometry usage via _lastUsed timestamp in drawBuffers()
- Evict least recently drawn geometry instead of oldest created
- Replace console.warn with p5._friendlyError in freeGeometry()

This addresses a long-standing TODO in the codebase and prevents
silent rendering corruption when the geometry cache is full.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebGL retained geometry cache uses naive FIFO eviction, causing silent rendering corruption

1 participant