diff options
author | Alan Donovan <adonovan@google.com> | 2023-02-23 13:49:41 -0500 |
---|---|---|
committer | Alan Donovan <adonovan@google.com> | 2023-02-25 00:08:52 +0000 |
commit | f98fce20d921851fe312582771bf14d4e9d8952f (patch) | |
tree | 6362acdba4a171df4142594de50245b1986b1af4 /internal | |
parent | 2e1074822d8f9c1dd080bf6511ee5e4fbe9c1295 (diff) | |
download | golang-x-tools-f98fce20d921851fe312582771bf14d4e9d8952f.tar.gz |
go/types/objectpath: add encoder type, to amortize allocation
This change adds a new encoder type with For method, that
is functionally equivalent to the old For function but avoids
the surprising cost of repeated calls to Scope.Names and
canonicalize (now called namedMethods), both of which
allocate and sorts a slice.
The former canonicalize function was previously applied to
interface types too, but this was costly and unnecessary
as they are already sorted.
The public API change will have to wait for proposal 58668
to be accepted and thus fix issue 51017; this change uses
the linkname hack to expose the function to x/tools to fix a
pressing bug in gopls.
See golang/go#58668
Updates golang/go#51017
Change-Id: Id3e8726517d31371b9376b0e47e320f8b6c5e085
Reviewed-on: https://go-review.googlesource.com/c/tools/+/470679
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'internal')
-rw-r--r-- | internal/typesinternal/types.go | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/internal/typesinternal/types.go b/internal/typesinternal/types.go index ce7d4351b..3c53fbc63 100644 --- a/internal/typesinternal/types.go +++ b/internal/typesinternal/types.go @@ -11,6 +11,8 @@ import ( "go/types" "reflect" "unsafe" + + "golang.org/x/tools/go/types/objectpath" ) func SetUsesCgo(conf *types.Config) bool { @@ -50,3 +52,10 @@ func ReadGo116ErrorData(err types.Error) (code ErrorCode, start, end token.Pos, } var SetGoVersion = func(conf *types.Config, version string) bool { return false } + +// NewObjectpathEncoder returns a function closure equivalent to +// objectpath.For but amortized for multiple (sequential) calls. +// It is a temporary workaround, pending the approval of proposal 58668. +// +//go:linkname NewObjectpathFunc golang.org/x/tools/go/types/objectpath.newEncoderFor +func NewObjectpathFunc() func(types.Object) (objectpath.Path, error) |