[tools] go/ssa: avoid allocations on the MethodValue hit path

0 views
Skip to first unread message

Gopher Robot (Gerrit)

unread,
Sep 25, 2026, 4:19:16 PMSep 25
to Hung Chuang, Gerrit Bot, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, golang...@luci-project-accounts.iam.gserviceaccount.com, Alex Putman, Alan Donovan, golang-co...@googlegroups.com

Gopher Robot submitted the change

Change information

Commit message:
go/ssa: avoid allocations on the MethodValue hit path

MethodValue is called once per (call site, concrete type) pair by RTA
and VTA, and nearly every call finds a Function it already created.
Three things still allocated on that path.

A builder was declared before the critical section and captured by
the closure, so it escaped to the heap on every call. The closure now
returns a builder only when it created a Function; a hit returns nil
and allocates nothing.

waitForSharedFunction allocated a task and a channel before checking
whether the function was already built, and iterate then closed and
waited on them. It now checks first.

The method table was keyed by Id(), which builds a new string for
every unexported method. Key it by (package path, name) instead,
which is the same equivalence without the concatenation. LookupMethod
uses the same key, so its fast path stops allocating too.

TestMethodValueNoAlloc checks with testing.AllocsPerRun that neither
MethodValue nor LookupMethod allocates once the method exists, for an
exported method, an unexported one, and a wrapper.

Updates golang/go#81308
Change-Id: Ib2dbbec10561672166436a882d555ba9a078fe6a
GitHub-Last-Rev: e40b3b08dd2140f864fe105f72b808a0aedc078e
GitHub-Pull-Request: golang/tools#687
Reviewed-by: Alan Donovan <adon...@google.com>
Reviewed-by: Alex Putman <apu...@golang.org>
Auto-Submit: Alan Donovan <adon...@google.com>
Files:
  • M go/ssa/builder.go
  • M go/ssa/methods.go
  • M go/ssa/methods_test.go
Change size: M
Delta: 3 files changed, 132 insertions(+), 37 deletions(-)
Branch: refs/heads/master
Submit Requirements:
Open in Gerrit
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: merged
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: Ib2dbbec10561672166436a882d555ba9a078fe6a
Gerrit-Change-Number: 836845
Gerrit-PatchSet: 3
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Alex Putman <apu...@golang.org>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-CC: Hung Chuang <ispf...@gmail.com>
open
diffy
satisfied_requirement
Reply all
Reply to author
Forward
0 new messages