Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion FuzzySharp.Benchmarks/ExtractSelectionBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
27 changes: 27 additions & 0 deletions FuzzySharp.Benchmarks/WeightedRatioHighSimilarityBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
17 changes: 16 additions & 1 deletion FuzzySharp.Test/FuzzyTests/CachedScorerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Linq;
using Raffinert.FuzzySharp.PreProcess;
using Raffinert.FuzzySharp.SimilarityRatio.Scorer.Composite;
Expand All @@ -16,6 +15,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]
Expand Down Expand Up @@ -59,4 +60,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);
}
}
}
60 changes: 60 additions & 0 deletions FuzzySharp.Test/FuzzyTests/ExtractorSelectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,66 @@ 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));
}
}
}

[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<string> { "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()
{
var scorer = new MapScorer(("a", 10), ("winner", 90), ("b", 20));
var one = ResultExtractor.Parallel.ExtractOne(
"query", new OneShotEnumerable<string>(["a", "winner", "b"]),
IdentityProcessor, scorer, parallelOptions: TestParallelOptions);
AssertResult(one, "winner", 90, 1);

var top = ResultExtractor.Parallel.ExtractTop(
"query", new OneShotEnumerable<string>(["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()
{
Expand Down
18 changes: 8 additions & 10 deletions FuzzySharp/Extractor/ResultExtractor.Parallel.Cached.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class Cached
{
public static IEnumerable<ExtractedResult<T>> ExtractWithoutOrder<T>(IEnumerable<T> choices, Func<T, string> extractor, Func<string, string> processor, ICachedRatioScorer scorer, double cutoff = 0, ParallelOptions parallelOptions = null)
{
var materializedChoices = choices.ToList();
var materializedChoices = GetIndexedChoices(choices);
var result = new ExtractedResult<T>[materializedChoices.Count];

System.Threading.Tasks.Parallel.ForEach(materializedChoices, parallelOptions ?? DefaultParallelOptions, (choice, _, index) =>
Expand All @@ -30,7 +30,7 @@ public static IEnumerable<ExtractedResult<T>> ExtractWithoutOrder<T>(IEnumerable

public static IEnumerable<ExtractedResult<string>> ExtractWithoutOrder(IEnumerable<string> choices, Func<string, string> processor, ICachedRatioScorer scorer, double cutoff = 0, ParallelOptions parallelOptions = null)
{
var materializedChoices = choices.ToList();
var materializedChoices = GetIndexedChoices(choices);
var result = new ExtractedResult<string>[materializedChoices.Count];

System.Threading.Tasks.Parallel.ForEach(materializedChoices, parallelOptions ?? DefaultParallelOptions, (choice, _, index) =>
Expand All @@ -46,13 +46,13 @@ public static IEnumerable<ExtractedResult<string>> ExtractWithoutOrder(IEnumerab

public static ExtractedResult<T> ExtractOne<T>(IEnumerable<T> choices, Func<T, string> extractor, Func<string, string> 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<string> ExtractOne(IEnumerable<string> choices, Func<string, string> 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);
}

Expand All @@ -72,9 +72,8 @@ public static IEnumerable<ExtractedResult<T>> ExtractTop<T>(IEnumerable<T> choic
if (limit == 0)
return Enumerable.Empty<ExtractedResult<T>>();

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<ExtractedResult<string>> ExtractTop(IEnumerable<string> choices, Func<string, string> processor, ICachedRatioScorer calculator, int limit, double cutoff = 0, ParallelOptions parallelOptions = null)
Expand All @@ -83,9 +82,8 @@ public static IEnumerable<ExtractedResult<string>> ExtractTop(IEnumerable<string
if (limit == 0)
return Enumerable.Empty<ExtractedResult<string>>();

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);
}
}
}
Expand Down
Loading
Loading