Repository navigation
perf(compiler): share reflect.Value values instead of cloning them - #542
Merged
Merged
Conversation
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>
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.
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.