Optimize performance for binder, imports and packages (Rebased from sbalabanov/master) #1711
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is not my work. I just rebased #1702 against master.
This PR consists of multiple commits and is pure refactoring. It should maintain full backwards compatibility with the origin.
imports.goModuleRoot
is called for every type resolution. It tries to find go.mod file in the directory tree making a gazillion of requests to open file. While someone more familiar with the application design can come up with a better architecture to avoid this altogether, I added a caching layer on top of this function to only traverse file system once.binder.FindObject
traverses all definitions in a package and for all needed types it is effectively O(n^2). Make it to O(n) by caching all package top-level definitions in a hashtable.packages.Load
calls 'packages.LoadAll' which allocates a slice from the heap even when a single package is required. Added a fast path by checking a cache map before calling topackages.LoadAll
to avoid that. A possibly better solution is to refactor common parts ofpackages.Load
andpackages.LoadAll
into a separate function.For the targeted use case we achieved ~20% speed optimizations after those improvements.
Because functionality did not change, existing tests should cover it all so no new tests added.