Skip to content

Pool collators in LS #1582

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 24 additions & 1 deletion internal/ls/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/microsoft/typescript-go/internal/scanner"
"github.com/microsoft/typescript-go/internal/stringutil"
"golang.org/x/text/collate"
"golang.org/x/text/language"
)

func (l *LanguageService) ProvideCompletion(
Expand Down Expand Up @@ -2980,6 +2981,25 @@ func getCompletionsSymbolKind(kind ScriptElementKind) lsproto.CompletionItemKind
}
}

var collatorCache collections.SyncMap[language.Tag, *sync.Pool]

func getCollator(tag language.Tag) *collate.Collator {
pool, ok := collatorCache.Load(tag)
if !ok {
pool, _ = collatorCache.LoadOrStore(tag, &sync.Pool{
New: func() any {
return collate.New(tag)
},
})
}
return pool.Get().(*collate.Collator)
}

func putCollator(tag language.Tag, collator *collate.Collator) {
pool, _ := collatorCache.Load(tag)
Copy link
Preview

Copilot AI Aug 14, 2025

Choose a reason for hiding this comment

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

The putCollator function assumes the pool exists in the cache, but there's no guarantee. If the pool was somehow evicted or never created, this will panic. Add a nil check or use LoadOrStore pattern similar to getCollator.

Suggested change
pool, _ := collatorCache.Load(tag)
pool, _ := collatorCache.LoadOrStore(tag, &sync.Pool{
New: func() any {
return collate.New(tag)
},
})

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested code is wrong because it'll just waste loads and loads of allocs. If I wanted to solve this, I'd just return a done func from get.

pool.Put(collator)
}

// Editors will use the `sortText` and then fall back to `name` for sorting, but leave ties in response order.
// So, it's important that we sort those ties in the order we want them displayed if it matters. We don't
// strictly need to sort by name or SortText here since clients are going to do it anyway, but we have to
Expand All @@ -2989,7 +3009,10 @@ func getCompletionsSymbolKind(kind ScriptElementKind) lsproto.CompletionItemKind
// this made tests really weird, since most fourslash tests don't use the server.
func getCompareCompletionEntries(ctx context.Context) func(entryInSlice *lsproto.CompletionItem, entryToInsert *lsproto.CompletionItem) int {
return func(entryInSlice *lsproto.CompletionItem, entryToInsert *lsproto.CompletionItem) int {
compareStrings := collate.New(core.GetLocale(ctx)).CompareString
locale := core.GetLocale(ctx)
collator := getCollator(locale)
defer putCollator(locale, collator)
compareStrings := collator.CompareString
result := compareStrings(*entryInSlice.SortText, *entryToInsert.SortText)
if result == stringutil.ComparisonEqual {
result = compareStrings(entryInSlice.Label, entryToInsert.Label)
Expand Down
Loading