Cherry Mui 3ebc191975 reflect: rework method Value representation
Currently, a method Value (aquired from e.g. Value.Method(i)) is
represented with its receiver and the index of the method, with
a special flag, instead of a regular func Value (as a method value
is a function). When the func value is needed (e.g. with
Value.Interface), it is lazily converted to the func Value
representation. The lazy conversion has a bad escape behavior: it
flows the Value to the a heap-allocated methodValue structure,
which causes the Value to escape. This in turn causes operations
like Value.Interface or Value.Convert escape its receiver to heap,
as they have a code path to handle the lazy conversion of method
Value, even if this code path is not taken.

To avoid the unnecessary escape, this CL changes the method Value
representation to eagerly creating the func Value. This eliminates
the code path with bad escape behavior in operations like
Value.Interface.

The lazy representation does have an advantage: Call on the method
Value is simple, as the method's code expects the receiver as the
first parameter, instead of a closure with the receiver pre-bound.
Keeping the receiver and the method index in the Value allows Call
to find the code pointer and pass the receiver directly. With the
func Value representation, Call has to unmarshal and re-marshal
the arguments in order to shift them by one. To avoid this problem,
we save the receiver and the method index in the closure structure
(i.e. methodValue structure), and set a special flag, so Call on
such a Value can still take the fast path, instead of going
through the closure. Benchmark result shows that Call on such a
Value is as fast as before, but creating the method Value is
slower as it involes an allocation. Creating a method Value and
then doing operations like Interface/Convert/Set is mostly
unchanged, as it just moves a lazy operation earlier.

Change-Id: I70beab0b816c8b0587a1b414e804ec3061c39e43
Reviewed-on: https://go-review.googlesource.com/c/go/+/799760
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
2026-07-21 08:21:38 -07:00
2026-07-20 12:51:53 -07:00
2026-07-09 11:24:04 -07:00
2024-08-09 14:54:31 +00:00
2024-07-22 17:45:27 +00:00

The Go Programming Language

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

Gopher image Gopher image by Renee French, licensed under Creative Commons 4.0 Attribution license.

Our canonical Git repository is located at https://go.googlesource.com/go. There is a mirror of the repository at https://github.com/golang/go.

Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file.

Download and Install

Binary Distributions

Official binary distributions are available at https://go.dev/dl/.

After downloading a binary release, visit https://go.dev/doc/install for installation instructions.

Install From Source

If a binary distribution is not available for your combination of operating system and architecture, visit https://go.dev/doc/install/source for source installation instructions.

Contributing

Go is the work of thousands of contributors. We appreciate your help!

To contribute, please read the contribution guidelines at https://go.dev/doc/contribute.

Note that the Go project uses the issue tracker for bug reports and proposals only. See https://go.dev/wiki/Questions for a list of places to ask questions about the Go language.

Description
Languages
Go 95.1%
Assembly 4.5%
C 0.2%