Repository navigation
Description
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 selectionheap_sortfallback (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:30src/fl/math/filter/median_filter_impl.h:39src/fl/stl/function.h:486src/platforms/esp/32/drivers/parlio/channel_driver_parlio.cpp.hpp:171, 203src/platforms/esp/32/drivers/uart/channel_driver_uart.cpp.hpp:117src/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— samechannel_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
src/fl/stl/algorithm.hhasfl::sort_small(two overloads, with and without comparator).- The three call sites listed above call
fl::sort_smallinstead offl::sort. bash lint --cpppasses (no new violations).- The regression-gate test (
tests/test_esp32s3_bloat_regression.py) passes — the new baseline at350568either stays or ratchets DOWN. (If it ratchets, lower the file in the same PR.) - Behavior preserved:
bash test --cpppasses (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
- Meta: ESP32-S3 Blink bloat reduction (file:line concrete plan) — supersedes #2885 #2886 — parent meta + the audit comment that surfaced
ClocklessIdf5as the top-1 target. - chore(bloat): ratchet baseline 388380 -> 350568 + flip Stage 1 row to ✅ measured #2904 — the ratchet PR that established
350568as the current baseline this PR lowers further. - tools(bloat): ship tests/measure_esp32s3_opt_ins.py multi-config audit driver #2906 — the multi-config measurement driver that can re-measure post-PR savings on demand.
🤖 Generated with Claude Code
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
chore(bloat): ratchet baseline 350568 -> 350127 (post-#2908 fl::sort_…
perf(esp32): add fl::sort_small for bounded containers; cut Clockless…