]> Cypherpunks repositories - gostls13.git/commit
cmd/compile: use method expression closures to implement bound method calls
authorKeith Randall <khr@golang.org>
Fri, 11 Feb 2022 20:58:37 +0000 (12:58 -0800)
committerKeith Randall <khr@golang.org>
Sat, 26 Mar 2022 20:36:29 +0000 (20:36 +0000)
commitf326964824a4001ea3964c256e70d61e7f663afa
tree9d32b33bdd44a4060f24821de9f376e7113accde
parent7fc38802e15be1a221290b0a9da1f587ace19488
cmd/compile: use method expression closures to implement bound method calls

When we have x.M(args) where x is a value of type parameter type, we
currently cast x to the bound of that type parameter (which is an interface)
and then invoke the method on that interface. That's pretty inefficient
because:
 1) We need to convert x to an interface, which often requires allocation.
    With CL 378178 it is at least stack allocation, but allocation nontheless.
 2) We need to call through wrapper functions to unpack the interface
    into the right argument locations for the callee.

Instead, let's just call the target directly. The previous CL to this one
added method expression closures to the dictionary, which is a simple
captureless closure that implements T.M for type parameter T and method M.
So to implement x.M(args) for x of type T, we use methodexpr(T,M)(x, args).
We just need to move x from the receiver slot to the first argument, and
use the dictionary entry to implement the polymorphism. This works because
we stencil by shape, so we know how to marshal x for the call even though
we don't know its exact type.

We should be able to revert CL 378178 after this one, as that optimization
will no longer be necssary as we're not converting values to interfaces
to implement this language construct anymore.

Update #50182

Change-Id: I813de4510e41ab63626e58bd1167f9ae93016202
Reviewed-on: https://go-review.googlesource.com/c/go/+/385274
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
src/cmd/compile/internal/noder/stencil.go