Skip to content

fix(wave): remove silent vector_psram default in WaveSimulation2D_Real — SRAM by default, PSRAM opt-in #3114

Closed

Description

@zackees

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:

  1. Existing users with grids ≤ 64×64: zero impact. They were paying the PSRAM penalty for no reason; now they don't.
  2. 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.
  3. Existing users with grids ≥ 256×256 on S3: they were already PSRAM-bound; their grid no longer fits in SRAM. They hit an OOM and have to either shrink the grid OR opt into WaveSimulation2D_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_Real default storage is fl::vector<i16> (SRAM).
  • Public WaveSimulation2D_Real_Large (or equivalent) provides PSRAM storage as an opt-in.
  • Wrapper class WaveSimulation2D exposes a way to pick between them (constructor flag or parallel wrapper class).
  • OOM at the SRAM allocation produces a clear error message naming the _Large variant.
  • 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 WaveSimulation2D still compile and run with no API churn on the consumer side.

Out of scope

  • Adding a templated Allocator parameter (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_psram users in the codebase. This issue is scoped to WaveSimulation2D_Real only.

Cross-references

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions