Skip to content

Pre-allocate parsedPatterns and cache some calculations #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/compiler/module/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type caches struct {
typeReferenceDirectiveCache *resolutionCache[*ResolvedTypeReferenceDirective]
packageJsonInfoCache *packagejson.InfoCache
resolvedTypeReferenceDirectiveLookupLocations map[*ResolvedTypeReferenceDirective]*LookupLocations

// Cached representation for `core.CompilerOptions.paths`.
// Doesn't handle other path patterns like in `typesVersions`.
parsedPatternsForPathsOnce sync.Once
parsedPatternsForPaths *parsedPatterns
}

func newCaches(
Expand Down
44 changes: 33 additions & 11 deletions internal/compiler/module/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ func (r *resolutionState) tryLoadModuleUsingPathsIfEligible() *resolved {
return continueSearching()
}
baseDirectory := getPathsBasePath(r.compilerOptions, r.resolver.host.GetCurrentDirectory())
pathPatterns := tryParsePatterns(r.compilerOptions.Paths)
pathPatterns := r.resolver.getParsedPatternsForPaths()
return r.tryLoadModuleUsingPaths(
r.extensions,
r.name,
Expand All @@ -1044,7 +1044,7 @@ func (r *resolutionState) tryLoadModuleUsingPathsIfEligible() *resolved {
)
}

func (r *resolutionState) tryLoadModuleUsingPaths(extensions extensions, moduleName string, containingDirectory string, paths *collections.OrderedMap[string, []string], pathPatterns parsedPatterns, loader resolutionKindSpecificLoader, onlyRecordFailures bool) *resolved {
func (r *resolutionState) tryLoadModuleUsingPaths(extensions extensions, moduleName string, containingDirectory string, paths *collections.OrderedMap[string, []string], pathPatterns *parsedPatterns, loader resolutionKindSpecificLoader, onlyRecordFailures bool) *resolved {
if matchedPattern := matchPatternOrExact(pathPatterns, moduleName); matchedPattern.IsValid() {
matchedStar := matchedPattern.MatchedText(moduleName)
if r.resolver.traceEnabled() {
Expand Down Expand Up @@ -1697,16 +1697,38 @@ func getPathsBasePath(options *core.CompilerOptions, currentDirectory string) st
}

type parsedPatterns struct {
matchableStringSet collections.OrderedSet[string]
matchableStringSet core.Set[string]
patterns []core.Pattern
}

func tryParsePatterns(paths *collections.OrderedMap[string, []string]) parsedPatterns {
// !!! TS has a weakmap cache
// We could store a cache on Resolver, but maybe we can wait and profile
matchableStringSet := collections.OrderedSet[string]{}
patterns := make([]core.Pattern, 0, paths.Size())
for path := range paths.Keys() {
func (r *Resolver) getParsedPatternsForPaths() *parsedPatterns {
r.parsedPatternsForPathsOnce.Do(func() {
r.parsedPatternsForPaths = tryParsePatterns(r.compilerOptions.Paths)
})
return r.parsedPatternsForPaths
}

func tryParsePatterns(pathMappings *collections.OrderedMap[string, []string]) *parsedPatterns {
paths := pathMappings.Keys()

numPatterns := 0
for path := range paths {
if pattern := core.TryParsePattern(path); pattern.IsValid() && pattern.StarIndex == -1 {
numPatterns++
}
}
numMatchables := pathMappings.Size() - numPatterns

var patterns []core.Pattern
var matchableStringSet core.Set[string]
if numPatterns != 0 {
patterns = make([]core.Pattern, 0, numPatterns)
}
if numMatchables != 0 {
matchableStringSet = *core.NewSetWithSizeHint[string](numMatchables)
}

for path := range paths {
if pattern := core.TryParsePattern(path); pattern.IsValid() {
if pattern.StarIndex == -1 {
matchableStringSet.Add(path)
Expand All @@ -1715,13 +1737,13 @@ func tryParsePatterns(paths *collections.OrderedMap[string, []string]) parsedPat
}
}
}
return parsedPatterns{
return &parsedPatterns{
matchableStringSet: matchableStringSet,
patterns: patterns,
}
}

func matchPatternOrExact(patterns parsedPatterns, candidate string) core.Pattern {
func matchPatternOrExact(patterns *parsedPatterns, candidate string) core.Pattern {
if patterns.matchableStringSet.Has(candidate) {
return core.Pattern{
Text: candidate,
Expand Down
7 changes: 7 additions & 0 deletions internal/core/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ type Set[T comparable] struct {
M map[T]struct{}
}

// NewSetWithSizeHint creates a new Set with a hint for the number of elements it will contain.
func NewSetWithSizeHint[T comparable](hint int) *Set[T] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably just do Set[T] here, since it's always dereferenced at its use.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But everyone else is doing *core.Set (everyone else is one function)

return &Set[T]{
M: make(map[T]struct{}, hint),
}
}

func (s *Set[T]) Has(key T) bool {
_, ok := s.M[key]
return ok
Expand Down