Skip to content

Cut ClocklessIdf5 bloat via fl::sort_small for bounded ≤16-elem call sites (#2886) #2907

Closed

Description

@zackees

Goal

Cut bytes attributed to fl::ClocklessIdf5<...>::ClocklessIdf5() (the top-1 symbol in the latest audit at 48,694 B — see #2886#issuecomment-4641413123) by replacing inappropriate fl::sort (full quicksort) calls in the ctor's transitive closure with a new fl::sort_small helper that maps directly to fl::detail::insertion_sort.

Mechanism, not algorithm. This is an algorithmic-bloat fix: the call sites in scope all operate on small bounded containers (≤16 elements, often ≤4 in practice), and the runtime-dispatch into fl::detail::quicksort_impl always falls through to insertion sort anyway. The compiler can't prove the bound is small, so it emits the whole quicksort + partition + heap_sort instantiation as dead code. A direct call to fl::sort_small removes that dead code.

Parent: #2886 — first algorithmic-bloat reduction targeting the top-1 symbol identified in the measured audit.

Why this is a real win, not a wash

fl::sort at src/fl/stl/algorithm.h:564 dispatches to fl::detail::quicksort_impl at line 411, which contains:

if (last - first <= 16) {  // Use insertion sort for small arrays
    insertion_sort(first, last, comp);
    return;
}
Iterator pivot = partition(first, last, comp);
quicksort_impl(first, pivot, comp);
quicksort_impl(pivot + 1, last, comp);

The size check is runtime, not constexpr. Per-template-parameter instantiation of fl::sort therefore emits:

  • quicksort_impl (recursive)
  • partition + median-of-three pivot selection
  • heap_sort fallback (transitively reachable from quicksort)
  • insertion_sort (only the actually-fired path)

For a call site that's provably bounded ≤16 (e.g. a fl::vector_inlined<T, 16>), every byte except insertion_sort is dead code.

In-scope call sites (all reachable from ClocklessIdf5 ctor)

Site Container Comment from source
src/fl/channels/manager.cpp.hpp:137 mDrivers (4-driver max) "Only 1-4 drivers expected, so sorting on insert is negligible"
src/fl/channels/manager.cpp.hpp:250 mDrivers (same) sort-on-removal codepath
src/platforms/esp/32/drivers/rmt/rmt_5/channel_driver_rmt.cpp.hpp:362 fl::vector_inlined<ChannelDataPtr, 16> "Sort: smallest strips first (helps async parallelism)"

All three sites are reachable from BusTraits<Bus::RMT>::registerWithManager() (called at idf5_clockless.h:45) which is the entry point that anchors the 48 KB map-derived attribution to ClocklessIdf5.

Out of scope (deferred to a follow-up PR)

Seven other fl::sort sites exist in src/ that also operate on small containers, but they're either outside the ClocklessIdf5 transitive closure or in alternate drivers (parlio/uart):

  • src/fl/math/filter/alpha_trimmed_mean_impl.h:30
  • src/fl/math/filter/median_filter_impl.h:39
  • src/fl/stl/function.h:486
  • src/platforms/esp/32/drivers/parlio/channel_driver_parlio.cpp.hpp:171, 203
  • src/platforms/esp/32/drivers/uart/channel_driver_uart.cpp.hpp:117
  • src/platforms/shared/ui/json/ui_manager.cpp.hpp:123

Migrating these is a one-line follow-up per site; deferred so the measured savings in this PR are clearly attributable to ClocklessIdf5's closure.

Deliverable

src/fl/stl/algorithm.h — add fl::sort_small

New public template, sibling to fl::sort / fl::stable_sort:

// Direct call to insertion sort — meant for call sites with statically
// bounded input (typically ≤ 16-32 elements). Skips the runtime fall-through
// from quicksort_impl, so the compiler doesn't instantiate the quicksort +
// partition + heap_sort body at all. See #2886 for the bloat motivation.
template <typename Iterator, typename Compare>
void sort_small(Iterator first, Iterator last, Compare comp) FL_NOEXCEPT {
    detail::insertion_sort(first, last, comp);
}

template <typename Iterator>
void sort_small(Iterator first, Iterator last) FL_NOEXCEPT {
    typedef typename fl::remove_reference<decltype(*first)>::type value_type;
    detail::insertion_sort(first, last,
        [](const value_type& a, const value_type& b) { return a < b; });
}

Migrate 3 call sites

  • manager.cpp.hpp:137 — fl::sort(mDrivers.begin(), mDrivers.end()) → fl::sort_small(...)
  • manager.cpp.hpp:250 — same
  • channel_driver_rmt.cpp.hpp:362 — fl::sort(sorted.begin(), sorted.end(), [...] FL_NOEXCEPT { ... }) → fl::sort_small(sorted.begin(), sorted.end(), [...] FL_NOEXCEPT { ... })

No tests, no API doc changes

fl::sort_small has the same semantic contract as fl::sort (same return = nothing, same compare-by-<, same in-place mutation). Behavior changes only in O() characteristics (insertion-sort O(n²) worst case — fine when n is bounded). No public API surface change for users.

Acceptance

  1. src/fl/stl/algorithm.h has fl::sort_small (two overloads, with and without comparator).
  2. The three call sites listed above call fl::sort_small instead of fl::sort.
  3. bash lint --cpp passes (no new violations).
  4. The regression-gate test (tests/test_esp32s3_bloat_regression.py) passes — the new baseline at 350568 either stays or ratchets DOWN. (If it ratchets, lower the file in the same PR.)
  5. Behavior preserved: bash test --cpp passes (the existing Channels / ChannelManager / RMT5 tests cover the sort dispatch).

Expected savings

Conservative estimate: 1-3 KB per template instantiation of the eliminated quicksort body. Across the three sites (each is a distinct Compare type → distinct instantiation), expected total reduction is 3-9 KB measured on the ESP32-S3 NEOPIXEL Blink build. That ratchets the Stage 6 gate's baseline file down accordingly.

Related

🤖 Generated with Claude Code

Activity

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