Skip to content

Count palette tables in the pre-upload RAM estimate - #157

Merged
stevenmunn312-tech merged 1 commit into
mainfrom
fix/ram-estimate-palette-tables
Aug 7, 2026
Merged

stevenmunn312-tech merged 1 commit into
mainfrom
fix/ram-estimate-palette-tables

Conversation

@stevenmunn312-tech

Copy link
Copy Markdown
Owner

Follow-up to #156. That PR removed ~1.4 KB of unused palette globals from generated sketches; this fixes the estimator that should have flagged them and didn't.

The gap

estimateFirmwareRam ignored CRGBPalette16 globals entirely. It counted the leds array, per-node buf_/field_ render buffers, and known simulation state — but not palette tables, which are non-const globals at 48 bytes each. On a pre-#156 sketch that was ~1.4 KB invisible to the one thing meant to warn you before an upload.

That blind spot is why the bloat survived: nothing that looked at RAM could see the largest single contributor.

How it resolves them

The same way cppGenerator's paletteExpr does, rather than restating which nodes consume a palette:

  • Palette-typed input ports come from NODE_LIBRARY, so the estimator and codegen follow one source. Adding a palette input to a node keeps both in step with no second list to maintain.
  • One shared paldef_<name> per distinct named palette — two nodes on ocean cost 48 bytes, not 96.
  • One pal_<id> per palette-building node (CustomPalette, PaletteFromImage, PaletteBlend, Poline); a consumer wired to one resolves to that table instead of also pulling in a shared one.
  • Only nodes reachable from MatrixOutput count, matching how the rest of the estimate already works.

Palettes land in internalBytes regardless of the usePsram toggle, because codegen never PSRAM-allocates them.

Where the two could disagree, it over-counts — if an emit case declares a palette port it never samples. That's the safe direction for a pre-upload budget.

Verification

Checked against a real Arduino Uno build, not just unit tests: the estimate's 48 bytes for a single-palette Plasma sketch matches the paldef_ symbols fbuild symbols reports in .bss for the same graph.

Gates green — lint, tsc -b, 1724 tests passed.

Three new tests cover distinct-palette deduplication, builder nodes resolving to their own table, and unreachable nodes being ignored. One existing ColorTrails expectation moved 144 → 192 bytes: it has a paletteIn port, so the sketch genuinely does declare a table for it — the old number was the bug.

Also in here

CHANGELOG.md entries from #156 corrected with the measured figures. They shipped quoting my arithmetic (~1.1 KB) rather than the real Uno numbers — 1,056 B of RAM and 7,960 B of flash. The flash half was unaccounted for entirely: these globals carry dynamic initialisers, so each also emits startup constructor code at roughly 360 B apiece. An unused palette cost ~408 B total, not 48.

Not in here

  • A calibration test asserting the estimate stays within tolerance of a measured build. That's the thing that would stop this class of drift for good, but it should follow the fbuild 2.5.4 → 2.5.15 bump rather than calibrate tolerances against a version about to change. It will also need -flto disabled, or AVR collapses every symbol into one startup blob with nothing to compare per-node.
  • Group expansion. estimateFirmwareRam doesn't flatten Group nodes, so palettes used only inside a group subgraph still aren't counted. Pre-existing and unchanged here.

🤖 Generated with Claude Code

estimateFirmwareRam ignored CRGBPalette16 globals entirely, under-reporting
by up to ~1.4KB — the gap that let the unused-palette bloat sit unnoticed
until a real build measured it.

Resolves them the way cppGenerator's paletteExpr does, reading palette-typed
input ports from NODE_LIBRARY rather than restating which nodes consume one:
one shared paldef_ table per distinct named palette, plus one pal_<id> per
palette-building node (CustomPalette, PaletteFromImage, PaletteBlend,
Poline). Only nodes reachable from MatrixOutput count, matching how the rest
of the estimate already works.

Palettes are always internal RAM — codegen never PSRAM-allocates them — so
they land in internalBytes regardless of the usePsram toggle.

Errs toward over-counting if an emit case declares a palette port it never
samples, which is the safe direction for a pre-upload budget. Verified
against a real Uno build: the estimate's 48 bytes for a single-palette
Plasma sketch matches the paldef_ symbols fbuild reports in .bss.

Co-Authored-By: Claude Opus 5 <[email protected]>
@stevenmunn312-tech
stevenmunn312-tech marked this pull request as ready for review August 7, 2026 10:19
@stevenmunn312-tech
stevenmunn312-tech merged commit 5648e3f into main Aug 7, 2026
2 checks passed
@stevenmunn312-tech
stevenmunn312-tech deleted the fix/ram-estimate-palette-tables branch August 7, 2026 10:19
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