Skip to content

Commit d85436e

Browse files
authored
Pool collators in LS (#1582)
1 parent 99fc3d3 commit d85436e

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

internal/ls/completions.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/microsoft/typescript-go/internal/scanner"
2929
"github.com/microsoft/typescript-go/internal/stringutil"
3030
"golang.org/x/text/collate"
31+
"golang.org/x/text/language"
3132
)
3233

3334
func (l *LanguageService) ProvideCompletion(
@@ -3094,6 +3095,25 @@ func getCompletionsSymbolKind(kind ScriptElementKind) lsproto.CompletionItemKind
30943095
}
30953096
}
30963097

3098+
var collatorCache collections.SyncMap[language.Tag, *sync.Pool]
3099+
3100+
func getCollator(tag language.Tag) *collate.Collator {
3101+
pool, ok := collatorCache.Load(tag)
3102+
if !ok {
3103+
pool, _ = collatorCache.LoadOrStore(tag, &sync.Pool{
3104+
New: func() any {
3105+
return collate.New(tag)
3106+
},
3107+
})
3108+
}
3109+
return pool.Get().(*collate.Collator)
3110+
}
3111+
3112+
func putCollator(tag language.Tag, collator *collate.Collator) {
3113+
pool, _ := collatorCache.Load(tag)
3114+
pool.Put(collator)
3115+
}
3116+
30973117
// Editors will use the `sortText` and then fall back to `name` for sorting, but leave ties in response order.
30983118
// So, it's important that we sort those ties in the order we want them displayed if it matters. We don't
30993119
// strictly need to sort by name or SortText here since clients are going to do it anyway, but we have to
@@ -3103,7 +3123,10 @@ func getCompletionsSymbolKind(kind ScriptElementKind) lsproto.CompletionItemKind
31033123
// this made tests really weird, since most fourslash tests don't use the server.
31043124
func getCompareCompletionEntries(ctx context.Context) func(entryInSlice *lsproto.CompletionItem, entryToInsert *lsproto.CompletionItem) int {
31053125
return func(entryInSlice *lsproto.CompletionItem, entryToInsert *lsproto.CompletionItem) int {
3106-
compareStrings := collate.New(core.GetLocale(ctx)).CompareString
3126+
locale := core.GetLocale(ctx)
3127+
collator := getCollator(locale)
3128+
defer putCollator(locale, collator)
3129+
compareStrings := collator.CompareString
31073130
result := compareStrings(*entryInSlice.SortText, *entryToInsert.SortText)
31083131
if result == stringutil.ComparisonEqual {
31093132
result = compareStrings(entryInSlice.Label, entryToInsert.Label)

0 commit comments

Comments
 (0)