From cc5747af04d4f1957c4cb5e63bfbff2574408373 Mon Sep 17 00:00:00 2001 From: Yevhen Cherkes Date: Sat, 14 Nov 2026 15:44:47 +0100 Subject: [PATCH 1/6] Improve fuzzy scoring and parallel extraction performance --- .../WeightedRatioHighSimilarityBenchmarks.cs | 27 ++++++ .../FuzzyTests/CachedScorerTests.cs | 16 ++++ .../FuzzyTests/ExtractorSelectionTests.cs | 37 +++++++++ .../ResultExtractor.Parallel.Cached.cs | 18 ++-- .../Extractor/ResultExtractor.Parallel.cs | 83 +++++++++++-------- .../Composite/CachedWeightedRatioScorer.cs | 4 + .../Scorer/Composite/WeightedRatioScorer.cs | 4 + .../TokenSet/CachedTokenSetScorerBase.cs | 18 ++-- .../TokenSet/TokenSetScorerBase.cs | 12 +++ README.md | 2 + docs/performance-testing-2026-11-14.md | 64 ++++++++++++++ 11 files changed, 233 insertions(+), 52 deletions(-) create mode 100644 FuzzySharp.Benchmarks/WeightedRatioHighSimilarityBenchmarks.cs create mode 100644 docs/performance-testing-2026-11-14.md diff --git a/FuzzySharp.Benchmarks/WeightedRatioHighSimilarityBenchmarks.cs b/FuzzySharp.Benchmarks/WeightedRatioHighSimilarityBenchmarks.cs new file mode 100644 index 0000000..2d56301 --- /dev/null +++ b/FuzzySharp.Benchmarks/WeightedRatioHighSimilarityBenchmarks.cs @@ -0,0 +1,27 @@ +using BenchmarkDotNet.Attributes; + +namespace Raffinert.FuzzySharp.Benchmarks; + +[MemoryDiagnoser] +public class WeightedRatioHighSimilarityBenchmarks +{ + private const string Query = "an almost exact weighted ratio match"; + + [Params("an almost exact weighted ratio match", "an almost exact weighted ratio matci")] + public string Candidate { get; set; } = ""; + + [Benchmark] + public double EarlyExit() + { + return Fuzz.WeightedRatio(Query, Candidate); + } + + [Benchmark(Baseline = true)] + public double FullEvaluation() + { + var baseRatio = Fuzz.Ratio(Query, Candidate); + var tokenSort = Fuzz.TokenSortRatio(Query, Candidate) * .95; + var tokenSet = Fuzz.TokenSetRatio(Query, Candidate) * .95; + return Math.Max(baseRatio, Math.Max(tokenSort, tokenSet)); + } +} diff --git a/FuzzySharp.Test/FuzzyTests/CachedScorerTests.cs b/FuzzySharp.Test/FuzzyTests/CachedScorerTests.cs index bd6a39e..f5b11ab 100644 --- a/FuzzySharp.Test/FuzzyTests/CachedScorerTests.cs +++ b/FuzzySharp.Test/FuzzyTests/CachedScorerTests.cs @@ -16,6 +16,8 @@ public class CachedScorerTests { "Acme 123", "123 Acme Ltd" }, { "red red blue", "blue red red" }, { "Acme, 123", "123 Acme" }, + { "an exact weighted ratio match", "an exact weighted ratio match" }, + { "an almost exact weighted ratio match", "an almost exact weighted ratio matci" }, }; [Theory] @@ -59,4 +61,18 @@ public void CachedPartialTokenSet_EmptyTokenCollectionScoresZero() using var nonEmptyQuery = new CachedPartialTokenSetScorer("x"); Assert.Equal(0, nonEmptyQuery.Score("")); } + + [Fact] + public void CachedTokenSetMatchesStatelessAcrossRepeatedCalls() + { + const string query = "red red blue\tgreen"; + string[] candidates = ["blue red", "red blue green", "green sky", " ", "blue\nred red"]; + using var cached = new CachedTokenSetScorer(query); + + for (var repeat = 0; repeat < 2; repeat++) + { + foreach (var candidate in candidates) + Assert.Equal(Fuzz.TokenSetRatio(query, candidate), cached.Score(candidate), precision: 10); + } + } } diff --git a/FuzzySharp.Test/FuzzyTests/ExtractorSelectionTests.cs b/FuzzySharp.Test/FuzzyTests/ExtractorSelectionTests.cs index 41fdbe8..8188af9 100644 --- a/FuzzySharp.Test/FuzzyTests/ExtractorSelectionTests.cs +++ b/FuzzySharp.Test/FuzzyTests/ExtractorSelectionTests.cs @@ -177,6 +177,43 @@ public void ExtractTop_GenericCachedParallelVariantsMatchLegacyTopN() AssertSameResults(expected, ResultExtractor.Parallel.Cached.ExtractTop(choices, static choice => choice.Name, IdentityProcessor, cachedScorer, limit: 3, cutoff: 70, parallelOptions: TestParallelOptions)); } + [Fact] + public void ExtractTop_ParallelMatchesSequentialForManyTiesAndLimits() + { + for (var seed = 0; seed < 20; seed++) + { + var random = new Random(seed); + var scores = Enumerable.Range(0, 40) + .Select(index => ($"choice-{index}", (double)random.Next(6))) + .ToArray(); + var choices = scores.Select(item => item.Item1).ToArray(); + var scorer = new MapScorer(scores); + var cachedScorer = new CachedMapScorer(scores); + + foreach (var limit in new[] { 1, 2, 5, 10, 40 }) + { + var expected = ResultExtractor.ExtractTop("query", choices, IdentityProcessor, scorer, limit).ToList(); + AssertSameResults(expected, ResultExtractor.Parallel.ExtractTop("query", choices, IdentityProcessor, scorer, limit, parallelOptions: TestParallelOptions)); + AssertSameResults(expected, ResultExtractor.Parallel.Cached.ExtractTop(choices, IdentityProcessor, cachedScorer, limit, parallelOptions: TestParallelOptions)); + } + } + } + + [Fact] + public void ParallelExtraction_MaterializesOneShotChoicesOnce() + { + var scorer = new MapScorer(("a", 10), ("winner", 90), ("b", 20)); + var one = ResultExtractor.Parallel.ExtractOne( + "query", new OneShotEnumerable(["a", "winner", "b"]), + IdentityProcessor, scorer, parallelOptions: TestParallelOptions); + AssertResult(one, "winner", 90, 1); + + var top = ResultExtractor.Parallel.ExtractTop( + "query", new OneShotEnumerable(["a", "winner", "b"]), + IdentityProcessor, scorer, limit: 2, parallelOptions: TestParallelOptions).ToList(); + Assert.Equal(new[] { "winner", "b" }, top.Select(result => result.Value)); + } + [Fact] public void ExtractTop_LimitZeroReturnsEmptyWithoutEnumeratingAndNegativeIsRejected() { diff --git a/FuzzySharp/Extractor/ResultExtractor.Parallel.Cached.cs b/FuzzySharp/Extractor/ResultExtractor.Parallel.Cached.cs index 6ea91df..8bc817e 100644 --- a/FuzzySharp/Extractor/ResultExtractor.Parallel.Cached.cs +++ b/FuzzySharp/Extractor/ResultExtractor.Parallel.Cached.cs @@ -14,7 +14,7 @@ public static class Cached { public static IEnumerable> ExtractWithoutOrder(IEnumerable choices, Func extractor, Func processor, ICachedRatioScorer scorer, double cutoff = 0, ParallelOptions parallelOptions = null) { - var materializedChoices = choices.ToList(); + var materializedChoices = GetIndexedChoices(choices); var result = new ExtractedResult[materializedChoices.Count]; System.Threading.Tasks.Parallel.ForEach(materializedChoices, parallelOptions ?? DefaultParallelOptions, (choice, _, index) => @@ -30,7 +30,7 @@ public static IEnumerable> ExtractWithoutOrder(IEnumerable public static IEnumerable> ExtractWithoutOrder(IEnumerable choices, Func processor, ICachedRatioScorer scorer, double cutoff = 0, ParallelOptions parallelOptions = null) { - var materializedChoices = choices.ToList(); + var materializedChoices = GetIndexedChoices(choices); var result = new ExtractedResult[materializedChoices.Count]; System.Threading.Tasks.Parallel.ForEach(materializedChoices, parallelOptions ?? DefaultParallelOptions, (choice, _, index) => @@ -46,13 +46,13 @@ public static IEnumerable> ExtractWithoutOrder(IEnumerab public static ExtractedResult ExtractOne(IEnumerable choices, Func extractor, Func processor, ICachedRatioScorer calculator, double cutoff = 0, ParallelOptions parallelOptions = null) { - var materializedChoices = choices.ToList(); + var materializedChoices = GetIndexedChoices(choices); return ExtractOneParallelCore(materializedChoices, choice => calculator.Score(processor(extractor(choice))), cutoff, parallelOptions); } public static ExtractedResult ExtractOne(IEnumerable choices, Func processor, ICachedRatioScorer calculator, double cutoff = 0, ParallelOptions parallelOptions = null) { - var materializedChoices = choices.ToList(); + var materializedChoices = GetIndexedChoices(choices); return ExtractOneParallelCore(materializedChoices, choice => calculator.Score(processor(choice)), cutoff, parallelOptions); } @@ -72,9 +72,8 @@ public static IEnumerable> ExtractTop(IEnumerable choic if (limit == 0) return Enumerable.Empty>(); - var materializedChoices = choices.ToList(); - var scores = ScoreParallel(materializedChoices, choice => calculator.Score(processor(extractor(choice))), parallelOptions); - return ExtractTopParallelCore(materializedChoices, scores, limit, cutoff); + var materializedChoices = GetIndexedChoices(choices); + return ExtractTopParallelCore(materializedChoices, choice => calculator.Score(processor(extractor(choice))), limit, cutoff, parallelOptions); } public static IEnumerable> ExtractTop(IEnumerable choices, Func processor, ICachedRatioScorer calculator, int limit, double cutoff = 0, ParallelOptions parallelOptions = null) @@ -83,9 +82,8 @@ public static IEnumerable> ExtractTop(IEnumerable>(); - var materializedChoices = choices.ToList(); - var scores = ScoreParallel(materializedChoices, choice => calculator.Score(processor(choice)), parallelOptions); - return ExtractTopParallelCore(materializedChoices, scores, limit, cutoff); + var materializedChoices = GetIndexedChoices(choices); + return ExtractTopParallelCore(materializedChoices, choice => calculator.Score(processor(choice)), limit, cutoff, parallelOptions); } } } diff --git a/FuzzySharp/Extractor/ResultExtractor.Parallel.cs b/FuzzySharp/Extractor/ResultExtractor.Parallel.cs index 092cdb9..3cd2dce 100644 --- a/FuzzySharp/Extractor/ResultExtractor.Parallel.cs +++ b/FuzzySharp/Extractor/ResultExtractor.Parallel.cs @@ -1,6 +1,7 @@ using Raffinert.FuzzySharp.SimilarityRatio.Scorer; using Raffinert.FuzzySharp.Utils; using System; +using System.Buffers; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -13,6 +14,9 @@ public static partial class ResultExtractor public static partial class Parallel { + private static IList GetIndexedChoices(IEnumerable choices) => + choices as IList ?? choices.ToList(); + private static ExtractedResult ExtractOneParallelCore( IList choices, Func scoreSelector, @@ -51,50 +55,60 @@ private static ExtractedResult ExtractOneParallelCore( private static IEnumerable> ExtractTopParallelCore( IList choices, - double[] scores, + Func scoreSelector, int limit, - double cutoff) + double cutoff, + ParallelOptions parallelOptions) { ValidateLimit(limit); if (limit == 0) - yield break; + return Enumerable.Empty>(); - var heap = new MinHeap>(ScoredCandidateComparer.Instance); - var comparer = ScoredCandidateComparer.Instance; + if (choices.Count == 0) + return Enumerable.Empty>(); - for (var index = 0; index < scores.Length; index++) + var scores = ArrayPool.Shared.Rent(choices.Count); + var comparer = ScoredCandidateComparer.Instance; + try { - var score = scores[index]; - if (score >= cutoff) + System.Threading.Tasks.Parallel.For(0, choices.Count, parallelOptions ?? DefaultParallelOptions, index => + { + scores[index] = scoreSelector(choices[index]); + }); + + var heap = new MinHeap>(comparer); + for (var index = 0; index < choices.Count; index++) { - AddTopCandidate(heap, comparer, new ScoredCandidate(choices[index], score, index), limit); + var score = scores[index]; + if (score >= cutoff) + AddTopCandidate(heap, comparer, new ScoredCandidate(index, score, index), limit); } - } - foreach (var result in CreateTopResults(heap)) + var selected = new ScoredCandidate[heap.Count]; + for (var i = selected.Length - 1; i >= 0; i--) + selected[i] = heap.ExtractDominating(); + + return CreateParallelTopResults(choices, selected); + } + finally { - yield return result; + ArrayPool.Shared.Return(scores); } } - private static double[] ScoreParallel( + private static IEnumerable> CreateParallelTopResults( IList choices, - Func scoreSelector, - ParallelOptions parallelOptions) + ScoredCandidate[] selected) { - var scores = new double[choices.Count]; - - System.Threading.Tasks.Parallel.For(0, choices.Count, parallelOptions ?? DefaultParallelOptions, index => + foreach (var candidate in selected) { - scores[index] = scoreSelector(choices[index]); - }); - - return scores; + yield return new ExtractedResult(choices[candidate.Index], candidate.Score, candidate.Index); + } } public static IEnumerable> ExtractWithoutOrder(string query, IEnumerable choices, Func extractor, Func processor, IRatioScorer scorer, double cutoff = 0, ParallelOptions parallelOptions = null) { - var materializedChoices = choices.ToList(); + var materializedChoices = GetIndexedChoices(choices); var result = new ExtractedResult[materializedChoices.Count]; var processedQuery = processor(query); @@ -111,7 +125,7 @@ public static IEnumerable> ExtractWithoutOrder(string quer public static IEnumerable> ExtractWithoutOrder(string query, IEnumerable choices, Func processor, IRatioScorer scorer, double cutoff = 0, ParallelOptions parallelOptions = null) { - var materializedChoices = choices.ToList(); + var materializedChoices = GetIndexedChoices(choices); var result = new ExtractedResult[materializedChoices.Count]; var processedQuery = processor(query); @@ -133,21 +147,21 @@ public static IEnumerable> ExtractWithoutOrder(T query, IE public static ExtractedResult ExtractOne(T query, IEnumerable choices, Func extractor, Func processor, IRatioScorer calculator, double cutoff = 0, ParallelOptions parallelOptions = null) { - var materializedChoices = choices.ToList(); + var materializedChoices = GetIndexedChoices(choices); var processedQuery = processor(extractor(query)); return ExtractOneParallelCore(materializedChoices, choice => calculator.Score(processedQuery, processor(extractor(choice))), cutoff, parallelOptions); } public static ExtractedResult ExtractOne(string query, IEnumerable choices, Func processor, IRatioScorer calculator, double cutoff = 0, ParallelOptions parallelOptions = null) { - var materializedChoices = choices.ToList(); + var materializedChoices = GetIndexedChoices(choices); var processedQuery = processor(query); return ExtractOneParallelCore(materializedChoices, choice => calculator.Score(processedQuery, processor(choice)), cutoff, parallelOptions); } public static ExtractedResult ExtractOne(string query, IEnumerable choices, Func extractor, Func processor, IRatioScorer calculator, double cutoff = 0, ParallelOptions parallelOptions = null) { - var materializedChoices = choices.ToList(); + var materializedChoices = GetIndexedChoices(choices); var processedQuery = processor(query); return ExtractOneParallelCore(materializedChoices, choice => calculator.Score(processedQuery, processor(extractor(choice))), cutoff, parallelOptions); } @@ -173,10 +187,9 @@ public static IEnumerable> ExtractTop(T query, IEnumerable if (limit == 0) return Enumerable.Empty>(); - var materializedChoices = choices.ToList(); + var materializedChoices = GetIndexedChoices(choices); var processedQuery = processor(extractor(query)); - var scores = ScoreParallel(materializedChoices, choice => calculator.Score(processedQuery, processor(extractor(choice))), parallelOptions); - return ExtractTopParallelCore(materializedChoices, scores, limit, cutoff); + return ExtractTopParallelCore(materializedChoices, choice => calculator.Score(processedQuery, processor(extractor(choice))), limit, cutoff, parallelOptions); } public static IEnumerable> ExtractTop(string query, IEnumerable choices, Func processor, IRatioScorer calculator, int limit, double cutoff = 0, ParallelOptions parallelOptions = null) @@ -185,10 +198,9 @@ public static IEnumerable> ExtractTop(string query, IEnu if (limit == 0) return Enumerable.Empty>(); - var materializedChoices = choices.ToList(); + var materializedChoices = GetIndexedChoices(choices); var processedQuery = processor(query); - var scores = ScoreParallel(materializedChoices, choice => calculator.Score(processedQuery, processor(choice)), parallelOptions); - return ExtractTopParallelCore(materializedChoices, scores, limit, cutoff); + return ExtractTopParallelCore(materializedChoices, choice => calculator.Score(processedQuery, processor(choice)), limit, cutoff, parallelOptions); } public static IEnumerable> ExtractTop(string query, IEnumerable choices, Func extractor, Func processor, IRatioScorer calculator, int limit, double cutoff = 0, ParallelOptions parallelOptions = null) @@ -197,10 +209,9 @@ public static IEnumerable> ExtractTop(string query, IEnume if (limit == 0) return Enumerable.Empty>(); - var materializedChoices = choices.ToList(); + var materializedChoices = GetIndexedChoices(choices); var processedQuery = processor(query); - var scores = ScoreParallel(materializedChoices, choice => calculator.Score(processedQuery, processor(extractor(choice))), parallelOptions); - return ExtractTopParallelCore(materializedChoices, scores, limit, cutoff); + return ExtractTopParallelCore(materializedChoices, choice => calculator.Score(processedQuery, processor(extractor(choice))), limit, cutoff, parallelOptions); } } } diff --git a/FuzzySharp/SimilarityRatio/Scorer/Composite/CachedWeightedRatioScorer.cs b/FuzzySharp/SimilarityRatio/Scorer/Composite/CachedWeightedRatioScorer.cs index 4b10f6b..23115c4 100644 --- a/FuzzySharp/SimilarityRatio/Scorer/Composite/CachedWeightedRatioScorer.cs +++ b/FuzzySharp/SimilarityRatio/Scorer/Composite/CachedWeightedRatioScorer.cs @@ -41,6 +41,10 @@ public double Score(string input2) double partialScale = PARTIAL_SCALE; double baseRatio = _baseRatioScorer.Score(input2); + // Every other branch is scaled to at most 95. + if (baseRatio >= unbaseScale * 100) + return baseRatio; + double lenRatio = (double)Math.Max(len1, len2) / Math.Min(len1, len2); // if strings are similar length don't use partials diff --git a/FuzzySharp/SimilarityRatio/Scorer/Composite/WeightedRatioScorer.cs b/FuzzySharp/SimilarityRatio/Scorer/Composite/WeightedRatioScorer.cs index 6e9b8e8..c941764 100644 --- a/FuzzySharp/SimilarityRatio/Scorer/Composite/WeightedRatioScorer.cs +++ b/FuzzySharp/SimilarityRatio/Scorer/Composite/WeightedRatioScorer.cs @@ -23,6 +23,10 @@ public override double Score(string input1, string input2) double partialScale = PARTIAL_SCALE; double baseRatio = Fuzz.Ratio(input1, input2); + // Every other branch is scaled to at most 95. + if (baseRatio >= unbaseScale * 100) + return baseRatio; + double lenRatio = (double)Math.Max(len1, len2) / Math.Min(len1, len2); // if strings are similar length don't use partials diff --git a/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/TokenSet/CachedTokenSetScorerBase.cs b/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/TokenSet/CachedTokenSetScorerBase.cs index ec6feb0..182fc3d 100644 --- a/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/TokenSet/CachedTokenSetScorerBase.cs +++ b/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/TokenSet/CachedTokenSetScorerBase.cs @@ -6,26 +6,32 @@ namespace Raffinert.FuzzySharp.SimilarityRatio.Scorer.StrategySensitive; public abstract class CachedTokenSetScorerBase(string input1) : ICachedRatioScorer { - private HashSet Tokens1 { get; set; } = new HashSet(input1.SplitByAnySpace()); + private readonly HashSet _tokens1 = new HashSet(input1.SplitByAnySpace()); protected abstract FuzzySharp.Scorer Scorer { get; } public double Score(string input2) { var tokens2 = new HashSet(input2.SplitByAnySpace()); - if (Tokens1.Count == 0 || tokens2.Count == 0) + if (_tokens1.Count == 0 || tokens2.Count == 0) { return 0; } - var tokens1 = new HashSet(Tokens1); - - var intersection = TokenSetScorerHelpers.GetIntersectionAndExcept(tokens1, tokens2); + var intersection = new List(Math.Min(_tokens1.Count, tokens2.Count)); + var difference1 = new List(); + foreach (var token in _tokens1) + { + if (tokens2.Remove(token)) + intersection.Add(token); + else + difference1.Add(token); + } intersection.Sort(); var sortedIntersection = string.Join(" ", intersection); - var sortedDiff1To2 = TokenSetScorerHelpers.JoinIntersectionAndDifference(sortedIntersection, tokens1); + var sortedDiff1To2 = TokenSetScorerHelpers.JoinIntersectionAndDifference(sortedIntersection, difference1); var sortedDiff2To1 = TokenSetScorerHelpers.JoinIntersectionAndDifference(sortedIntersection, tokens2); var score1 = Scorer(sortedIntersection, sortedDiff1To2); diff --git a/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/TokenSet/TokenSetScorerBase.cs b/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/TokenSet/TokenSetScorerBase.cs index eb410db..18cc872 100644 --- a/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/TokenSet/TokenSetScorerBase.cs +++ b/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/TokenSet/TokenSetScorerBase.cs @@ -66,6 +66,18 @@ internal static string JoinIntersectionAndDifference(string sortedIntersection, : string.Concat(sortedIntersection, " ", sortedDifference); } + internal static string JoinIntersectionAndDifference(string sortedIntersection, List difference) + { + if (difference.Count == 0) + return sortedIntersection; + + difference.Sort(); + var sortedDifference = string.Join(" ", difference); + return sortedIntersection.Length == 0 + ? sortedDifference + : string.Concat(sortedIntersection, " ", sortedDifference); + } + private static string JoinSorted(IEnumerable tokens) { var sortedTokens = new List(tokens); diff --git a/README.md b/README.md index fba7ee6..f6f9cb8 100644 --- a/README.md +++ b/README.md @@ -325,6 +325,8 @@ var pipeline = Process.Configure() var result = pipeline.ExtractOne("cowboys", new[] { "Atlanta Falcons", "New York Jets", "New York Giants", "Dallas Cowboys" }); ``` +Caching pays off when the query is compared with many candidates or reused across calls. For a single score, call `Fuzz.Ratio` or `Fuzz.WeightedRatio` directly; constructing a cached scorer for one comparison adds work and allocations. + ### Cached + Parallel Combine caching and parallelism. Builder methods are **order independent** -- `.Cached().Parallel()` and `.Parallel().Cached()` produce identical results: diff --git a/docs/performance-testing-2026-11-14.md b/docs/performance-testing-2026-11-14.md new file mode 100644 index 0000000..c57fcdb --- /dev/null +++ b/docs/performance-testing-2026-11-14.md @@ -0,0 +1,64 @@ +# Performance testing and tuning plan — 2026-11-14 + +## Test setup + +- Windows 11, .NET SDK 10.0.403, .NET runtime 10.0.14, x64 RyuJIT. +- BenchmarkDotNet 0.15.8, Release build, in-process toolchain. The configured out-of-process `ShortRun` harness could not build in this restricted environment, so its `NA` rows were excluded. +- The scorer run used BenchmarkDotNet's default iteration counts. The extraction confirmation used 3 warmups, 10 measured iterations, and a 100 ms target per iteration. +- These are single-host, fixed-input microbenchmarks. Differences of a few percent and behavior on other runtimes or input distributions need separate confirmation. + +Reproduction: build `FuzzySharp.Benchmarks` in Release, then run its executable with `--inProcess --filter '*RatioBenchmarks.Ratio*' '*WeightedRatioBenchmarks.WeightedRatio*'`. The token-set run used `--filter '*TokenSetRatioBenchmarks.TokenSetRatio*' --iterationCount 3 --warmupCount 1 --iterationTime 100`. The extraction confirmation used `--filter '*ExtractSelectionFocusedBenchmarks.Optimized_ExtractOne' '*ExtractSelectionFocusedBenchmarks.Optimized_Parallel_ExtractOne' --iterationCount 10 --warmupCount 3 --iterationTime 100`. The repository's `Program.cs` also adds an out-of-process `ShortRun` job, whose failed rows should be ignored in this environment. + +## Measured results + +| Workload | Current library | Comparison | Allocation, current / comparison | +| --- | ---: | ---: | ---: | +| `Ratio` | 51.0 ns | classic FuzzySharp: 109.5 ns | 80 B / 320 B | +| `Ratio`, scorer reused across calls | 24.3 ns | stateless call: 51.0 ns | 0 B / 80 B | +| `Ratio`, new cached scorer per call | 69.8 ns | stateless call: 51.0 ns | 168 B / 80 B | +| `WeightedRatio` | 1.951 µs | classic FuzzySharp: 5.627 µs | 3.69 KB / 11.22 KB | +| `WeightedRatio`, scorer reused across calls | 1.336 µs | stateless call: 1.951 µs | 2.58 KB / 3.69 KB | +| `WeightedRatio`, new cached scorer per call | 2.425 µs | stateless call: 1.951 µs | 12.32 KB / 3.69 KB | +| `TokenSetRatio` | 329.8 ns | classic FuzzySharp: 1,488.4 ns | 1.46 KB / 3.88 KB | +| `TokenSetRatio`, scorer reused across calls | 320.8 ns | stateless call: 329.8 ns | 1.23 KB / 1.46 KB | + +The token-set measurements used only three measured iterations and varied enough that the small time difference between cached and stateless scoring is inconclusive. Its allocation difference and the cost of constructing a cached scorer are clearer signals. + +`ExtractSelectionFocusedBenchmarks` scored 100,000 choices with the benchmark's cheap scorer and four workers: + +| Cutoff | Sequential `ExtractOne` | Parallel `ExtractOne` | Allocation, sequential / parallel | +| ---: | ---: | ---: | ---: | +| 0 | 715.3 µs | 429.0 µs | 80 B / 803,612 B | +| 95 | 743.4 µs | 393.3 µs | 111 B / 803,815 B | + +Parallel results here are about 1.7–1.9 times faster but copy the input array into a list. This case does not establish a parallel crossover for smaller choice counts or expensive scorers. + +## Prioritized changes to test + +1. **Avoid per-call choice copies in parallel extraction.** `ResultExtractor.Parallel.cs` and its cached variant call `choices.ToList()` even for arrays and existing lists. Use an `IList` or array fast path and materialize only other enumerables. The 100,000-choice benchmark allocates about 804 KB per parallel call, close to the size of the copied reference array. Benchmark arrays, lists, and lazy enumerables at 64, 1,000, and 100,000 choices; verify index and tie behavior. +2. **Short-circuit `WeightedRatio` when the base score already wins.** The unscaled score is calculated before the token and partial branches. For similar-length strings, the other branches cannot exceed 95; for the partial branch they cannot exceed 90. Return the base score at those bounds in both stateless and cached scorers. Add exact, near-exact, and low-similarity cases to quantify the affected input distribution. Keep the score identical and validate boundary cases. +3. **Reduce token-set allocation.** `TokenSetScorerBase` splits both inputs into strings, constructs two hash sets, sorts collections, and joins several temporary strings. The cached version also clones the first hash set per score. Prototype a read-only cached token representation with membership checks and a tokenization path that avoids unnecessary intermediate arrays. Measure allocation and runtime for repeated queries with 2, 8, and 32 tokens, including duplicates and Unicode whitespace. Preserve current sorting and duplicate semantics. +4. **Apply cached scorers only to repeated queries.** The existing benchmark shows that constructing cached `Ratio` and `WeightedRatio` scorers for a single call is slower and allocates more than stateless scoring. Document this crossover in the public guidance and include realistic query-reuse counts in benchmarks. +5. **Investigate parallel top-N memory separately.** Parallel `ExtractTop` materializes choices and creates a `double[choices.Count]` before the final heap selection. Try worker-local bounded heaps and a final merge to avoid the full score array. Benchmark both cheap and weighted scorers across list sizes and limits; preserve stable ties and cutoff behavior. This remains a code-based hypothesis until measured. + +Run correctness tests after each implementation, then compare old and new versions with the same BenchmarkDotNet job and host. Record mean, spread, and bytes allocated; promote a change only when it improves a representative workload without a material regression elsewhere. + +## Implementation follow-up + +The tuning work applied the indexed-collection fast path, the weighted-ratio bound, and the cached token-set change. Parallel top-N now rents its score buffer and returns it after preserving the original score-order selection. A worker-local heap prototype failed the expanded tie-order test and was replaced. + +| Workload | Before | After | Allocation before / after | +| --- | ---: | ---: | ---: | +| Parallel `ExtractOne`, 100,000 choices, cutoff 0 | 429.0 µs | 297.7 µs | 803,612 B / 2,544 B | +| Parallel `ExtractOne`, 100,000 choices, cutoff 95 | 393.3 µs | 254.0 µs | 803,815 B / 2,541 B | +| Reused cached `TokenSetRatio` | 320.8 ns | 202.6 ns | 1.23 KB / 976 B | +| Parallel top-N, 100,000 choices, cutoff 0 | 660.9 µs | 456.9 µs | 786.33 KB / 3.98 KB | +| Parallel top-N, 100,000 choices, cutoff 95 | 472.8 µs | 402.4 µs | 786.82 KB / 3.98 KB | + +These before/after runs were separate in-process runs on the same host. The high-similarity weighted-ratio benchmark includes a full-evaluation baseline within the same run: exact inputs took 22.75 ns with the early exit versus 877.61 ns with all branches evaluated (80 B versus 2,464 B). One-character-different inputs took 36.78 ns versus 890.63 ns (80 B versus 2,776 B). Inputs below the bound still evaluate the existing branches. + +On the existing lower-similarity weighted benchmark, stateless scoring measured 2.022 µs after the change versus 1.951 µs before; that difference is smaller than the run-to-run spread. Reused cached scoring measured 1.269 µs and 2.22 KB after versus 1.336 µs and 2.58 KB before. The cached allocation reduction is consistent with the token-set change. + +Validation: 5,145 tests passed on both .NET 8 and .NET 10, including additional repeated-cache, tie-order, and one-shot enumerable cases. The library built across all configured target frameworks. The existing netcoreapp3.1 dependency support warning remained. + +The pooled top-N path reduces garbage collection pressure but still uses a buffer proportional to the number of choices. More work on tokenization and score-buffer size should be driven by broader input distributions and runtime measurements. From ad6b322f1423fe0e3f23df34c1ac82373f305e1f Mon Sep 17 00:00:00 2001 From: Yevhen Cherkes Date: Sat, 14 Nov 2026 15:46:31 +0100 Subject: [PATCH 2/6] Bump package version to 6.0.2 --- CHANGELOG.md | 7 +++++++ FuzzySharp/FuzzySharp.csproj | 11 ++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a178b9c..bddbf8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## v6.0.2 + +- Reduced parallel extraction allocations by using indexed choices directly and pooling top-N score buffers. +- Added an early exit for high-similarity weighted scores when the base score cannot be beaten. +- Reduced cached token-set scoring allocations by reusing the query token set without cloning it per call. +- Added performance benchmarks and regression coverage for cached scoring, tie ordering, and one-shot choice sequences. + ## v6.0.1 - Fixed `PartialRatio` false exact matches for near-perfect long strings. diff --git a/FuzzySharp/FuzzySharp.csproj b/FuzzySharp/FuzzySharp.csproj index 9d01eaa..d06d0a0 100644 --- a/FuzzySharp/FuzzySharp.csproj +++ b/FuzzySharp/FuzzySharp.csproj @@ -1,13 +1,10 @@  - 6.0.1.0 - 6.0.1 - 6.0.1 - 6.0.1.0 - 6.0.1.0 - 6.0.1 - 6.0.1 + 6.0.2.0 + 6.0.2 + 6.0.2 + 6.0.2.0 Yevhen Cherkes;Jacob Bayer From 7a772d41d69117398ac1d753da88f6c81fd0d5f7 Mon Sep 17 00:00:00 2001 From: Yevhen Cherkes Date: Sat, 14 Nov 2026 15:52:55 +0100 Subject: [PATCH 3/6] Capture parallel top results before returning --- .../FuzzyTests/ExtractorSelectionTests.cs | 23 +++++++++++++++++++ .../Extractor/ResultExtractor.Parallel.cs | 15 ++++++------ docs/performance-testing-2026-11-14.md | 4 +++- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/FuzzySharp.Test/FuzzyTests/ExtractorSelectionTests.cs b/FuzzySharp.Test/FuzzyTests/ExtractorSelectionTests.cs index 8188af9..46b2d1f 100644 --- a/FuzzySharp.Test/FuzzyTests/ExtractorSelectionTests.cs +++ b/FuzzySharp.Test/FuzzyTests/ExtractorSelectionTests.cs @@ -199,6 +199,29 @@ public void ExtractTop_ParallelMatchesSequentialForManyTiesAndLimits() } } + [Theory] + [InlineData(false)] + [InlineData(true)] + public void ExtractTop_ParallelCapturesSelectedValuesBeforeEnumeration(bool cached) + { + var scorer = new MapScorer(("low", 10), ("winner", 90), ("other", 20)); + using var cachedScorer = new CachedMapScorer(("low", 10), ("winner", 90), ("other", 20)); + + var array = new[] { "low", "winner", "other" }; + var arrayResults = cached + ? ResultExtractor.Parallel.Cached.ExtractTop(array, IdentityProcessor, cachedScorer, limit: 1, parallelOptions: TestParallelOptions) + : ResultExtractor.Parallel.ExtractTop("query", array, IdentityProcessor, scorer, limit: 1, parallelOptions: TestParallelOptions); + array[1] = "replacement"; + AssertResult(Assert.Single(arrayResults), "winner", 90, 1); + + var list = new List { "low", "winner", "other" }; + var listResults = cached + ? ResultExtractor.Parallel.Cached.ExtractTop(list, IdentityProcessor, cachedScorer, limit: 1, parallelOptions: TestParallelOptions) + : ResultExtractor.Parallel.ExtractTop("query", list, IdentityProcessor, scorer, limit: 1, parallelOptions: TestParallelOptions); + list.Clear(); + AssertResult(Assert.Single(listResults), "winner", 90, 1); + } + [Fact] public void ParallelExtraction_MaterializesOneShotChoicesOnce() { diff --git a/FuzzySharp/Extractor/ResultExtractor.Parallel.cs b/FuzzySharp/Extractor/ResultExtractor.Parallel.cs index 3cd2dce..f6aaaf9 100644 --- a/FuzzySharp/Extractor/ResultExtractor.Parallel.cs +++ b/FuzzySharp/Extractor/ResultExtractor.Parallel.cs @@ -68,7 +68,7 @@ private static IEnumerable> ExtractTopParallelCore( return Enumerable.Empty>(); var scores = ArrayPool.Shared.Rent(choices.Count); - var comparer = ScoredCandidateComparer.Instance; + var comparer = ScoredCandidateComparer.Instance; try { System.Threading.Tasks.Parallel.For(0, choices.Count, parallelOptions ?? DefaultParallelOptions, index => @@ -76,19 +76,19 @@ private static IEnumerable> ExtractTopParallelCore( scores[index] = scoreSelector(choices[index]); }); - var heap = new MinHeap>(comparer); + var heap = new MinHeap>(comparer); for (var index = 0; index < choices.Count; index++) { var score = scores[index]; if (score >= cutoff) - AddTopCandidate(heap, comparer, new ScoredCandidate(index, score, index), limit); + AddTopCandidate(heap, comparer, new ScoredCandidate(choices[index], score, index), limit); } - var selected = new ScoredCandidate[heap.Count]; + var selected = new ScoredCandidate[heap.Count]; for (var i = selected.Length - 1; i >= 0; i--) selected[i] = heap.ExtractDominating(); - return CreateParallelTopResults(choices, selected); + return CreateParallelTopResults(selected); } finally { @@ -97,12 +97,11 @@ private static IEnumerable> ExtractTopParallelCore( } private static IEnumerable> CreateParallelTopResults( - IList choices, - ScoredCandidate[] selected) + ScoredCandidate[] selected) { foreach (var candidate in selected) { - yield return new ExtractedResult(choices[candidate.Index], candidate.Score, candidate.Index); + yield return new ExtractedResult(candidate.Value, candidate.Score, candidate.Index); } } diff --git a/docs/performance-testing-2026-11-14.md b/docs/performance-testing-2026-11-14.md index c57fcdb..3ddc659 100644 --- a/docs/performance-testing-2026-11-14.md +++ b/docs/performance-testing-2026-11-14.md @@ -59,6 +59,8 @@ These before/after runs were separate in-process runs on the same host. The high On the existing lower-similarity weighted benchmark, stateless scoring measured 2.022 µs after the change versus 1.951 µs before; that difference is smaller than the run-to-run spread. Reused cached scoring measured 1.269 µs and 2.22 KB after versus 1.336 µs and 2.58 KB before. The cached allocation reduction is consistent with the token-set change. -Validation: 5,145 tests passed on both .NET 8 and .NET 10, including additional repeated-cache, tie-order, and one-shot enumerable cases. The library built across all configured target frameworks. The existing netcoreapp3.1 dependency support warning remained. +Validation: 5,147 tests passed on both .NET 8 and .NET 10, including additional repeated-cache, tie-order, one-shot enumerable, and post-return mutation cases. The library built across all configured target frameworks. The existing netcoreapp3.1 dependency support warning remained. + +PR review follow-up: parallel top-N now retains each selected value before returning its result iterator. Replacing an array element or clearing a list after the call no longer changes the scored results. A repeat in-process benchmark measured 553.7 µs and 3.95 KB at cutoff 0, and 404.1 µs and 3.96 KB at cutoff 95. The first cutoff-0 run after this fix was noisy, so these timings should be treated as indicative rather than a precise before/after comparison. The pooled top-N path reduces garbage collection pressure but still uses a buffer proportional to the number of choices. More work on tokenization and score-buffer size should be driven by broader input distributions and runtime measurements. From 0e95c6285e053bd4b735135699a1742ecbb05863 Mon Sep 17 00:00:00 2001 From: Yevhen Cherkes Date: Sat, 14 Nov 2026 17:33:14 +0100 Subject: [PATCH 4/6] Polish performance code after JetBrains inspection --- .../Extractor/ResultExtractor.Parallel.cs | 9 ++---- .../Composite/CachedWeightedRatioScorer.cs | 28 ++++++------------- .../Scorer/Composite/WeightedRatioScorer.cs | 28 ++++++------------- .../TokenSet/CachedTokenSetScorerBase.cs | 2 +- 4 files changed, 22 insertions(+), 45 deletions(-) diff --git a/FuzzySharp/Extractor/ResultExtractor.Parallel.cs b/FuzzySharp/Extractor/ResultExtractor.Parallel.cs index f6aaaf9..dec2717 100644 --- a/FuzzySharp/Extractor/ResultExtractor.Parallel.cs +++ b/FuzzySharp/Extractor/ResultExtractor.Parallel.cs @@ -10,7 +10,7 @@ namespace Raffinert.FuzzySharp.Extractor; public static partial class ResultExtractor { - private static readonly ParallelOptions DefaultParallelOptions = new ParallelOptions(); + private static readonly ParallelOptions DefaultParallelOptions = new(); public static partial class Parallel { @@ -61,11 +61,8 @@ private static IEnumerable> ExtractTopParallelCore( ParallelOptions parallelOptions) { ValidateLimit(limit); - if (limit == 0) - return Enumerable.Empty>(); - - if (choices.Count == 0) - return Enumerable.Empty>(); + if (limit == 0 || choices.Count == 0) + return []; var scores = ArrayPool.Shared.Rent(choices.Count); var comparer = ScoredCandidateComparer.Instance; diff --git a/FuzzySharp/SimilarityRatio/Scorer/Composite/CachedWeightedRatioScorer.cs b/FuzzySharp/SimilarityRatio/Scorer/Composite/CachedWeightedRatioScorer.cs index 23115c4..211134c 100644 --- a/FuzzySharp/SimilarityRatio/Scorer/Composite/CachedWeightedRatioScorer.cs +++ b/FuzzySharp/SimilarityRatio/Scorer/Composite/CachedWeightedRatioScorer.cs @@ -6,9 +6,8 @@ namespace Raffinert.FuzzySharp.SimilarityRatio.Scorer.Composite; public sealed class CachedWeightedRatioScorer : ICachedRatioScorer { - private static readonly double UNBASE_SCALE = .95; - private static readonly double PARTIAL_SCALE = .90; - private static readonly bool TRY_PARTIALS = true; + private const double UnbaseScale = .95; + private const double PartialScale = .90; private readonly ICachedStrategy _strategy; private readonly CachedDefaultRatioScorer _baseRatioScorer; @@ -36,34 +35,25 @@ public double Score(string input2) return 0; } - bool tryPartials = TRY_PARTIALS; - double unbaseScale = UNBASE_SCALE; - double partialScale = PARTIAL_SCALE; - double baseRatio = _baseRatioScorer.Score(input2); // Every other branch is scaled to at most 95. - if (baseRatio >= unbaseScale * 100) + if (baseRatio >= UnbaseScale * 100) return baseRatio; double lenRatio = (double)Math.Max(len1, len2) / Math.Min(len1, len2); + double partialScale = lenRatio > 8 ? .6 : PartialScale; - // if strings are similar length don't use partials - if (lenRatio < 1.5) tryPartials = false; - - // if one string is much shorter than the other - if (lenRatio > 8) partialScale = .6; - - if (tryPartials) + if (lenRatio >= 1.5) { double partial = Fuzz.PartialRatio(_input1, input2) * partialScale; - double partialSor = _tokenSortScorer.Score(input2) * unbaseScale * partialScale; - double partialSet = _tokenSetScorer.Score(input2) * unbaseScale * partialScale; + double partialSor = _tokenSortScorer.Score(input2) * UnbaseScale * partialScale; + double partialSet = _tokenSetScorer.Score(input2) * UnbaseScale * partialScale; return Math.Max(baseRatio, Math.Max(partial, Math.Max(partialSor, partialSet))); } - double tokenSort = _tokenSortScorer.Score(input2) * unbaseScale; - double tokenSet = _tokenSetScorer.Score(input2) * unbaseScale; + double tokenSort = _tokenSortScorer.Score(input2) * UnbaseScale; + double tokenSet = _tokenSetScorer.Score(input2) * UnbaseScale; return Math.Max(baseRatio, Math.Max(tokenSort, tokenSet)); } diff --git a/FuzzySharp/SimilarityRatio/Scorer/Composite/WeightedRatioScorer.cs b/FuzzySharp/SimilarityRatio/Scorer/Composite/WeightedRatioScorer.cs index c941764..3c84cd3 100644 --- a/FuzzySharp/SimilarityRatio/Scorer/Composite/WeightedRatioScorer.cs +++ b/FuzzySharp/SimilarityRatio/Scorer/Composite/WeightedRatioScorer.cs @@ -4,9 +4,8 @@ namespace Raffinert.FuzzySharp.SimilarityRatio.Scorer.Composite; public class WeightedRatioScorer : ScorerBase { - private static readonly double UNBASE_SCALE = .95; - private static readonly double PARTIAL_SCALE = .90; - private static readonly bool TRY_PARTIALS = true; + private const double UnbaseScale = .95; + private const double PartialScale = .90; public override double Score(string input1, string input2) { @@ -18,35 +17,26 @@ public override double Score(string input1, string input2) return 0; } - bool tryPartials = TRY_PARTIALS; - double unbaseScale = UNBASE_SCALE; - double partialScale = PARTIAL_SCALE; - double baseRatio = Fuzz.Ratio(input1, input2); // Every other branch is scaled to at most 95. - if (baseRatio >= unbaseScale * 100) + if (baseRatio >= UnbaseScale * 100) return baseRatio; double lenRatio = (double)Math.Max(len1, len2) / Math.Min(len1, len2); + double partialScale = lenRatio > 8 ? .6 : PartialScale; - // if strings are similar length don't use partials - if (lenRatio < 1.5) tryPartials = false; - - // if one string is much shorter than the other - if (lenRatio > 8) partialScale = .6; - - if (tryPartials) + if (lenRatio >= 1.5) { double partial = Fuzz.PartialRatio(input1, input2) * partialScale; // Keep the legacy full TokenSort/TokenSet partial branch for compatibility. - double partialSor = Fuzz.TokenSortRatio(input1, input2) * unbaseScale * partialScale; - double partialSet = Fuzz.TokenSetRatio(input1, input2) * unbaseScale * partialScale; + double partialSor = Fuzz.TokenSortRatio(input1, input2) * UnbaseScale * partialScale; + double partialSet = Fuzz.TokenSetRatio(input1, input2) * UnbaseScale * partialScale; return Math.Max(baseRatio, Math.Max(partial, Math.Max(partialSor, partialSet))); } - double tokenSort = Fuzz.TokenSortRatio(input1, input2) * unbaseScale; - double tokenSet = Fuzz.TokenSetRatio(input1, input2) * unbaseScale; + double tokenSort = Fuzz.TokenSortRatio(input1, input2) * UnbaseScale; + double tokenSet = Fuzz.TokenSetRatio(input1, input2) * UnbaseScale; return Math.Max(baseRatio, Math.Max(tokenSort, tokenSet)); } } diff --git a/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/TokenSet/CachedTokenSetScorerBase.cs b/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/TokenSet/CachedTokenSetScorerBase.cs index 182fc3d..39827aa 100644 --- a/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/TokenSet/CachedTokenSetScorerBase.cs +++ b/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/TokenSet/CachedTokenSetScorerBase.cs @@ -6,7 +6,7 @@ namespace Raffinert.FuzzySharp.SimilarityRatio.Scorer.StrategySensitive; public abstract class CachedTokenSetScorerBase(string input1) : ICachedRatioScorer { - private readonly HashSet _tokens1 = new HashSet(input1.SplitByAnySpace()); + private readonly HashSet _tokens1 = new(input1.SplitByAnySpace()); protected abstract FuzzySharp.Scorer Scorer { get; } public double Score(string input2) From 82b2c53acd2dad9f7b1f1c940d4d7ecdbcf2286a Mon Sep 17 00:00:00 2001 From: Yevhen Cherkes Date: Sat, 14 Nov 2026 17:34:33 +0100 Subject: [PATCH 5/6] Remove unused using directives Clean up unnecessary namespace imports in benchmark, test, and scorer files without changing behavior. --- FuzzySharp.Benchmarks/ExtractSelectionBenchmarks.cs | 1 - FuzzySharp.Test/FuzzyTests/CachedScorerTests.cs | 1 - .../Scorer/StrategySensitive/Simple/CachedDefaultRatioScorer.cs | 1 - 3 files changed, 3 deletions(-) diff --git a/FuzzySharp.Benchmarks/ExtractSelectionBenchmarks.cs b/FuzzySharp.Benchmarks/ExtractSelectionBenchmarks.cs index 86428db..32339dc 100644 --- a/FuzzySharp.Benchmarks/ExtractSelectionBenchmarks.cs +++ b/FuzzySharp.Benchmarks/ExtractSelectionBenchmarks.cs @@ -4,7 +4,6 @@ using Raffinert.FuzzySharp.SimilarityRatio; using Raffinert.FuzzySharp.SimilarityRatio.Scorer; using Raffinert.FuzzySharp.SimilarityRatio.Scorer.Composite; -using System.Threading.Tasks; namespace Raffinert.FuzzySharp.Benchmarks; diff --git a/FuzzySharp.Test/FuzzyTests/CachedScorerTests.cs b/FuzzySharp.Test/FuzzyTests/CachedScorerTests.cs index f5b11ab..a1dd6f9 100644 --- a/FuzzySharp.Test/FuzzyTests/CachedScorerTests.cs +++ b/FuzzySharp.Test/FuzzyTests/CachedScorerTests.cs @@ -1,4 +1,3 @@ -using System; using System.Linq; using Raffinert.FuzzySharp.PreProcess; using Raffinert.FuzzySharp.SimilarityRatio.Scorer.Composite; diff --git a/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/Simple/CachedDefaultRatioScorer.cs b/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/Simple/CachedDefaultRatioScorer.cs index de4b7c2..378775d 100644 --- a/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/Simple/CachedDefaultRatioScorer.cs +++ b/FuzzySharp/SimilarityRatio/Scorer/StrategySensitive/Simple/CachedDefaultRatioScorer.cs @@ -1,5 +1,4 @@ using System; -using Raffinert.FuzzySharp.PreProcess; using Raffinert.FuzzySharp.SimilarityRatio.Strategy; namespace Raffinert.FuzzySharp.SimilarityRatio.Scorer.StrategySensitive; From ee162ae555820185286cf0d60169d303a6226994 Mon Sep 17 00:00:00 2001 From: Yevhen Cherkes Date: Sat, 14 Nov 2026 17:54:03 +0100 Subject: [PATCH 6/6] Name weighted score early-exit threshold --- .../Scorer/Composite/CachedWeightedRatioScorer.cs | 4 ++-- .../SimilarityRatio/Scorer/Composite/WeightedRatioScorer.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/FuzzySharp/SimilarityRatio/Scorer/Composite/CachedWeightedRatioScorer.cs b/FuzzySharp/SimilarityRatio/Scorer/Composite/CachedWeightedRatioScorer.cs index 211134c..9d82f42 100644 --- a/FuzzySharp/SimilarityRatio/Scorer/Composite/CachedWeightedRatioScorer.cs +++ b/FuzzySharp/SimilarityRatio/Scorer/Composite/CachedWeightedRatioScorer.cs @@ -8,6 +8,7 @@ public sealed class CachedWeightedRatioScorer : ICachedRatioScorer { private const double UnbaseScale = .95; private const double PartialScale = .90; + private const double MaxScaledScore = UnbaseScale * 100; private readonly ICachedStrategy _strategy; private readonly CachedDefaultRatioScorer _baseRatioScorer; @@ -36,8 +37,7 @@ public double Score(string input2) } double baseRatio = _baseRatioScorer.Score(input2); - // Every other branch is scaled to at most 95. - if (baseRatio >= UnbaseScale * 100) + if (baseRatio >= MaxScaledScore) return baseRatio; double lenRatio = (double)Math.Max(len1, len2) / Math.Min(len1, len2); diff --git a/FuzzySharp/SimilarityRatio/Scorer/Composite/WeightedRatioScorer.cs b/FuzzySharp/SimilarityRatio/Scorer/Composite/WeightedRatioScorer.cs index 3c84cd3..877ccae 100644 --- a/FuzzySharp/SimilarityRatio/Scorer/Composite/WeightedRatioScorer.cs +++ b/FuzzySharp/SimilarityRatio/Scorer/Composite/WeightedRatioScorer.cs @@ -6,6 +6,7 @@ public class WeightedRatioScorer : ScorerBase { private const double UnbaseScale = .95; private const double PartialScale = .90; + private const double MaxScaledScore = UnbaseScale * 100; public override double Score(string input1, string input2) { @@ -18,8 +19,7 @@ public override double Score(string input1, string input2) } double baseRatio = Fuzz.Ratio(input1, input2); - // Every other branch is scaled to at most 95. - if (baseRatio >= UnbaseScale * 100) + if (baseRatio >= MaxScaledScore) return baseRatio; double lenRatio = (double)Math.Max(len1, len2) / Math.Min(len1, len2);