Skip to content

perf: keep generated values in the entity only (generate 1.2x faster) - #6

Merged
dasmig merged 1 commit into
mainfrom
context-refs
Oct 16, 2026
Merged

dasmig merged 1 commit into
mainfrom
context-refs

Conversation

@dasmig

@dasmig dasmig commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Phase 3, step 6. Output and signatures are unchanged (a patch release, 4.0.1).

Each component's value was copied into the generation context (std::map<std::wstring, std::any>: a deep copy of the value and a new key string) and also moved into the entity. The context now holds pointers to the entity's entry, key and value; the entries are reserved for every component before the loop, so they never move while the entity is generated. Lookup is a linear search over the values generated so far, as entity::get already does.

ctx.get<const T&>(key) already returned a reference into the stored value (it is std::any_cast<const T&>). It is now documented, with a test, so a component can read a large dependency (a struct with strings and vectors) without copying it. persona-generator will use it next.

clang 22, -O2 before after
generate (3 components) 2.5 µs 2.1 µs
generate_batch x100 104 µs 65 µs
persona-generator get_persona (8 components, lite) 46 µs 40 µs

All tests pass (5,279 assertions), and persona-generator's known answers are unchanged with this header.

🤖 Generated with Claude Code

Each component's value was copied into the generation context's map (a
deep copy of the std::any, plus a wstring key) and also moved into the
entity. The context now points at the entity's entry, whose storage is
reserved up front so it never moves. Same draws, so output and signatures
are unchanged.

get<const T&>() already returned a reference into the stored value; it is
now documented and tested, so a component can read a large dependency
without copying it.

Measured (clang 22, -O2): generate (3 components) 2.5 -> 2.1 us, a batch
of 100 104 -> 65 us; persona-generator's get_persona 46 -> 40 us. All tests
pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dasmig
dasmig merged commit f4e9825 into main Oct 16, 2026
18 checks passed
@dasmig
dasmig deleted the context-refs branch October 18, 2026 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant