Skip to content

bloat: extract RmtMemoryManager::handleAllocateTxFailure format machinery into shared cold helper #2978

Closed

Description

@zackees

Tracked in #2974 (sub-issue G).

Symbol

fl::RmtMemoryManager::handleAllocateTxFailure(unsigned char, unsigned int, unsigned int, bool) — 1,710 B on ESP32-S3 fbuild post-#2968.

Defined in src/platforms/esp/32/drivers/rmt/rmt_5/rmt_memory_manager.cpp.hpp (lines 417-484), declared in src/platforms/esp/32/drivers/rmt/rmt_5/rmt_memory_manager.h (line 465). Already marked FL_NO_INLINE and extracted from allocateTx for #2773 item 2.5 — but the function body itself is still pure error-reporting bloat.

Top callees (per-symbol bloat report)

Callee Bytes
fl::vector_basic::push_back_move_impl(void*) 234
fl::detail::log_emit(...) 222
fl::expected<unsigned int, fl::RmtMemoryError>::failure(RmtMemoryError, char...) 168
_Unwind_Resume 156
fl::RmtMemoryManager::getAvailableWords(bool) const 114
fl::fastled_file_offset 113
fl::sstream::appendFormatted(long) 83
fl::sstream::appendFormatted(unsigned long) 83
fl::RmtMemoryManager::tryAllocateWords(unsigned int, bool) 65

So the bulk of the 1,710 B is per-call-site sstream ctor + operator<< chain + log_emit + landing-pad metadata, repeated across 11 log sites:

  • 4 × FL_LOG_RMT (fallback retry path, lines 436-447)
  • 7 × FL_WARN (diagnostic block, lines 461-480)

Pattern

The diagnostic block is repetitive. Each FL_WARN formats a handful of size_t values into a fixed template, e.g.:

FL_WARN("RMT TX allocation failed for channel " << static_cast<int>(channel_id));
FL_WARN("  Requested: " << words_needed << " words (" << mem_blocks << "× buffer"
        << (networkActive ? \", Network mode\" : \"\") << \")\");
FL_WARN(\"  Available: \" << available << \" words\");
FL_WARN(\"  Memory breakdown: Total=\" << total << \", Allocated=\" << allocated
        << \", Reserved=\" << reserved << \" (external RMT usage)\");
// ... 5 more FL_WARN sites with conditional suggestions

Each site instantiates an fl::sstream, runs the << chain (each integer pulls in appendFormatted(unsigned long) glue), calls log_emit(LOG_WARN, __FILE__, __LINE__, ss), and emits a landing pad because ~sstream/~basic_string can throw. Per-site cost: ~200-300 B inline + ~100-200 B unwind metadata. Repeated 7-11× → most of the symbol.

The FL_LOG_RMT/FL_WARN formatting is not unique per site in any meaningful way — it's the same template (a fixed prefix string + 1-3 integer interpolations) with different values. This is the textbook case for moving the format machinery out-of-line.

Proposed fix

Extract a single FL_NO_INLINE cold helper in the .cpp.hpp (file-local / anonymous namespace), e.g.:

// Anonymous namespace, file-local.
FL_NO_INLINE static void emitAllocTxFailureDiagnostic(
    u8 channel_id, size_t words_needed, size_t mem_blocks, bool networkActive,
    size_t total, size_t allocated, size_t reserved, size_t available) FL_NOEXCEPT {
#if FASTLED_LOG_RUNTIME_ENABLED
    FL_WARN(\"RMT TX allocation failed for channel \" << static_cast<int>(channel_id));
    FL_WARN(\"  Requested: \" << words_needed << \" words (\" << mem_blocks << \"× buffer\"
            << (networkActive ? \", Network mode\" : \"\") << \")\");
    FL_WARN(\"  Available: \" << available << \" words\");
    FL_WARN(\"  Memory breakdown: Total=\" << total << \", Allocated=\" << allocated
            << \", Reserved=\" << reserved << \" (external RMT usage)\");
    if (reserved > 0) {
        FL_WARN(\"  Suggestion: \" << reserved << \" words reserved by external RMT usage (e.g., USB CDC)\");
        FL_WARN(\"              Consider using DMA channels (use_dma=true) to bypass on-chip memory\");
    }
    if (allocated > 0) {
        FL_WARN(\"  Suggestion: \" << allocated << \" words already allocated to other channels\");
        FL_WARN(\"              Consider reducing LED count or using fewer channels\");
    }
    if (mem_blocks > 2 && networkActive) {
        FL_WARN(\"  Suggestion: Network mode uses 3× buffering (\"
                << (mem_blocks * SOC_RMT_MEM_WORDS_PER_CHANNEL) << \" words per channel)\");
        FL_WARN(\"              Consider disabling network or using DMA channels\");
    }
#else
    (void)channel_id; (void)words_needed; (void)mem_blocks; (void)networkActive;
    (void)total; (void)allocated; (void)reserved; (void)available;
#endif
}

FL_NO_INLINE static void emitAllocTxFallbackLog(
    size_t mem_blocks, size_t words_needed, size_t fallback_words,
    u8 channel_id, bool success) FL_NOEXCEPT { /* the 4 FL_LOG_RMT sites */ }

Then handleAllocateTxFailure becomes a thin orchestrator that:

  1. Calls tryAllocateWords (single-buffer retry)
  2. On success: pushes the ledger entry, logs via emitAllocTxFallbackLog, returns success(fallback_words)
  3. On failure: gathers total/allocated/reserved/available via existing accessors, calls emitAllocTxFailureDiagnostic(...), returns expected::failure(RmtMemoryError::INSUFFICIENT_TX_MEMORY)

The helpers are file-local so the linker drops them entirely in builds that don't reference handleAllocateTxFailure (none today, but defensive).

Estimated savings

  • All 11 sstream construct + << chain + log_emit call expansions move into the helper bodies (which exist exactly once instead of being inlined into the 1,710 B caller).
  • Landing-pad metadata (~100-200 B × 7-11 sites) consolidates into the helper's single landing pad.
  • The helper itself costs ~400-600 B (shipped once).
  • Net savings on handleAllocateTxFailure: ~800-1200 B (the symbol should shrink to ~500-900 B, mostly the expected::failure glue + the tryAllocateWords/push_back calls + parameter marshalling).

Logging preservation

Logging output is identical — same FL_WARN/FL_LOG_RMT macros, same message templates, same severity, same __FILE__/__LINE__ (the file/line will now point at the helper, which is fine — these are diagnostic warnings, not stack traces). The helpers respect FASTLED_LOG_RUNTIME_ENABLED, matching today's gating on the diagnostic block.

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