Description
The objectpath.For function calls Scope.Names, which allocates and sorts a slice of all package members. If For
is called for every member of a package (or for every declaration within a package, which is much more), then it incurs an amount of allocation that is quadratic in the number of package members. Some packages (e.g. for CPU instructions sets) are very large, and objectpath loops become so slow they appear to be stuck.
I propose we add a new Encoder type with a For method so that new(Encoder).For gives the old behavior, but re-using the encoder across multiple calls for objects in the same package amortizes the expensive steps like Scope.Names.
See https://go.dev/cl/470679 for the expedient workaround in gopls (merged).
See https://go.dev/cl/470678 for the change to the public API.
Activity
gopherbot commentedon Feb 23, 2023
Change https://go.dev/cl/470679 mentions this issue:
go/types/objectpath: add encoder type, to amortize Scope.Names
gopherbot commentedon Feb 23, 2023
Change https://go.dev/cl/470678 mentions this issue:
go/types/objectpath: add Encoder type, to amortize Scope.Names
go/types/objectpath: add encoder type, to amortize allocation
findleyr commentedon Feb 28, 2023
To add additional context: we simply wouldn't be able to use the objectpath package in gopls without this optimization.
Furthermore, I think
Encoder
is a good name.rsc commentedon Mar 8, 2023
This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group
rsc commentedon Mar 15, 2023
The API is
Are there any concerns about this API?
rsc commentedon Mar 29, 2023
Based on the discussion above, this proposal seems like a likely accept.
— rsc for the proposal review group
23 remaining items