Skip to content

bloat: reduce dispatch-loop landing pad count in ChannelEngineRMTImpl::processPendingChannels #2980

Closed

Description

@zackees

Tracked in #2974 (item TBD-I).

Function under audit

fl::ChannelEngineRMTImpl::processPendingChannels() — 1,078 B on ESP32-S3 Blink.

Located at src/platforms/esp/32/drivers/rmt/rmt_5/channel_driver_rmt.cpp.hpp:981.

Called by:

  • beginTransmission (×15)
  • poll (×6)

The function is already marked FL_NOEXCEPT, but the body still pays full exception-cleanup metadata because the callees inside the dispatch loop are mostly noexcept but writeWithPadding and the inline FL_WARN/FL_LOG_RMT machinery are not.

Top callees (per bloat report)

Callee Bytes
fl::ChannelEngineRMTImpl::acquireChannel(...) 516 B
fl::RMTBufferPool::acquireDMA(unsigned int) 394 B
fl::ChannelEngineRMTImpl::releaseChannel(...) 374 B
fl::RMTBufferPool::releaseInternal(fl::span<unsigned char>) 362 B
fl::detail::log_emit 222 B
fl::RMTBufferPool::releaseDMA() 178 B
_Unwind_Resume 156 B
fl::ChannelData::writeWithPadding(fl::span<unsigned char>) 100 B
plus fl::sstream::appendFormatted × 2, fl::string::string, fl::basic_string::~, etc.

Loop structure (verified in source)

processPendingChannels() is a for(size_t i=0; i<mPendingChannels.size(); ) walk over the pending queue. Each iteration tries to start one transmission and contains four distinct try-acquire / on-fail-log / on-success-continue branches, plus a final swap-and-pop on full success:

  1. Acquire HW channel — acquireChannel(...); on fail: ++i; continue; (no log)
  2. Verify encoder — if (!channel->encoder); on fail: FL_WARN(...) + releaseChannel(channel) + ++i; continue;
  3. Acquire pooled buffer — mBufferPool.acquireDMA(...) or acquireInternal(...); on empty: FL_WARN(...) + releaseChannel(channel) + ++i; continue;
  4. writeWithPadding into the pooled buffer (no error path, but callee is not noexcept)
  5. Enable channel — mPeripheral.enableChannel(...); on fail: FL_LOG_RMT(...) + buffer release (DMA or internal) + releaseChannel(channel) + ++i; continue;
  6. Reset encoder — mPeripheral.resetEncoder(...); on fail: FL_LOG_RMT(...) + disableChannel(...) + buffer release + releaseChannel(channel) + ++i; continue;
  7. Transmit — mPeripheral.transmit(...); on fail: FL_WARN(...) + disableChannel(...) + buffer release + releaseChannel(channel) + ++i; continue;
  8. Success log — FL_LOG_RMT(\"Started transmission for pin ...\") + swap-and-pop

That's 6 distinct failure branches (steps 2, 3, 5, 6, 7) carrying full log-call-site bursts and inline cleanup blocks, plus the success log site — 7 sstream/log call sites total in a single function body, each with its own landing-pad metadata for the destructors of the temporary sstream / string that build the message.

Per-branch cost analysis

  • Each FL_WARN / FL_LOG_RMT site: ~200-300 B inline (sstream ctor + << chain + log_emit call) + ~100-200 B landing pad metadata for sstream/string destructors that the compiler currently treats as potentially throwing.
  • 7 log sites × ~150 B landing pad = ~1 KB of pure landing-pad overhead in a 1,078 B function — i.e. the majority of the function's size is exception-cleanup metadata, not actual work.
  • Verified: acquireChannel, releaseChannel, all RMTBufferPool::{acquire,release}{DMA,Internal} are already declared FL_NOEXCEPT (see buffer_pool.h:51,60,67,72 and channel_driver_rmt.cpp.hpp:390-394). processPendingChannels itself is also FL_NOEXCEPT (line 981).
  • Remaining non-noexcept callees:
    • fl::ChannelData::writeWithPadding(fl::span<u8>) — declared FL_NOEXCEPT in data.h:110 but the definition in data.cpp.hpp:62 is missing the noexcept specifier. Marking the definition to match the declaration eliminates the landing pad around the writeWithPadding call.
    • FL_WARN / FL_LOG_RMT machinery (fl::sstream ctor/dtor, fl::string dtor, log_emit) — currently the dominant landing-pad source, addressed by TBD-A (mark sstream/string dtors noexcept).

Cross-references (these apply directly here)

  • TBD-A — noexcept destructors on fl::basic_string + fl::sstream. This single fix removes the per-log-site landing pad metadata for all 7 log sites in this function. Largest individual lever.
  • TBD-D — consolidate error paths via expected<>::failure(...). The 6 failure branches all follow the same pattern: cleanup + log + ++i; continue;. They can be folded into per-step helper functions that return expected<void, FailureKind>; the loop body then has a single FL_WARN site at the bottom that switches on the failure kind. Same approach proposed for reconfigureForNetwork() / createChannel(...) in TBD-D.
  • Quick win: add noexcept (or FL_NOEXCEPT) to the definition of ChannelData::writeWithPadding in data.cpp.hpp:62 to match the header declaration — strictly an oversight, costs nothing.

Estimated savings

~300-500 B — smaller than the bigger 2 KB+ functions in this meta because processPendingChannels is itself only ~1 KB, but the pattern (6 nearly-identical cleanup branches + 7 log sites) is the same one TBD-A and TBD-D target. Once those land cross-cuttingly, this function inherits most of the savings automatically.

Suggested follow-up order

  1. Fix writeWithPadding definition noexcept mismatch (trivial, no behavior change, removes one landing pad).
  2. Land TBD-A (sstream/string dtors noexcept) — eliminates landing pads from all 7 log sites in this function.
  3. Apply TBD-D pattern: refactor the 6 failure branches into helpers returning fl::expected<void, FailureKind> and consolidate the FL_WARN to one bottom-of-loop site keyed on the failure kind.
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