Skip to content

perf(log): migrate fl::sstream to printf-style backend for ~5-10 KB on ESP32-S3 (split from #3122 B2 / refs #2856 item 3.2) #3158

Closed

Description

@zackees

Why this issue exists

Split from the now-closed rundown meta #3122 row B2. Item 3.2 of meta #2856 ("fl::sstream operator<< → printf-style log backend") was sized as the single highest-ROI of #2856's five architectural items (~5-10 KB on ESP32-S3 Blink) but is genuinely too big for a "go" / "skip" rundown commitment — it touches every FL_WARN / FL_LOG_* / FL_DBG call site in the codebase and changes their compilation behavior. Pulling it into its own focused issue lets us plan the API contract and migration without forcing it through a one-sentence-to-unblock format.

Problem

fl::sstream is a header-only operator-chain logger. Every FL_WARN(...) call site instantiates a fresh chain of operator<< template specializations for whatever types it streams, and each specialization carries its own rodata strings (separators, formatters, prefix/suffix). On a typical ESP32-S3 Blink build the libFastLED.a carries 75 FL_WARN/FL_LOG sites in channel_driver_rmt.cpp.hpp, 59 in rmt_memory_manager.cpp.hpp — each contributing to the per-site weight that the #2886 audit measured (#2886 row #1: 58,770 B for the transitive closure of one ClocklessIdf5<> ctor).

printf (or snprintf) is the opposite pattern: a single shared TU does the formatting, every call site is just a function call with a format string and varargs. Newlib's vfprintf cluster is ~36 KB but that already lives in the binary today (used by Serial.printf etc.), so the marginal cost of routing FastLED logs through it is zero or negative — we'd remove the per-site sstream rodata in exchange for shared printf calls.

Proposed architecture

Three sub-questions, each non-trivial:

1. API contract

Two options:

a. Keep FL_WARN(stream-expression) source-compatible, swap the backend.

#define FL_WARN(x) ::fl::detail::log_sstream_to_printf(x)

Requires a sstream-shim that captures the operator<<-chain result into a temporary buffer, then routes through printf. Saves migration effort but doesn't actually shrink the binary — the operator<< chains still instantiate per-site, they just write to the shim instead of to Serial.

b. Migrate call sites to printf-style.

FL_WARN("RMT memory: %u bytes used / %u free", used, free);

Real binary win because operator<< chains are gone. But ~400+ call sites need rewriting, and inline expressions like FL_WARN("foo " << myFunc()) need expansion to format strings.

Recommendation: (b), with a transition macro FL_WARN_LEGACY(x) that wraps the old form during migration so we can land it in passes.

2. Format-string safety

Switching to printf opens the door to format-string bugs. Two safeguards:

  • __attribute__((format(printf, N, M))) on the FL_WARN function so GCC/Clang verify format-string ↔ arg-type matches at compile time.
  • A custom-clang-tool check in ci/lint_cpp_rs/ that catches FL_WARN(some_string_var, ...) (i.e. non-literal format strings — they bypass the printf attribute).

3. Newlib NANO compatibility

Several boards (per tools/sdkconfig_for_smallest_fastled.defaults) enable CONFIG_NEWLIB_NANO_FORMAT=y which drops %f/%lld/%llu from printf. Our refactored call sites need to:

  • Audit which existing FL_WARN sites stream float / i64 / u64 and decide whether to keep them as nano-incompatible (gated by #if FASTLED_LOG_VERBOSITY > 0) or rewrite to use integer-only formatting.
  • Add a FL_WARN_NANO_INCOMPAT(...) variant that errors at compile time when NANO is on, so we catch regressions.

Scope of the actual implementation PR

Once the API and safeguards are agreed:

  • Introduce FL_PRINTF_FORMAT macro in fl/system/log.h (or wherever the FL_WARN/FL_LOG entry points live) — __attribute__((format(printf,N,M))) on GCC/Clang, empty on MSVC.
  • Add the printf-style FL_WARN_F / FL_LOG_F / FL_DBG_F macros alongside the existing sstream ones. Both work side-by-side initially.
  • Add the non-literal-format clang-tool check in ci/lint_cpp_rs/src/lint_core/.
  • Migrate the hot-path call sites (the top-25 weighted ones from Meta: ESP32-S3 Blink bloat reduction (file:line concrete plan) — supersedes #2885 #2886) to the new macros — typically batches of 5-10 sites per PR.
  • Add a NANO-compat audit test that walks all migrated call sites and verifies format-string compatibility.
  • Deprecate the original FL_WARN(stream-expr) form once migration is complete — gate it behind #define FL_ALLOW_LEGACY_SSTREAM_LOG so users who depend on the old form opt in explicitly.

Acceptance

  • ESP32-S3 NEOPIXEL Blink binary shrinks by ≥ 5,000 B vs the post-Meta: ESP32-S3 Blink bloat reduction (file:line concrete plan) — supersedes #2885 #2886-Stage-1 baseline (338,117 B → ≤ 333,117 B).
  • No FL_WARN/FL_LOG regression — every existing log message still reaches the serial console with at-least-equivalent semantic content.
  • Format-string attribute catches at least one bug at compile time during migration (proves the safeguard works).
  • NANO-compat test catches at least one %f or %lld introduced during migration (proves the audit works).

Why this is a focused issue rather than rundown-row work

The original rundown meta #3122 B2 mis-scoped this as a single rundown-row commitment. The actual work is API design + lint infrastructure + ~400 call-site migrations + NANO compatibility + format-string-attribute discipline — easily 2-3 weeks of focused work spread across many PRs. Pulling it into its own issue lets us plan it properly. See the closeout comment on #3122 for the full rundown context.

Cross-references

Activity

  1. zackees commented on Jun 19, 2026

    @zackees
    MemberAuthor

    Status audit (2026-06-19)

    A walk through the acceptance criteria against the current master:

    Item Status Evidence
    Add printf-style FL_WARN_F / FL_LOG_F / FL_DBG_F alongside sstream macros ✅ Done src/fl/log/log.h since #3190
    Migrate hot-path call sites (top-25 weighted from #2886) to the new macros ✅ Done by #3190 git log --oneline -- src/platforms/esp/32/drivers/rmt/rmt_5/ shows commit da59e86e2 Migrate logging macros to printf style (#3190); the two cited hot files (channel_driver_rmt.cpp.hpp, rmt_memory_manager.cpp.hpp) contain zero remaining FL_WARN(... << ...) stream-form calls — only FL_WARN_F(\"%s\", ...) / FL_LOG_RMT_F(...)
    Deprecate the original FL_WARN(stream-expr) form / unify under one entry point ✅ Addressed by #3272 (PR #3273) One FL_WARN(...) macro accepts both forms via argument-count dispatch; _F macros become aliases
    Introduce FL_PRINTF_FORMAT macro (__attribute__((format(printf, N, M)))) ⏳ Not blocking — already on fl::printf itself in fl/stl/stdio.h Independent: see #3174 for placeholder-safety improvements
    Non-literal-format clang-tool lint check ⏳ Deferred Belongs in the ci/lint_cpp_rs/ infrastructure; not blocking
    NANO-compat audit test for %f / %lld ⏳ Deferred Separate concern; tracked independently
    Binary-size acceptance: ESP32-S3 NEOPIXEL Blink ≤ 333,117 B 🟡 Best-measured by the CI bloat snapshot The migration that #3190 landed targeted exactly this; the size-tracking workflow (.github/workflows/check_esp32_size.yml) is what enforces the 5 KB win on each push

    Remaining stream-form sites

    Grep FL_WARN\s*\([^)]*<< across src/ reports ~79 hits across 22 files. These break down into two buckets:

    Cannot reasonably be migrated (template code or header-only generic formatting — printf can't bind arbitrary types via templates):

    • src/fl/task/promise.h, src/fl/task/promise_result.h, src/fl/task/executor.h
    • src/fl/stl/shared_ptr.h, src/fl/stl/json.h, src/fl/stl/json/types.h
    • src/fl/audio/fft/fft_impl.h
    • src/fl/gfx/raster_sparse.h
    • src/fl/channels/spi.h, src/fl/channels/spi/multi_lane_device.h
    • src/fl/math/transposition.h
    • src/fl/net/http/fetch.h

    These genuinely need operator<< because the payload type is not known at the call site (template parameter / arbitrary user type). They are also off the ESP32-S3 NEOPIXEL Blink hot path.

    Cold non-hot-path sites that COULD be migrated, but won't move the binary-size needle:

    • src/platforms/wasm/ui.cpp.hpp (6), src/platforms/wasm/timer.cpp.hpp (3) — WASM-only, doesn't ship on embedded
    • src/platforms/shared/ui/json/ui_manager.cpp.hpp (12), ui.cpp.hpp (13), audio.cpp.hpp (1) — JSON UI manipulation
    • src/platforms/esp/32/drivers/parlio/parlio_engine.cpp.hpp (3 — all commented out)

    Recommendation

    Close this issue once #3273 (which provides the unified FL_WARN(...) entry point that drops the _F suffix from new code) merges. The binary-size acceptance criterion is best tracked by the existing CI size workflow; the lint-infrastructure and NANO-audit items belong in their own focused issues (or #3174 where they already partially overlap).

  2. zackees commented on Jun 19, 2026

    @zackees
    MemberAuthor

    Closing as substantially complete via merged PRs:

    Remaining ~79 stream-form sites in src/ are either template code that genuinely needs operator<< (e.g. fl/task/promise.h, fl/stl/json/types.h, fl/stl/shared_ptr.h) or off-the-hot-path WASM/UI/HTTP files. The binary-size acceptance criterion is best tracked by the existing .github/workflows/check_esp32_size.yml size workflow, which #3190 was specifically designed to satisfy.

    Deferred items (lint infrastructure, NANO-compat audit, FL_PRINTF_FORMAT lint tool) belong in their own focused follow-ups; they overlap with #3174's formatter-safety work rather than this issue's API/migration scope.

  3. moved this from Triage to Done in FastLED Trackeron Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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