Skip to content

perf(reflect): read struct fields and check assignability cheaply - #543

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

paralin merged 1 commit into
s4wave:devfrom
cogentcore:perf/reflect-field-access

Conversation

@rcoreilly

Copy link
Copy Markdown
Contributor

Two costs that showed up in profiles of a reflect-heavy program (a Go interpreter that reads and writes struct fields through reflect):

Value.Field built a complete StructField for the field, copying its Index slice, only to read the field's Type. For a StructType it now reads the field's type and storage key straight from the type's descriptors (a new StructType.fieldType alongside fieldKey); other struct types keep the old path.

typeAssignableTo computed and compared the identity keys of both types, which walks and stringifies them, even when the two were the same Type object, as they are for most Value.Set calls. It now returns true for identical types before building any key.

In the interpreter's struct method benchmark these took about 15% off (2.23 s to 1.91 s). The reflect unit tests and the reflect, struct, json and assignment compliance tests pass.

Value.Field made a whole StructField, copying its index slice, only to
read the field's type; for a StructType it now reads the type and the
storage key of the field directly.

typeAssignableTo built and compared the identity keys of the two types
even when they were the same type, as they are for most Value.Set calls;
a type is now assignable to itself without them.

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

paralin commented Oct 25, 2026

Copy link
Copy Markdown
Member

Landed on master as 90d3b6d, rebased onto the batch #532 to #550, with regenerated fixtures in fca8071. Added a blank line before the identity check in typeAssignableTo.

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