From 59d9bd85273dc10d761ef68a98e76bef92046c24 Mon Sep 17 00:00:00 2001 From: Kevin Petit Date: Wed, 1 Apr 2026 00:04:14 +0200 Subject: [PATCH 1/2] Fuzzy Search optimization --- LunrCore/TokenSet.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/LunrCore/TokenSet.cs b/LunrCore/TokenSet.cs index 0a948bd..fca4b38 100644 --- a/LunrCore/TokenSet.cs +++ b/LunrCore/TokenSet.cs @@ -101,16 +101,25 @@ public static TokenSet FromFuzzyString( idProvider ??= TokenSetIdProvider.Instance; var root = new TokenSet(idProvider); + var seen = new HashSet<(int nodeId, string frameStr, int editsRemaining)>(); + var stack = new Stack<(TokenSet node, int editsRemaining, string str)>(); stack.Push(( node: root, editsRemaining: editDistance, str)); - while (stack.Any()) + while (stack.Count > 0) { (TokenSet frameNode, int frameEditsRemaining, string frameStr) = stack.Pop(); + // skip if already processed + var stateKey = (frameNode.Id, frameStr, frameEditsRemaining); + if (!seen.Add(stateKey)) + { + continue; + } + // no edit if (frameStr.Length > 0) { From 39145f511da64a504b2bdc455d896be0f00d7883 Mon Sep 17 00:00:00 2001 From: Kevin Petit Date: Wed, 1 Apr 2026 00:56:22 +0200 Subject: [PATCH 2/2] Revert Any => Count --- LunrCore/TokenSet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LunrCore/TokenSet.cs b/LunrCore/TokenSet.cs index fca4b38..c54f561 100644 --- a/LunrCore/TokenSet.cs +++ b/LunrCore/TokenSet.cs @@ -109,7 +109,7 @@ public static TokenSet FromFuzzyString( editsRemaining: editDistance, str)); - while (stack.Count > 0) + while (stack.Any()) { (TokenSet frameNode, int frameEditsRemaining, string frameStr) = stack.Pop();