Repository navigation
Description
Problem
src/fl/math/wave/wave_simulation_real.h declares the two grids in WaveSimulation2D_Real as:
fl::vector_psram<i16> grid1;
fl::vector_psram<i16> grid2;vector_psram allocates from PSRAM on every ESP32 that has it. On ESP32-S3 (no L2 cache) this is a perf disaster for the stencil-pattern access of the wave kernel: every cell read pays the full ~80 ns PSRAM access latency, with no cache to amortize it. Back-of-envelope: 64×64 update touches ~80 KB per step → ~1.6 ms per step just on memory at ~50 MB/s practical PSRAM throughput → frame rate cliff at 30 fps.
ESP32-P4 saves us for small-to-medium grids because of its 128 KB L2 cache (a 128×128 i16 grid is 32 KB, still fits). But the moment a user goes to 256×256 they blow L2 and we're back to streaming PSRAM. The current "always PSRAM" decision silently fails on real hardware.
The auto-decision is the worst part: identical-looking user code produces wildly different frame rates on ESP32-S3 vs Teensy 4 vs P4. We need to make storage choice explicit and predictable.
Proposed fix
Default storage is SRAM. PSRAM becomes an explicit, documented opt-in.
// src/fl/math/wave/wave_simulation_real.h
class WaveSimulation2D_Real {
// ... existing API ...
private:
// Default: SRAM. Predictable perf, fits 64×64 trivially everywhere
// useful, fits 128×128 on ESP32-S3 (320 KB SRAM), fits 256×256 on
// ESP32-P4 (~750 KB SRAM). For genuinely larger grids users can
// construct a WaveSimulation2D_Real_Large (below).
fl::vector<i16> grid1;
fl::vector<i16> grid2;
// ...
};
// Optional: explicit PSRAM variant for users with huge grids who accept
// the perf cost. Public, documented, opt-in.
class WaveSimulation2D_Real_Large {
// Identical API, vector_psram<i16> storage.
// Updates take ~5-10x longer per cell on ESP32-S3 due to PSRAM latency.
// Recommended only for grids > 128×128 OR on ESP32-P4 with L2.
};Wrapper class WaveSimulation2D (in wave_simulation.h) currently uses fl::make_unique<WaveSimulation2D_Real>(...) — that path stays unchanged with the new SRAM default. The _Large variant gets a parallel WaveSimulation2D_Large wrapper or a constructor flag.
Alternatives considered (and rejected)
| Option | Why not |
|---|---|
B. Templated Allocator (WaveSimulation2D_Real<Alloc>) |
Doubles template instantiations across the codebase. Harder to debug perf — the type signature doesn't tell you where memory lives. |
C. Size-gated default (SRAM if W*H ≤ N, PSRAM otherwise) |
Same hidden-perf-cliff problem as today, just at a different size threshold. Users still get surprised when they go from N×N to (N+1)×(N+1) and frame rate falls off. |
| D. Runtime probe (try SRAM first, fall back to PSRAM) | Unpredictable. Same code produces different storage on different hardware revisions / heap fragmentation states. |
Option A (the recommended one) trades a small API-surface widening for predictable perf and a clear escape hatch.
Migration notes
This is a public-API-affecting change for users who currently rely on the implicit vector_psram storage:
- Existing users with grids ≤ 64×64: zero impact. They were paying the PSRAM penalty for no reason; now they don't.
- Existing users with grids ~128×128 on S3: default behavior changes — their grid moves from PSRAM into SRAM (8 KB → 32 KB → 32 KB of S3's 320 KB internal SRAM is fine). They get a frame-rate improvement.
- Existing users with grids ≥ 256×256 on S3: they were already PSRAM-bound; their grid no longer fits in SRAM. They hit an
OOMand have to either shrink the grid OR opt intoWaveSimulation2D_Real_Large. The error message should say so explicitly.
A FL_WARN("WaveSimulation2D_Real: grid size W×H requires PSRAM; use WaveSimulation2D_Real_Large or reduce grid size") at the allocation site on OOM gives the user a clear next step.
Acceptance
-
WaveSimulation2D_Realdefault storage isfl::vector<i16>(SRAM). - Public
WaveSimulation2D_Real_Large(or equivalent) provides PSRAM storage as an opt-in. - Wrapper class
WaveSimulation2Dexposes a way to pick between them (constructor flag or parallel wrapper class). - OOM at the SRAM allocation produces a clear error message naming the
_Largevariant. - Perf numbers on ESP32-S3 (from the parent meta's Task 1 benchmark) show SRAM ≥ 3× faster than PSRAM at 64×64.
- Existing examples that use
WaveSimulation2Dstill compile and run with no API churn on the consumer side.
Out of scope
- Adding a templated
Allocatorparameter (Option B above). Stick to the simple-defaults approach. - Per-platform conditional default. SRAM default applies on every platform that has SRAM (i.e. all of them).
- Changes to other
vector_psramusers in the codebase. This issue is scoped toWaveSimulation2D_Realonly.
Cross-references
- Parent meta: [meta] Wave2D perf pipeline — autoresearch benchmark + sanity calibration + PSRAM storage + algorithmic follow-ups #3113
- Audit family: audit(wave/diff-eq): overflow risk + perf wins in WaveSimulation Q15 stencil (behind WaveFx) #3073, [meta] WaveSimulation Q15 stencil — perf + algorithmic follow-ups from #3073 #3098 (both closed)
- Research doc: research(wave): state-of-the-art survey for 2D wave PDE alternatives (closes #3111) #3112 →
agents/docs/research/wave_pde_alternatives.md - PSRAM-vs-cache discussion: see the "PSRAM concern" section of the parent meta
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
feat(autoresearch): add wave2dPerf RPC handler — Task 1 of meta #3113 (…
fix(wave): switch WaveSimulation2D_Real grids from vector_psram to SR…
feat(autoresearch): add --wave2d-perf <W>x<H> host flag (refs #3122 A…
feat(wave): explicit PsramStorage opt-in for WaveSimulation2D_Real (r…