Skip to content

bloat: consolidate error log sites in ChannelEngineRMTImpl::reconfigureForNetwork + createChannel through expected<>::failure #2983

Closed

Description

@zackees

Tracked in #2974 (TBD-D). Compounds with #2975 (TBD-A, noexcept dtors).

Targets

Function Size File / line
fl::ChannelEngineRMTImpl::createChannel(...) 2,198 B src/platforms/esp/32/drivers/rmt/rmt_5/channel_driver_rmt.cpp.hpp:553
fl::ChannelEngineRMTImpl::reconfigureForNetwork() 2,378 B src/platforms/esp/32/drivers/rmt/rmt_5/channel_driver_rmt.cpp.hpp:1153

Class is defined inline in channel_driver_rmt.cpp.hpp; there is no separate rmt_engine.h for this type. expected<T, E> lives in src/fl/stl/expected.h.

Call-site audit (post-#2890 verification)

createChannel(...) — 553-846

Line Kind Path
626 FL_WARN Informational: TX/RX DMA conflict, disabling DMA
650 FL_WARN Error: memMgr.tryAllocateTx fail (DMA)
693 FL_WARN Error: DMA created but memMgr.allocateDMA fail
710 FL_WARN Error: encoder creation fail (DMA)
729 FL_WARN Error: DMA channel creation fail, falling back
746-751 FL_WARN x4 Error: tryAllocateTx fail (non-DMA) + 3 diag lines
835 FL_WARN Error: encoder creation fail (non-DMA)
843 FL_WARN Informational (always-on): "Channel created on GPIO ..."
(plus ~8 FL_LOG_RMT info sites at 631, 638, 668, 720, 758, 766, 771, 817 - already no-op under release verbosity)

That is 6 distinct error-exit FL_WARN sites in the cold paths (excluding the always-on success warn at 843, which is a separate concern - see below), plus the diagnostic 3-line FL_WARN cluster at 746-751.

reconfigureForNetwork() — 1153-1261

Line Kind Path
1234 FL_WARN Error: registerChannelCallback failed after recreate
1256 FL_WARN Error: createChannel(...) returned false during network reconfig
(plus 8 FL_DBG sites at 1162, 1172, 1177, 1182, 1187, 1204, 1253, 1260 - already gated by FASTLED_HAS_DBG)

Only 2 FL_WARN sites in reconfigureForNetwork itself. The bulk of its 2,378 B is the inlined createChannel body it pulls in transitively (the if (createChannel(&state, state.pin, state.timing, 0)) at line 1231), plus the surrounding teardown sequence. The bloat-investigation report already noted at docs/SLIM_ESP32S3.md:136 that reconfigureForNetwork's 8 FL_DBG are gated and only the 2 FL_WARN are live in release.

Why the 2,378 B figure for reconfigureForNetwork is misleading

The naive call-site count (2 FL_WARN) does not explain ~2.4 KB. The actual cost source is:

  1. Per-call-site inline cost of the 2 FL_WARN sites: ~200-300 B each = 400-600 B
  2. Exception landing pad metadata for each call site (string/sstream dtors not noexcept): another ~100-200 B each = 200-400 B
  3. The teardown sequence between L1206-1250 (deleteEncoder / deleteChannel / freeDMA / free / re-register-callback / undo-on-fail), which has its own per-cleanup-branch landing pads
  4. Each call to createChannel from L1231 brings in the full inlined createChannel exit cleanup machinery once (the body itself is shared, but the post-call cleanup at 1235-1250 duplicates much of createChannel's own cleanup)

For createChannel, the 6 FL_WARN sites x (200-300 B inline + 100-200 B EH landing pad) = 1.8-3.0 KB of pure logging+EH overhead, which fully explains its 2,198 B.

Proposed refactor pattern

Step 1: extend expected<>::failure with structured context

The existing API at src/fl/stl/expected.h:115 is:

static expected failure(E err, const char* msg = nullptr) FL_NOEXCEPT;

This is already sufficient for string-literal error messages (no allocation, no landing pad if string ctor is noexcept). The remaining gap is: error sites today log formatted data ("Memory manager TX allocation failed for channel " << id). We need either:

(a) A failure(code, sstream&&) overload that captures a formatted sstream and emits at the single log call site, OR
(b) An error context carrier - e.g. struct ErrorCtx { E code; const char* literal; uint32_t arg0; uint32_t arg1; } - that the bottom-of-function log site formats once.

Option (b) is strictly cheaper in flash because it avoids materializing a fl::string per failed path; the format string lives in rodata at the single log site.

Step 2: convert createChannel return type and consolidate

Before (sketch):

bool createChannel(ChannelState* state, int pin, const ChipsetTiming& timing, fl::size dataSize = 0) {
    if (!memMgr.tryAllocateTx(...)) {
        FL_WARN("Memory manager TX allocation failed for DMA channel " << id);
        return false;                                                              // site 1
    }
    if (dma_success && !memMgr.allocateDMA(...)) {
        FL_WARN("DMA hardware creation succeeded but memory manager allocation failed");
        cleanup_dma();
        return false;                                                              // site 2
    }
    if (dma_success && !state->encoder) {
        FL_WARN("Failed to create encoder for DMA channel");
        cleanup_dma_encoder();
        return false;                                                              // site 3
    }
    // ... 3 more error sites in non-DMA path ...
    return true;
}

After (sketch):

enum class RmtCreateErr : u8 {
    OK, TX_ALLOC_FAIL_DMA, DMA_HW_ALLOC_MISMATCH, ENCODER_FAIL_DMA,
    TX_ALLOC_FAIL_NONDMA, CHANNEL_CREATE_FAIL, ENCODER_FAIL_NONDMA,
};

// Cold helper, FL_NO_INLINE, single FL_WARN site.
FL_NO_INLINE static void logCreateChannelFail(RmtCreateErr e, int pin,
                                              u8 chId, fl::size words) FL_NOEXCEPT;

fl::expected<void, RmtCreateErr> createChannelImpl(ChannelState* state, int pin,
                                                    const ChipsetTiming& timing,
                                                    fl::size dataSize) FL_NOEXCEPT {
    using E = fl::expected<void, RmtCreateErr>;
    if (!memMgr.tryAllocateTx(...))
        return E::failure(RmtCreateErr::TX_ALLOC_FAIL_DMA);
    if (dma_success && !memMgr.allocateDMA(...)) {
        cleanup_dma();
        return E::failure(RmtCreateErr::DMA_HW_ALLOC_MISMATCH);
    }
    // ... etc; each site is now a single 8-byte return ...
    return E::success();
}

bool createChannel(ChannelState* state, int pin, const ChipsetTiming& timing,
                   fl::size dataSize = 0) FL_NOEXCEPT {
    auto r = createChannelImpl(state, pin, timing, dataSize);
    if (!r) {
        logCreateChannelFail(r.error(), pin, state->memoryChannelId, /*words=*/0);
        return false;
    }
    return true;
}

The hot createChannel body loses its ~6 inline sstream+log_emit bursts; the single logCreateChannelFail lives out-of-line in .text.cold with one FL_WARN that switches on RmtCreateErr.

The same pattern applies to reconfigureForNetwork (collapse the 2 FL_WARN at 1234 + 1256 plus the rollback cleanup block at 1235-1250 into a single cold helper).

Independent fix: gate the always-on success log

createChannel line 843 is an unconditional FL_WARN("[RMT TX] Channel created on GPIO ...") - this fires on every successful channel creation in release builds. Should be demoted to FL_LOG_RMT (gated by FASTLED_LOG_VERBOSITY) since the "you successfully allocated a channel" message is not a warning. Independent ~200-300 B save.

Estimated savings

Item Estimate
createChannel: 5 cold-path FL_WARN sites collapsed to 1 (8 - 1) ~1.2-1.8 KB
createChannel: demote always-on L843 FL_WARN to FL_LOG_RMT ~200-300 B
reconfigureForNetwork: 2 FL_WARN + rollback block collapsed ~400-600 B
Combined ~1.8-2.7 KB (conservative ~800-1200 B if EH landing pads do not collapse)

Savings compound with #2975 (noexcept dtors): once string/sstream dtors are noexcept, the remaining single log site loses its landing pad entirely, pushing the per-site cost down to pure call overhead.

Acceptance

  • createChannel and reconfigureForNetwork each contain at most one FL_WARN/FL_ERROR call site in their direct body (cold helpers may have their own single site)
  • expected<>::failure API supports either a structured error context or an sstream rvalue (decided during impl)
  • Existing log content is preserved at FASTLED_LOG_VERBOSITY=1 - error code -> message mapping in the cold helper carries all the info the per-site FL_WARN(...) chains carry today
  • Symbol report on ESP32-S3 Blink shows createChannel < 1,400 B and reconfigureForNetwork < 1,600 B (i.e. each drops ~800 B+)
  • Tests still pass; behavior under sustained allocation failure (mAllocationFailed path) unchanged

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