Repository navigation
Closed
Description
Parent: #3244 (Phase 3 burn-down).
Goal
Collapse fl::variant<Ts...>::destroy_fn<T> / copy_fn<T> / move_fn<T> thunk explosion for trivially-destructible / trivially-copyable T's. Today every (T × op) pair emits its own thunk (~50-164 B each). On json_value's 10-alternative variant × N visitors = ~330 thunks totaling ~6.7 KB on LPC845. Most of those thunks for trivially-typed alternatives are doing the same work (no-op destroy, memcpy copy/move).
Reproduction (baseline)
bash bloat lpc845brk --example AutoResearch --top 30
# Then grep the report for variant<...>::destroy_fn / copy_fn / move_fn entries.
# Audit showed ~245 thunks totalling ~6,724 B on LPC845.
Acceptance
- PR modifies
fl/stl/variant.hso that:destroy_fn<T>collapses to a single sharednoop_destroywhenis_trivially_destructible<T>::valueis true.copy_fn<T>andmove_fn<T>collapse to a single per-sizetrivial_copy<sizeof(T)>thunk whenis_trivially_copyable<T>::valueis true.- Non-trivial T's keep their own per-T thunks (correctness-preserving).
- Existing
tests/test_variant*.cpphost tests pass unchanged. - Measured savings on LPC845-BRK / AutoResearch: ≥ 800 B net flash drop. Audit predicted ~2-3 KB; the higher figure assumed json-heavy Large-memory targets.
Design reference
The container type-erasure audit run during #3235 identified this as the highest variant-level leverage point that doesn't change visit semantics. The split is:
- Per-T
visit_fnthunks must stay (they encode T-specific dispatch to the visitor'soperator()(T const&)). - Per-(T × op) lifecycle thunks (destroy / copy / move) can collapse for trivial T's because the work is uniform (no-op or memcpy).
The pattern matches what PR #3238 (fl::function Option B) already does internally with trivial_manager<sizeof(F)> vs non_trivial_manager<F>.
Risk
is_trivially_destructible/is_trivially_copyabletraits already exist infl/stl/type_traits.h(verified during perf(fl::function): Option B refactor -- single SBO + invoker + manager (#3235 Tier 1B, -9.2 KB Teensy 4) #3238 work).- Some variant alternatives in
json_valuecarryfl::string,fl::vector<T>,fl::shared_ptr<json_value>-- those are non-trivial and would keep their per-T thunks. Onlybool,i64,float,nullptr_tare candidates for the trivial-thunk collapse on thejson_value::variant_tside.
Out of scope
visit_fn<T, Visitor>collapse -- that's a deeper redesign covered by Tier 2E Phase 2 (separate sub-issue).- Variant-without-visit (e.g. tag-based dispatch only) -- redesign too invasive.
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
- StatusShow more project fieldsDone
Activity
zackees commentedon Jun 19, 2026
What is going wrong
The audit predicted ≥800 B (and possibly 2-3 KB on json-heavy Large-memory) by collapsing per-T
destroy_fn<T>/copy_fn<T>/move_fn<T>thunks into a sharednoop_destroy/trivial_copy_n<sizeof(T)>/trivial_move_n<sizeof(T)>for trivially-destructible / trivially-copyable T's. The implementation was clean (enable_if overloads on apick_destroy_fn<T>()/pick_copy_fn<T>()/pick_move_fn<T>()selector + newis_trivially_destructibletrait infl/stl/type_traits.h), and the trait was verified correct via in-sourcestatic_assert(is_trivially_destructible<bool>::value)that built cleanly on both clang-host and arm-none-eabi-gcc.The linker has already done this dedup work via ICF. Identical Code Folding in LLVM lld (Teensy 4) and arm-none-eabi-ld (LPC845-BRK with
-flto) collapses all emptydestroy_fn<bool/float/long long/decltype(nullptr)>bodies — every one is just a trivial-destructor elision + return, identical machine code — into a single shared symbol. The names in the symbol table are misleading: the linker emits one of the deduped names (e.g.destroy_fn<bool>) as the canonical alias for what the source-level refactor would have callednoop_destroy. The bytes that look like duplication in the bloat report are already deduped at link time.Reproduction status
Acceptance gate (≥800 B net drop on LPC845-BRK / AutoResearch) cannot be met by this refactor.
Symbol-level evidence (clean rebuild, my branch):
noop_destroy: 0 symbols emitted (despite trait verified true and dispatch routed to it).trivial_copy_n/trivial_move_n: 0 symbols emitted.destroy_fn<bool>/<float>/<long long>still appear in the binary — but they are size-equal across variants, which is the ICF-fold pattern.That
noop_destroydoesn't appear ANDdestroy_fn<bool>does is the smoking gun: the linker aliased what the source callednoop_destroyonto whichever name was first encountered for the identical-body function.Planned fix (what would unblock this sub-issue)
This sub-issue is not actionable as specified. There are three follow-up directions; each would need its own filing:
Re-baseline the audit against a build that ICF is not deduping. If a target without LTO or with
-fno-tree-icfshowed the predicted bytes, the refactor would deliver. But every target FastLED currently ships with uses LTO. The audit's "245 thunks × 50-164 B = ~6.7 KB" treats eachdestroy_fn<T>as unique code; the actual machine code is identical and ICF-foldable.Collapse the visit dispatch instead of the lifecycle dispatch.
visit_fn<T, Visitor>thunks callvisitor.accept(*p)— that's per-T body with no ICF dedup possible. The Tier 2E Phase 2 sub-issue ([Type-Erasure] json: collapse JsonToInteger/Float/Bool/String visitors into json_convert_to_<X>_core (#3244 Tier 2E Phase 2) #3247, already closed via PR perf(json): JsonToType<T> dispatch through non-template cores (-2 KB Teensy 4; closes #3247) #3253) covered the JSON-side visitors. Other visitor-heavy variants (e.g. the parser tokens) might still leak symbols and would be a real target.Shrink the constexpr dispatch tables themselves. Each variant<...> emits a
static constexpr DestroyFn table[N]per variant per op. On Teensy 4, 275 lifecycle-related symbols (6.8 KB) is dominated by these tables, not the thunks. Atag → oppacked dispatch (no per-variant table) could deliver, but it's a deeper redesign — file as a separate ticket.Recommendation
Close this sub-issue as "not actionable: ICF already dedups." Open follow-ups for (2) and (3) above if Phase 4 wants to keep leveraging variant. The refactor's design itself (per-size shared
trivial_copy_n<N>, enable_if dispatch,is_trivially_destructibletrait) is sound and worth saving for a later landing where ICF behavior differs — but shipping it on master right now adds plumbing without measurable benefit.perf(variant): collapse trivial-destructor / trivial-copy thunks (#3249…
zackees commentedon Jun 23, 2026
Closed as resolved by merged PR #3266. The PR landed the requested variant lifecycle thunk collapse in src/fl/stl/variant.h, verified the per-variant destroy/copy/move thunk symbols dropped from 55 to 0, and passed variant/full C++ tests. The measured LPC845 flash win was 436 B, below the original 800 B target, but the structural optimization was accepted and merged with that caveat documented in the PR.