Skip to content

bloat: mark fl::basic_string + fl::sstream dtors noexcept to eliminate per-call-site landing pads #2975

Closed

Description

@zackees

Tracked in #2974.

Smoking-gun pattern (from the meta)

Per the bloat report at .build/symbols/esp32s3-fbuild/report.md, every 1-4 KB fl::* symbol on ESP32-S3 Blink references _Unwind_Resume (156 B once + landing pad metadata per call site). The per-call-site cost is roughly 100-200 B of exception unwinding tables at each FL_WARN / FL_ERROR / FL_INFO site, because the compiler must assume that the temporary fl::sstream and the fl::string it carries can throw on destruction.

From #2974's analysis:

Per FL_WARN/FL_ERROR call site:
  ~200-300 B inline (sstream ctor + format << chain + log_emit call)
  ~100-200 B exception landing pad metadata (string/sstream dtors can throw)

Removing the landing pads is the cheapest, lowest-risk win because nothing in the destructor chain actually throws today — it's just that the compiler can't prove it.

Current state (observed)

Destructors are declared with the FL_NOEXCEPT macro, but FL_NOEXCEPT is currently a no-op on every platform:

src/fl/stl/noexcept.h:

// FL_NOEXCEPT: intentionally a noop on all platforms.
// noexcept was causing too many platform compatibility issues (AVR, WASM, etc.)
// so it is disabled everywhere until a robust cross-platform solution is found.
#ifndef FL_NOEXCEPT
#define FL_NOEXCEPT
#endif

So while the source intent is clearly "these dtors don't throw", the compiler sees no noexcept specifier and dutifully emits landing pads at every call site. This is precisely the bloat the meta is targeting.

Destructor chain inventory

Type Location Has explicit dtor? Currently marked? Effective state
fl::basic_string src/fl/stl/basic_string.h:439 yes (~basic_string() FL_NOEXCEPT;) FL_NOEXCEPT (no-op) potentially throwing
fl::sstream src/fl/stl/strstream.h:34 no — implicit n/a inherits from string mStr -> potentially throwing
fl::StringHolder src/fl/stl/detail/string_holder.h:18 yes FL_NOEXCEPT (no-op) potentially throwing
fl::variant<...> src/fl/stl/variant.h:52 yes (~variant() FL_NOEXCEPT { reset(); }) FL_NOEXCEPT (no-op) potentially throwing
fl::shared_ptr<StringHolder> src/fl/stl/shared_ptr.h yes FL_NOEXCEPT (no-op) potentially throwing
fl::detail::ControlBlockBase (virtual) src/fl/stl/shared_ptr.h:39 yes (virtual ~ControlBlockBase() FL_NOEXCEPT;) FL_NOEXCEPT (no-op) potentially throwing

~basic_string() body in src/fl/stl/basic_string.cpp.hpp:19 is {} — all real cleanup happens via implicit member destruction of mStorage (the variant), which calls into shared_ptr -> StringHolder destruction. Implicit dtor noexcept-ness is derived from member subobjects, so the entire chain must be noexcept for the spec to actually propagate to the call site.

Proposed fix

The minimum change is to make FL_NOEXCEPT expand to the real noexcept keyword for the platforms where C++11 is fully supported (essentially every modern target — desktop, ESP32, Teensy, WASM). Per the comment in noexcept.h the historical block was "AVR, WASM, etc." — both AVR-GCC and Emscripten have supported noexcept for years, so the macro can almost certainly be turned on globally.

Option A (preferred — single source change, fixes the whole chain at once):

// src/fl/stl/noexcept.h
#ifndef FL_NOEXCEPT
#define FL_NOEXCEPT noexcept
#define FL_HAS_NOEXCEPT 1
#endif

Option B (conservative — opt-in per dtor): keep FL_NOEXCEPT as a no-op but introduce a separate FL_DTOR_NOEXCEPT macro that always expands to noexcept, and apply it only to the dtors in the chain above. This avoids touching every existing call site but does require auditing each dtor in the chain individually.

Either way the chain that must end up noexcept for the optimization to land is:

  1. fl::basic_string::~basic_string()
  2. fl::sstream::~sstream() (implicit — fixing the basic_string chain fixes this for free)
  3. fl::StringHolder::~StringHolder()
  4. fl::variant<NotNullStringHolderPtr, ConstLiteral, ConstView>::~variant() (and its reset() helper)
  5. fl::shared_ptr<StringHolder>::~shared_ptr()
  6. fl::detail::ControlBlockBase::~ControlBlockBase() (virtual — must be noexcept in the base for any override to be noexcept)
  7. fl::detail::ControlBlock<StringHolder>::destroy_object() / destroy_control_block()

Chain elements that may need work first

  • StringHolder allocates with new[] in grow(). If new throws (i.e., not nothrow new), then StringHolder constructors aren't noexcept — but constructors aren't relevant here; the dtor just calls delete[] mData, which is implicitly noexcept. Fine.
  • variant::reset() calls placed destructors. As long as every contained type's dtor is noexcept, variant::~variant() is noexcept. The variant in basic_string holds NotNullStringHolderPtr (shared_ptr), ConstLiteral (trivial), ConstView (trivial) — only the shared_ptr matters, and its dtor is delete on a control block, which is implicitly noexcept.
  • ControlBlockBase::~ControlBlockBase() is virtual. Its definition lives out-of-line in src/fl/stl/shared_ptr.cpp.hpp (anchored there for vtable ODR reasons). The override-able-while-noexcept rule means both the base declaration and every override must have the same exception spec; flipping the macro covers both.
  • Nothing in the chain actually throws today — the only potential throw points would be new, and the only new in the chain is in the add_shared_ref / refcount paths which use atomic counters, not allocations. The dtors themselves only do delete and arithmetic.

Estimated savings

Per the meta's analysis: ~100-200 B per FL_WARN / FL_ERROR call site in landing pad metadata. The meta identifies 8 distinct 1-4 KB functions, each containing 3-5 log sites, plus the broader 30+ call sites in the 1-4 KB band. Conservative estimate:

  • ~3-6 KB recovered on ESP32-S3 Blink from landing-pad elimination alone
  • Plus the shared _Unwind_Resume (156 B) reference may itself drop out if no FastLED code path remains that requires it (depends on other dtors in the binary)

This is the lowest-risk single change in the #2974 plan: no semantic changes, no code reorganization, just a macro flip and (if needed) a small audit of the destructor chain to confirm nothing actually throws.

Verification plan

  1. Toggle FL_NOEXCEPT -> noexcept (Option A).
  2. bash compile esp32s3 --examples Blink — confirm clean build.
  3. bash bloat esp32s3 — diff .build/symbols/esp32s3-fbuild/report.md for _Unwind_Resume references and total flash delta on the 8 target functions.
  4. bash test --cpp — confirm no regression in unit tests (the noexcept change should be transparent unless some test relies on dtor-throwing-during-stack-unwind, which would already be a bug).
  5. Spot-check AVR (uno) and WASM compile to confirm noexcept is accepted on the historically-problematic targets.

Refs

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