Skip to content

feat(shrink): apply --shrink=printf on ESP32 via newlib-nano (#1459) - #1826

Merged
zackees merged 2 commits into
mainfrom
agent/shrink-printf-nano
Oct 31, 2026
Merged

zackees merged 2 commits into
mainfrom
agent/shrink-printf-nano

Conversation

@zackees

@zackees zackees commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Implements the one reducer #1459 asks for on ESP32-S3/Xtensa + newlib: fbuild build --shrink=printf now links newlib-nano instead of the full newlib printf cores, and says so on the terminal.

Part 1 (#1813, merged 3c2b0a2) made --shrink report that nothing was registered. This PR is the reducer itself.

Why newlib-nano and not a hand-written printf

Every newlib formatting entry point funnels through _vfprintf_r / _svfprintf_r. Rather than re-implement printf (the #493 --wrap design), the Xtensa toolchain already ships a second, vendor-maintained copy of the same libc — libc_nano.a + nano.specs — whose spec file rewrites -lc → -lc_nano and -lm → -lm_nano at link time. So the substitution is Espressif's own thin formatter: no formatting semantics are invented in this repo, and --gc-sections still applies.

-Wl,-u,_printf_float is linked alongside it, because newlib-nano gates its float formatter behind that symbol and otherwise prints %f verbatim. Keeping it means a shrunk build formats the same values; it costs the float path (~1.5 KB) but not the 11 KB full _vfprintf_r.

Measured A/B (Blink, tests/platform/esp32s3, ESP32-S3, xtensa-esp-elf 14.2.0, IDF 5.5.5)

Same link argv, -specs=nano.specs -Wl,-u,_printf_float added:

full newlib --shrink=printf delta
.text 229,743 B 208,227 B −21,516 B
.data 77,975 B 76,463 B −1,512 B
flash (.text + .data) 307,718 B 284,690 B −23,028 B (−7.5%)
_vfprintf_r 11,413 B 698 B −10,715
_svfprintf_r 11,222 B 624 B −10,598
_dtoa_r 3,223 B 3,223 B (kept for floats) —

Without -u _printf_float the same link is 276,282 B of flash (_dtoa_r drops too) — −31,436 B — but %f stops printing. That is the knob to revisit if a project never formats floats; it is not the default.

What changed

  • crates/fbuild-build-esp/src/esp32/printf_thin.rs (new, 116 LOC): applies() (explicit printf + ESP32 only), link_flags(), nano_specs_from_probe() (pure, tested) and probe_nano_specs(), which asks the selected compiler -print-file-name=nano.specs and requires an absolute path that exists.
  • Fail-safe: if the toolchain has no nano variant the build logs shrink: --shrink=printf found no nano.specs …; building unshrunk and links normally — it cannot break a build.
  • BuildParams.shrink: ShrinkMode (threaded CLI → daemon request → orchestrator, like caller_path), because the daemon does not inherit caller flags; the ESP32 orchestrator extends LinkExtraArgs.flags only when applies() is true.
  • Esp32FingerprintMetadata.shrink_mode, so flipping off ↔ printf invalidates the fast path and relinks (test added).
  • shrink_notice: --shrink=printf on ESP32 reports the applied reducer line instead of the "no effect yet" warning; every other platform/mode keeps the warning (tests updated).
  • ShrinkMode::as_str/FromStr for the daemon request field.

Tests

  • bash test (workspace) → 4116 passed, 0 failed, rc 0 (TMPDIR=/home/niteris/t).
  • cargo test -p fbuild-build-esp --lib printf_thin → 3 passed.
  • cargo test -p fbuild-build-engine --lib shrink → 42 passed.
  • cargo test -p fbuild-cli --bin fbuild shrink → 16 passed, incl. the updated notice tests.
  • cargo clippy --workspace --all-targets -- -D warnings → rc 0.
  • cargo check -p fbuild-build-esp -p fbuild-cli -p fbuild-daemon --all-targets clean.
  • ci/enforce_platform_boundary.py --print-totals → 44/43; ci/check_test_layout.py ok.

The LOC gate first failed on tests/it/esp32_build.rs (994 → 1004 lines because every BuildParams literal needed the new field); the second commit replaces its nine duplicated literals with one esp32_params() builder (file is now 819 lines).

Not in this PR

build_info_<env>.json telemetry (ShrinkRecord still unwired) and Auto → Safe promotion. --shrink=auto continues to resolve to Off, as documented, until the formatting-compatibility matrix is justified; the reducer is explicit and opt-in.

Live CLI A/B (tests/platform/esp32s3, same dir, mode flipped)

$ fbuild build --environment esp32s3 --no-shrink     -> flash 356075 B, firmware.bin 300.62 KB
$ fbuild build --environment esp32s3 --shrink=printf -> flash 333047 B, firmware.bin 278.12 KB   (-23028 B, -6.5%)
   shrink: --shrink=printf links newlib-nano (-specs=nano.specs) instead of newlib's full
   _vfprintf_r/_svfprintf_r; float formatting kept via _printf_float
$ fbuild build --environment esp32s3 --no-shrink     -> flash 356075 B   (flip back relinks)

nm on the shrunk image: _vfprintf_r 698 B, _svfprintf_r 624 B, _printf_float 1120 B, _dtoa_r unchanged. The RAM figure is identical (74919 B) in both — the reducer only touches flash.

Not verified: a flash/boot of the shrunk image. The attached USB-JTAG device reports only the shared 303A:1001 Espressif identity, so flashing it could have targeted the wrong chip. The link, symbol and image-size deltas above are real; boot is unproven.

Refs #1459 — the remaining acceptance item is build_info_<env>.json telemetry (ShrinkRecord is still unwired) and any Auto → Safe promotion.

`--shrink=printf` now reaches the link stage on ESP32: the link gains
`-specs=nano.specs` (which rewrites -lc/-lm to libc_nano/libm_nano) plus
`-Wl,-u,_printf_float` so `%f` keeps working. The full `_vfprintf_r`
(11,413 B) + `_svfprintf_r` (11,222 B) cluster is replaced by newlib's own
thin formatter (698 B / 624 B), so no printf semantics are invented in-tree.

Blink on esp32-s3-devkitc-1 (xtensa-esp-elf 14.2.0, IDF 5.5.5):
flash 356,075 -> 333,047 B (-23,028, -6.5%), .text 229,743 -> 208,227.

- `shrink: ShrinkMode` on BuildParams, threaded CLI -> daemon -> orchestrator
  (the daemon does not inherit caller flags), like `caller_path`.
- `probe_nano_specs()` asks the selected compiler for its nano.specs and
  requires an absolute existing path; without it the build logs and links
  unshrunk instead of failing on a missing spec file.
- `Esp32FingerprintMetadata.shrink_mode` invalidates the fast path when the
  mode flips, so off <-> printf always relinks.
- `--shrink=printf` on ESP32 reports the applied reducer instead of the
  "no effect yet" warning; every other mode/platform keeps the warning.
@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@zackees
zackees added this pull request to the merge queue Oct 31, 2026
Merged via the queue into main with commit 564b676 Oct 31, 2026
37 checks passed
@zackees
zackees deleted the agent/shrink-printf-nano branch October 31, 2026 21:09
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

1 participant