Skip to content

Add a RAM-estimate calibration harness - #159

Merged
stevenmunn312-tech merged 1 commit into
mainfrom
test/ram-estimate-calibration
Aug 7, 2026
Merged

stevenmunn312-tech merged 1 commit into
mainfrom
test/ram-estimate-calibration

Conversation

@stevenmunn312-tech

Copy link
Copy Markdown
Owner

Closes the loop opened by #157. That PR fixed estimateFirmwareRam's palette blind spot; this makes the class of gap detectable instead of relying on someone noticing.

Why not just measure

estimateFirmwareRam predicts RAM from the graph alone — no compile, no toolchain, no board selected. That's what lets it run in the browser while you edit, and the live capacity meter already provides measured ground truth via /api/compile-check. Replacing the heuristic with symbol data would duplicate the meter and break the offline path.

So this doesn't replace it. It checks the prediction against a real build so the two can't drift apart silently — which is exactly how palette globals went uncounted: nothing compared the estimate to reality.

How it works

Per case: generate the sketch with the real generateCpp → build with fbuild for an Uno → read per-symbol sizes from fbuild symbols --json → bucket those symbols into the categories the estimate models → compare.

Category Symbols
leds array leds
frame buffers buf_*
field buffers field_*
palette tables paldef_*, pal_*
per-node state loop::* — cppGenerator emits it as a static inside loop()

Comparing named symbols rather than totals is what makes the assertions exact rather than tolerance-based. A measured total always sits above the estimate by the Arduino core plus FastLED's own statics, which the estimate never claimed to model.

Results — 5 cases, 25 comparisons, all exact

[solid]         leds 192/192   frame 192/192   field   0/0    pal   0/0    state  0/0
[plasma]        leds 192/192   frame 192/192   field   0/0    pal  48/48   state  0/0
[two-palettes]  leds 192/192   frame 576/576   field   0/0    pal  96/96   state  0/0
[field-chain]   leds 192/192   frame 192/192   field 256/256  pal  48/48   state  0/0
[fire2012]      leds 192/192   frame 192/192   field   0/0    pal  48/48   state 64/64

The fire2012 case is the reason this exists. STATEFUL_EXTRA_BYTES_PER_LED is a hand-maintained table where each entry restates by hand what an emit case allocates, making it the likeliest thing to drift. Its heat map lands at 64 bytes against a predicted 64 (loop::_heat_f).

A finding worth following up

The unmodelled baseline is exactly 373 bytes on every Uno build — 757−384, 805−432, 1237−864, 1061−688, 871−496. It's deterministic, so a future change could report predicted total RAM by adding a per-board constant, making the pre-upload readout directly comparable to the linker's figure. Not done here; it needs a measured constant per board.

Constraints baked in, and why

Gated behind RAM_CALIBRATION=1 since each case is a real compile:

RAM_CALIBRATION=1 npx vitest run src/utils/__tests__/ramCalibration.test.ts

Skips in the normal suite — 1724 passed, 11 skipped, lint and tsc -b clean.

Known limits

  • Uno only. The categories are board-independent but the numbers aren't exercised on ESP32, where PSRAM routing (usePsram) changes which bucket a buffer lands in. That path is entirely untested here.
  • Group nodes still aren't flattened by estimateFirmwareRam, so palettes and buffers inside a group subgraph remain uncounted. Pre-existing, carried over from Count palette tables in the pre-upload RAM estimate #157, and no case here covers it — adding one would fail, which is the honest way to surface it when someone wants it fixed.
  • Nodes with small untracked statics will fail the stateful assertion. The estimate deliberately tracks only materially large per-LED state. That failure is the signal, not noise: either the table should account for it, or the case doesn't belong here.

🤖 Generated with Claude Code

estimateFirmwareRam predicts a sketch's RAM from the graph alone, with no
compile — that is what lets it run in the browser while the user edits, so
this does not replace it with measurement. It checks the prediction against a
real build so the two cannot drift apart silently. Palette globals went
uncounted for exactly that reason: nothing compared the estimate to reality.

Per case it generates the sketch with the real generateCpp, builds it with
fbuild for an Uno, reads per-symbol sizes from `fbuild symbols --json`, and
buckets those symbols into the categories the estimate models: `leds`, the
`buf_*` frame buffers, the `field_*` field buffers, the `paldef_*`/`pal_*`
palette tables, and per-node simulation state, which cppGenerator emits as a
static inside loop() and the linker keeps as `loop::<name>`.

Comparison is against named symbols rather than total RAM, so assertions are
exact rather than tolerance-based — a measured total would always sit above
the estimate by the Arduino core and FastLED's own statics, which it never
claimed to model. That baseline is a constant 373 bytes per Uno build across
all five cases, so a future change could predict the total directly by adding
a per-board constant.

Five cases cover every modelled category and all 25 comparisons are exact.
The stateful one is the reason it exists: STATEFUL_EXTRA_BYTES_PER_LED is a
hand-maintained table where each entry restates by hand what an emit case
allocates, which makes it the likeliest to drift. Fire2012's heat map lands
at 64 bytes against a predicted 64.

LTO is disabled in the generated project: with it on, AVR merges everything
into a single `startup` symbol and per-symbol attribution is impossible.
The sketch is written as main.cpp rather than main.ino to avoid fbuild's
prototype insertion (FastLED/fbuild#1275), and it builds against the FastLED
the upload helper already vendors rather than cloning its own.

Gated behind RAM_CALIBRATION=1 since each case is a real compile; it skips in
the normal suite.

Co-Authored-By: Claude Opus 5 <[email protected]>
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.

1 participant