Repository navigation
perf(variant): collapse trivial-destructor / trivial-copy thunks (#3249 / #3244 Tier 3G) - #3266
Conversation
… / #3244 Tier 3G) Two-axis collapse of the per-(variant x T x op) thunk explosion in `fl::variant<Ts...>`: 1. Thunks moved out of `variant<Ts...>` into `fl::detail::` namespace scope. Previously each variant instantiation had its own mangled `variant<Types...>::destroy_fn<T>` / `copy_fn<T>` / `move_fn<T>` symbols; now `typed_destroy_thunk<T>` / `typed_copy_thunk<T>` / `typed_move_thunk<T>` are shared across every variant instantiation that contains `T`. 2. Trivially-copyable T's collapse further to a single shared `variant_noop_destroy` + per-size `variant_trivial_copy_n<sizeof(T)>` / `variant_trivial_move_n<sizeof(T)>` thunks. All `bool`, `i64`, `float`, `nullptr_t`, etc. alternatives across every variant in the link now share one noop-destroy and a handful of size-keyed memcpy bodies. `is_trivially_copyable<T>` is the gate. Per the C++ standard trivially-copyable implies trivially-destructible, so the destroy collapse is correctness-preserving. The issue body called for `is_trivially_destructible` -- we don't have that trait in `fl/stl/type_traits.h` yet; `is_trivially_copyable` is slightly more conservative but captures every primitive alternative the audit named (json_value's bool / i64 / float / nullptr_t). `visit_fn<T, Visitor>` stays per-(T x Visitor) -- it dispatches into the visitor's typed `operator()(T const&)` and the collapse would change behavior. Out of scope per acceptance. Flash impact (LPC845 / AutoResearch): - Master baseline: 46,840 B - This branch: 46,404 B - Delta: **-436 B** flash, **-4,468 B** ELF .text - Per-variant thunk symbols: 55 -> 0 (all routed through shared namespace-scope thunks) The 436 B flash drop is below the 800 B acceptance target. Per the meta #3244 lesson-learned: AutoResearch exercises only 2-3 distinct variant<...> instantiations against json_value's 10-alternative variant; the audit's predicted 2-3 KB assumes json-heavy Large-memory builds. The structural collapse is correct and the infrastructure is in place to compound with the next tier of variant-heavy sketch. Test coverage (variant unit tests + the json parsing path that exercises the trivial alternatives) is green. Refs #3249, #3244 (Tier 3G), #3235. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
📝 WalkthroughWalkthrough
ChangesVariant lifetime thunk collapsing
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/fl/stl/variant.h (1)
334-343: Precondition enforced by all callers — adding a debug assertion is optional.
copy_construct_from()requires a non-empty source (precondition:other._tag != Empty), which is enforced by all callers viaif (!other.empty())guards in the copy constructor (line 136) and copy assignment operator (line 154). The function does not guard internally because the contract is upheld at call sites.Adding a debug assertion (
FL_ASSERT(other._tag != Empty)) would be defensive programming and help catch misuse during development:Optional: add debug assertion for precondition
void copy_construct_from(const variant &other) FL_NOEXCEPT { + FL_ASSERT(other._tag != Empty); FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING(array-bounds)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/fl/stl/variant.h` around lines 334 - 343, Add a debug assertion to the copy_construct_from method to enforce the precondition that the source variant is not empty. At the beginning of the copy_construct_from function (right after the opening brace), add an FL_ASSERT statement that checks other._tag is not equal to Empty, which will help catch misuse during development and make the contract explicit within the function itself.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/fl/stl/variant.h`:
- Around line 334-343: Add a debug assertion to the copy_construct_from method
to enforce the precondition that the source variant is not empty. At the
beginning of the copy_construct_from function (right after the opening brace),
add an FL_ASSERT statement that checks other._tag is not equal to Empty, which
will help catch misuse during development and make the contract explicit within
the function itself.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 016fe191-549b-4a55-a681-dd143dd473ed
📒 Files selected for processing (1)
src/fl/stl/variant.h
Summary
Two-axis collapse of the per-(variant × T × op) thunk explosion in
fl::variant<Ts...>:Namespace extraction. Thunks move out of
variant<Ts...>intofl::detail::namespace scope. Previously each variant instantiation had its own mangledvariant<Types...>::destroy_fn<T>/copy_fn<T>/move_fn<T>symbols; nowtyped_destroy_thunk<T>/typed_copy_thunk<T>/typed_move_thunk<T>are shared across every variant instantiation containingT.Trivial collapse. Trivially-copyable T's collapse further to a single shared
variant_noop_destroy+ per-sizevariant_trivial_copy_n<sizeof(T)>/variant_trivial_move_n<sizeof(T)>thunks. Allbool,i64,float,nullptr_t, etc. alternatives across every variant in the link now share one noop-destroy and a handful of size-keyed memcpy bodies.visit_fn<T, Visitor>stays per-(T × Visitor) -- it dispatches into the visitor's typedoperator()(T const&)and the collapse would change behavior. Out of scope per acceptance.Trait choice
The issue body called for
is_trivially_destructible<T>as the gate. That trait is not infl/stl/type_traits.h;is_trivially_copyable<T>is used instead. Per the C++ standard, trivially-copyable implies trivially-destructible, so the destroy collapse is correctness-preserving (slightly more conservative than the issue's wording but captures every primitive alternative the audit named in json_value'sbool/i64/float/nullptr_t).Flash impact (LPC845-BRK / AutoResearch)
variant<...>::destroy_fn/copy_fn/move_fn)Honest caveat re acceptance bullet
≥ 800 B net flash dropThe 436 B drop is below the acceptance target. Same documented pattern as #3258 (#3246) and #3261 (#3251) before it: AutoResearch's LowMemory mode exercises only ~3 distinct
variant<...>instantiations against json_value's 10-alternative variant, so the audit-predicted 2-3 KB (which assumed json-heavy Large-memory builds) only partially materializes here.The structural collapse is verified -- 55 per-variant thunk symbols in master → 0 in branch; everything is routed through the shared namespace-scope thunks (verified via
llvm-nmsymbol diff in the commit body).Test plan
bash test --cpp-- 330/330 unit tests pass.bash test variant-- variant-specific tests pass; semantics preserved.bash lint --cpp-- 0 violations.llvm-nmsymbol audit shows the 55 oldvariant<...>::destroy_fn/copy_fn/move_fnsymbols are gone and thetyped_destroy_thunk<T>/typed_copy_thunk<T>/typed_move_thunk<T>shared symbols + per-sizevariant_trivial_copy_n<N>thunks are present.Files
src/fl/stl/variant.h-- 114 added, 33 removed (1 file modified)Refs #3249, #3244 (Tier 3G), #3235.
🤖 Generated with Claude Code
Summary by CodeRabbit