Skip to content

perf(compiler): share reflect.Value values instead of cloning them - #542

Merged
paralin merged 1 commit into
s4wave:devfrom
cogentcore:perf/reflect-value-shared
Oct 25, 2026
Merged

paralin merged 1 commit into
s4wave:devfrom
cogentcore:perf/reflect-value-shared

Conversation

@rcoreilly

Copy link
Copy Markdown
Contributor

reflect.Value is a struct type, so the compiler cloned it on every copy: assignments, arguments and results, struct fields and slice elements that hold one, and the receiver of each of its value methods (markAsStructValue(cloneStructValue(v)).Kind()). A program that keeps reflect.Values in its data spends much of its time in those clones and in assignStruct; in a Go interpreter whose stack slots hold a reflect.Value they were the largest cost in the profile.

The override Value is never changed in place by its methods: its setters write through to the storage it refers to, which is what the copies of a Go reflect.Value share, and withAddressable is only called on freshly made Values. So copies can share the object. The compiler now treats reflect.Value as a shared value type (isSharedValueType): isStructValueType reports false for it, so copies, fields, and receivers take the value as it is and generated classes copy such a field by reference, while its zero value is still a new Value and conversion to an interface still boxes it with its type name, uncopied. A comment on the Value class records the invariant.

I kept the special case to reflect.Value rather than adding override metadata for it, since isStructValueType is used from many places that have no access to the override facts; it could become a meta.json field if other override types want the same treatment.

In the interpreter benchmark this took a loop of integer arithmetic from 942 ms to 383 ms and one of struct method calls from 6.7 s to 4.1 s. The full compliance suite and the gs unit tests pass; the regenerated outputs of the compliance tests that use reflect are included, and the new reflect_value_copies test checks copies of settable Values, struct fields, slice elements, field Values and zero values.

A reflect.Value is a struct, so the compiler cloned it on every copy: an
assignment, a call that passes or receives one, a struct or slice element
that holds one, and the receiver of each of its value methods. A program
that keeps reflect.Values in its data, such as an interpreter whose stack
slots hold them, spent much of its time in those clones.

The override Value is never changed in place by its methods (setters
write through to the storage it refers to, as the copies of a Go
reflect.Value share that variable), so its copies can share it. The
compiler now treats reflect.Value as such a shared value type: copies,
fields and receivers take the value as it is, and conversion to an
interface boxes it uncopied. Its zero value is still a new Value.

In a Go interpreter benchmark this took a loop of integer arithmetic from
942 ms to 383 ms and one of struct method calls from 6.7 s to 4.1 s.
reflect_value_copies checks copies, struct fields, slice elements, field
Values and zero values.

Signed-off-by: Randall C. O'Reilly <oreilly@ucdavis.edu>
@paralin
paralin changed the base branch from master to dev October 25, 2026 18:37
@paralin
paralin merged commit daa131c into s4wave:dev Oct 25, 2026
7 checks passed
@paralin

paralin commented Oct 25, 2026

Copy link
Copy Markdown
Member

Landed on master as bf99974, rebased onto the batch #532 to #550, with regenerated fixtures in fca8071.

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.

2 participants