Skip to content

[Type-Erasure] variant: collapse trivial-destructor / trivial-copy thunks (#3244 Tier 3G) #3249

Closed

Description

@zackees

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.h so that:
    • destroy_fn<T> collapses to a single shared noop_destroy when is_trivially_destructible<T>::value is true.
    • copy_fn<T> and move_fn<T> collapse to a single per-size trivial_copy<sizeof(T)> thunk when is_trivially_copyable<T>::value is true.
    • Non-trivial T's keep their own per-T thunks (correctness-preserving).
  • Existing tests/test_variant*.cpp host 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_fn thunks must stay (they encode T-specific dispatch to the visitor's operator()(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

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.

Refs #3244, #3235 (Tier 3G).

Activity

  1. zackees commented on Jun 19, 2026

    @zackees
    MemberAuthor

    ⚠ /clud-fix validation failed. This comment is the deliverable, not a merged PR. The refactor was implemented and measured against the acceptance criteria; it does not deliver the predicted savings on either tested target.

    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 shared noop_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 a pick_destroy_fn<T>() / pick_copy_fn<T>() / pick_move_fn<T>() selector + new is_trivially_destructible trait in fl/stl/type_traits.h), and the trait was verified correct via in-source static_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 empty destroy_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 called noop_destroy. The bytes that look like duplication in the bloat report are already deduped at link time.

    Reproduction status

    • LPC845-BRK / AutoResearch: 49,068 B (master) → 49,068 B (refactor) — 0 B change.
    • Teensy 4 / AutoResearch: 285,844 B (master) → 285,844 B (refactor) — 0 B change.

    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.
    • Per-T 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_destroy doesn't appear AND destroy_fn<bool> does is the smoking gun: the linker aliased what the source called noop_destroy onto 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:

    1. Re-baseline the audit against a build that ICF is not deduping. If a target without LTO or with -fno-tree-icf showed 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 each destroy_fn<T> as unique code; the actual machine code is identical and ICF-foldable.

    2. Collapse the visit dispatch instead of the lifecycle dispatch. visit_fn<T, Visitor> thunks call visitor.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.

    3. 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. A tag → op packed 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_destructible trait) 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.

  2. zackees commented on Jun 23, 2026

    @zackees
    MemberAuthor

    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.

  3. moved this from Triage to Done in FastLED Trackeron Jun 23, 2026
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