Repository navigation
Conversation
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
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.
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.