Skip to content

Gate FASTLED_ASSERT diagnostic on FASTLED_LOG_RUNTIME_ENABLED (~1-2 KB) (#2886) #2923

Closed

Description

@zackees

Goal

Gate the diagnostic chain inside FASTLED_ASSERT(x, MSG) on FASTLED_LOG_RUNTIME_ENABLED. The condition check stays (assertion semantics preserved), but the fl::sstream() << "FASTLED ASSERT FAILED: " << MSG)).c_str() chain that fires on assertion FAILURE becomes a no-op in release builds where logging is already disabled.

Parent: #2886 — eighth algorithmic-bloat reduction. Mirrors the #2918 pattern: identify cold-path diagnostic chains the compiler can't DCE because their side-effects on a sstream are observable.

Why this is bloat

src/platforms/esp/esp_assert.h:13-18:

#define FASTLED_ASSERT(x, MSG)                                                 \
    do {                                                                       \
        if (!(x)) {                                                            \
            fl::println((fl::sstream() << "FASTLED ASSERT FAILED: " << MSG).c_str());  \
        }                                                                      \
    } while (0)

In release builds (NDEBUG → FASTLED_LOG_VERBOSITY=0 per Stage 1), this macro is not gated. The condition check + diagnostic block are emitted at every call site.

The optimizer leaves the fl::sstream() << ... << MSG chain inside the failure branch live because:

  1. The branch is conditional on a runtime value ((x)), not a constant.
  2. The sstream side effects are observable (heap allocation, sequential writes), so the optimizer can't DCE the chain even if it knows the branch is rarely taken.

There are 65 FASTLED_ASSERT call sites in src/ (per grep -c). Each adds ~30-50 B of unique diagnostic-chain code (the MSG literal + per-call-site operator<< instantiation). Cumulative bloat reachable in a Blink build: estimated ~1-2 KB.

Deliverable

src/platforms/esp/esp_assert.h — gate the diagnostic

 #define FASTLED_ASSERT(x, MSG)                                                 \
     do {                                                                       \
         if (!(x)) {                                                            \
+#if FASTLED_LOG_RUNTIME_ENABLED                                                \
             fl::println((fl::sstream() << "FASTLED ASSERT FAILED: " << MSG).c_str());  \
+#else                                                                          \
+            (void)0;                                                           \
+#endif                                                                         \
         }                                                                      \
     } while (0)

Same gating for src/platforms/assert_defs.h (the cross-platform fallback) and src/platforms/wasm/js_assert.h (the WASM emscripten variant).

The condition (x) is still evaluated for side effects — matching <cassert> semantics. The diagnostic message is suppressed in release.

Behavior matrix

Build mode Macro emits Behavior on assertion failure
Debug (FASTLED_LOG_VERBOSITY=1, no NDEBUG) full chain prints "FASTLED ASSERT FAILED: " via fl::println
Release (NDEBUG → FASTLED_LOG_VERBOSITY=0) condition check only silent (matches C++ NDEBUG semantics for <cassert>)

The C++ standard library precedent: <cassert>'s assert() macro is also NDEBUG-gated. FASTLED_ASSERT now follows the same convention.

Acceptance

  1. src/platforms/esp/esp_assert.h — diagnostic gated on FASTLED_LOG_RUNTIME_ENABLED.
  2. src/platforms/assert_defs.h — same gate.
  3. src/platforms/wasm/js_assert.h — same gate.
  4. bash lint --cpp passes.
  5. Measured against the post-perf(esp32): gate emit* cold helpers on FASTLED_LOG_RUNTIME_ENABLED (~800 B) #2918 ELF baseline (344,769 B), a release build should be ~1-2 KB lower. Baseline file ratchets in the same PR.

Expected savings

Conservative estimate: ~800 B - 2 KB measured on the ESP32-S3 NEOPIXEL Blink build. Variance reflects how many FASTLED_ASSERT call sites are reachable from the Blink transitive closure.

Out of scope

  • Replacing FASTLED_ASSERT with C assert() macros. Different API surface.
  • Adding a separate FASTLED_DEBUG_ASSERT for asserts that should be debug-only without the noise of full diagnostics. Future iteration.
  • Auditing every assert call site for correctness. They're correct; only the diagnostic chain is the bloat.

Related

🤖 Generated with Claude Code

Activity

  1. added a commit that references this issue on Jun 7, 2026
    669d199
  2. moved this from Triage to Done in FastLED Trackeron Jun 7, 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