Skip to content

perf(builtin): remember the last field reference - #532

Open
rcoreilly wants to merge 1 commit into
s4wave:masterfrom
cogentcore:perf/field-ref-cache
Open

rcoreilly wants to merge 1 commit into
s4wave:masterfrom
cogentcore:perf/field-ref-cache

Conversation

@rcoreilly

Copy link
Copy Markdown
Contributor

fieldRef keeps each field reference in a Map inside a WeakMap keyed by the target, so that taking the address of a struct field always yields the same pointer. Every call does both lookups. In practice a hot loop takes the address of the same field over and over, for example through a method that returns &s.field: Cogent Lab tensors' Shape() returns &tsr.shape, and code calls it for every element access.

This remembers the target, key and reference of the last fieldRef call and returns that reference directly when both match. References are created once and never replaced or removed, so the remembered one is always the same object the maps hold, and pointer identity is unchanged. The only side effect is that the last target stays reachable until the next fieldRef call.

In a benchmark of tensor element access through Shape() under Node 26, an access takes 24 ns instead of 33 ns.

A new unit test checks that interleaved fieldRef calls on different targets and fields still return the same reference for each target and field. The full Go test suite, typecheck and vitest pass.

fieldRef keeps each field reference in a map in a WeakMap, so that taking
the address of a field always gives the same pointer, and each call looks
up both maps. A loop usually takes the address of the same field again
and again, for example through a method that returns &s.field, as
Cogent Lab tensors' Shape method does for every element access.

Remember the target, key and reference of the last call, and return the
reference without the lookups when they match. References are never
replaced, so it is always the one the maps hold. Accessing a tensor
element through Shape() takes 24 ns instead of 33 ns under Node 26.

Signed-off-by: Randall C. O'Reilly <oreilly@ucdavis.edu>

This branch has not been deployed

No deployments
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