Skip to content

Commit a52a6af

Browse files
committed
feat: optimize and stabilize string normalization in StringMatcher
1 parent b6028dc commit a52a6af

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

Flow.Launcher.Infrastructure/StringMatcher.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using CommunityToolkit.Mvvm.DependencyInjection;
22
using Flow.Launcher.Plugin.SharedModels;
33
using System;
4+
using System.Buffers;
45
using System.Collections.Generic;
6+
using System.ComponentModel.DataAnnotations;
57
using System.Globalization;
68
using System.Linq;
79
using System.Text;
810
using Flow.Launcher.Infrastructure.UserSettings;
11+
using JetBrains.Annotations;
912

1013
namespace Flow.Launcher.Infrastructure
1114
{
@@ -256,14 +259,23 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
256259
};
257260
public static string Normalize(string value)
258261
{
259-
Span<char> buffer = stackalloc char[value.Length];
260-
for (int i = 0; i < value.Length; i++)
262+
if(string.IsNullOrEmpty(value)) return value;
263+
char[] arrayFromPool = null;
264+
Span<char> buffer = value.Length <= 512 ? stackalloc char[value.Length] : (arrayFromPool = ArrayPool<char>.Shared.Rent(value.Length));
265+
try
261266
{
262-
var c = char.ToLowerInvariant(value[i]);
263-
buffer[i] = AccentMap.TryGetValue(c, out var mapped) ? mapped : c;
267+
for (int i = 0; i < value.Length; i++)
268+
{
269+
var c = char.ToLowerInvariant(value[i]);
270+
buffer[i] = AccentMap.TryGetValue(c, out var mapped) ? mapped : c;
271+
}
272+
return new string(buffer.Slice(0, value.Length));
273+
}
274+
finally
275+
{
276+
if(arrayFromPool != null)
277+
ArrayPool<char>.Shared.Return(arrayFromPool);
264278
}
265-
266-
return new string(buffer);
267279
}
268280
private static bool IsAcronym(string stringToCompare, int compareStringIndex)
269281
{

0 commit comments

Comments
 (0)