Repository navigation
perf: InternalValue is 32 bytes, a trivially copyable variant plus one intrusive owner (0140 P2) - #462
Merged
Conversation
…e intrusive owner (0140 P2) A value holds scalars, short strings, string views and typed pointers to what it owns in a trivially copyable std::variant (24 B), and owns those objects through one boost::intrusive_ptr<const ValueObject>. Copying a value is a memcpy and one refcount increment instead of a dispatch on its kind. Lists, maps, long strings, pairs, callables, renderers and undefined info are ValueObjects. The owner may be bigger than the object pointed to (a loop's frame, the 0154 block of lent item adapters), which replaces the aliasing shared_ptrs. SetParentData makes an Anchor object instead of a second pointer. Visitors get short strings as std::basic_string (built in its SSO buffer), so a view a visitor keeps never points into a value. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012zgKj9cL7pJGk1EKHf5xPR
clang with libstdc++/libc++ instantiates ~vector<InternalValue> from the in-class body while InternalValue is still incomplete. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012zgKj9cL7pJGk1EKHf5xPR
rmorozov
force-pushed
the
claude/project-thread-9pqgy9
branch
from
October 11, 2026 14:58
a4a5995 to
983aede
Compare
… no item libc++'s stable_sort leaves moved-from elements in the range when the comparator throws (mixed types), and a moved-from InternalValue is now undefined; the fallback pass then saw those instead of the items. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012zgKj9cL7pJGk1EKHf5xPR
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012zgKj9cL7pJGk1EKHf5xPR
rmorozov
marked this pull request as ready for review
October 11, 2026 18:01
rmorozov
pushed a commit
that referenced
this pull request
Oct 11, 2026
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KMcWcpGwhiaEs33YjAgEV4
rmorozov
added a commit
that referenced
this pull request
Oct 11, 2026
Claude-Session: https://claude.ai/code/session_01KMcWcpGwhiaEs33YjAgEV4 Co-authored-by: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requested by Ruslan · project thread
Before:
InternalValuewas 72 bytes. Every copy, move and destroy went throughstd::variant's jump tables plus ashared_ptrfor parent data, even for an integer.After:
InternalValueis 32 bytes. A trivially copyablestd::variant(24 B) holds scalars, strings of up to 15 narrow characters, string views and typed pointers to what the value owns. Oneboost::intrusive_ptr<const ValueObject>(8 B) owns those objects. Copying a value is a memcpy plus one refcount increment. Render instructions drop 8-27% on every benchmark case.Task 0140, step P2 of docs/design/0140-internal-value-plan.md.
How
ValueObjects with an atomic intrusive count.loopvalue points at the accessor inside itsLoopFrame, which is now refcounted;ItemAdaptersblock, which is now aValueObject. This is the obligation recorded in 0154.shared_ptrs.ListRef/MapRefcarry the owner, soToAdapter()works on any view.SetParentDatamakes anAnchorobject that holds the value's own object and its parent. The value keeps pointing where it did.std::basic_stringbuilt in its SSO buffer, never as a view into the value. The first run of the suite showed why:Valueconversions were keeping views into dead short strings.uniquesorts positions instead of the items. A moved-from value is now undefined, and libc++'sstable_sortleaves moved-from items behind when the comparator throws on mixed types, whichuniquecatches before falling back.Deliberate changes
SetMarkupon a non-string does nothing. No caller marks a non-string.InternalValue("literal")is now deleted. It would otherwise convert to bool.Counts (local, GCC 13 Release,
bench/count.pyvs 6471f17)Load is -1..-6% everywhere, except mitsuhiko_table_wide (+0.94%) and plain_text (+0.33%).
P2 gates, from the CI "instruction counts vs base" job on 60b4b7e (base ea60b00, which includes 0160):
CI allocations per render: strings 975 → 1,285, chat_qwen 318 → 395, chat_llama 350 → 421, chat_mistral 416 → 491, html_autoescape 932 → 1,094, config_file 1,120 → 1,281.
Trade to accept or refuse: allocations rise where long strings are made (plan contradiction 2). A string over 15 characters takes its
StringObjectas well as its buffer. That misses the plan's +5% allocation bar. Bytes allocated fall on most of these cases. Task 0151, which builds strings straight into their object, is the fix.Roles and checks
CloneObject, which is now exported;scripts/preflight.shis clean.ListAdapter::ToValueListbody instantiated~vector<InternalValue>whileInternalValuewas still incomplete. GCC accepts this, and preflight builds with GCC only. Fixed by defining it after the class.filters.unique_mixed_types. This was a latent bug that the new move semantics exposed:uniquecaught the comparator's throw and then read the moved-from elementsstable_sortleft behind. Fixed by sorting positions.🤖 Generated with Claude Code
https://claude.ai/code/session_012zgKj9cL7pJGk1EKHf5xPR
Generated by Claude Code