Skip to content

Commit

Permalink
memory usage improvements
Browse files Browse the repository at this point in the history
- add console app for testinng allocations
- reduce allocations in various places
- some refactoring and cleanup
- bump version
  • Loading branch information
vforteli committed Feb 24, 2024
1 parent d88cac4 commit c51b7ed
Show file tree
Hide file tree
Showing 17 changed files with 450 additions and 1,660 deletions.
12 changes: 12 additions & 0 deletions ConsoleTester/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-counters": {
"version": "8.0.452401",
"commands": [
"dotnet-counters"
]
}
}
}
13 changes: 13 additions & 0 deletions ConsoleTester/ConsoleTester.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\FuzzySearchNet\FuzzySearchNet.csproj" />
</ItemGroup>
</Project>
29 changes: 29 additions & 0 deletions ConsoleTester/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Text;
using FuzzySearchNet;

// Console.WriteLine("Press enter to begin");
// Console.ReadLine();

//const string term = "foo";
const string term2 = "fooo--foo-----fo";
const string text = "foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--";

var count = 100000;

var stream = new MemoryStream(Encoding.UTF8.GetBytes(text));

Console.WriteLine($"Running fuzzy search with count {count}");
for (int i = 0; i < count; i++)
{

await foreach (var _ in FuzzySearch.FindLevenshteinAsync(term2, stream, new FuzzySearchOptions(3), leaveOpen: true))
{

}
stream.Position = 0;
}

// for (int i = 0; i < count; i++)
// {
// _ = FuzzySearch.FindLevenshtein(term2, text, new FuzzySearchOptions(3)).ToList();
// }
30 changes: 23 additions & 7 deletions FuzzySearchNet.Benchmark/BenchmarkFuzzySearch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BenchmarkDotNet.Attributes;
using System.Text;
using BenchmarkDotNet.Attributes;

namespace FuzzySearchNet.Benchmark;

Expand All @@ -8,20 +9,35 @@ public class BenchmarkFuzzySearch
private const string term2 = "fooo--foo-----fo";
private const string text = "foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--foo-----fo--foo-f--fooo--";

//[Benchmark]
//public void SubstitutionOnlyBufferingShort() => FuzzySearch.FindSubstitutionsOnlyBuffering(term, text, 1);
public static readonly MemoryStream textStream = new MemoryStream(Encoding.UTF8.GetBytes(text));

//[Benchmark]
//public void SubstitutionOnlyBufferingLong() => FuzzySearch.FindSubstitutionsOnlyBuffering(term2, text, 1);
[Benchmark]
public void SubstitutionOnlyShort() => FuzzySearch.FindSubstitutionsOnly(term, text, 1);

[Benchmark]
public void SubstitutionOnlyLong() => FuzzySearch.FindSubstitutionsOnly(term2, text, 1);

[Benchmark]
public void SubstitutionOnlyShor_3_distance() => FuzzySearch.FindSubstitutionsOnly(term, text, 3);

[Benchmark]
public void SubstitutionOnlyLong_3_distance() => FuzzySearch.FindSubstitutionsOnly(term2, text, 3);

//[Benchmark]
//public void SubstitutionOnlyBufferingLong3distance() => FuzzySearch.FindSubstitutionsOnlyBuffering(term2, text, 3);


[Benchmark]
public void LevenshteinLong()
{
_ = FuzzySearch.FindLevenshtein(term2, text, new FuzzySearchOptions(3)).ToList();
}

[Benchmark]
public async Task LevenshteinLongAsync()
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(text)); // this is not ideal, but the effect of doing this here is basically within the stddev of the benchmark
await foreach (var _ in FuzzySearch.FindLevenshteinAsync(term2, stream, new FuzzySearchOptions(3)))
{

}
}
}
2 changes: 1 addition & 1 deletion FuzzySearchNet.Benchmark/FuzzySearchNet.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
</ItemGroup>

<ItemGroup>
Expand Down
69 changes: 1 addition & 68 deletions FuzzySearchNet.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,71 +1,4 @@
using BenchmarkDotNet.Running;
using FuzzySearchNet.Benchmark;

var summary = BenchmarkRunner.Run<BenchmarkFuzzySearch>();



/*
*
| Method | Mean | Error | StdDev |
|----------------- |---------:|----------:|----------:|
| SubstitutionOnly | 4.564 us | 0.0905 us | 0.2551 us |
| Method | Mean | Error | StdDev |
|----------------- |---------:|---------:|---------:|
| SubstitutionOnly | 18.98 us | 0.373 us | 0.510 us |
| Method | Mean | Error | StdDev |
|--------------------------------------- |----------:|----------:|----------:|
| SubstitutionOnlyBufferingShort | 3.704 us | 0.0740 us | 0.0909 us |
| SubstitutionOnlyBufferingLong | 2.818 us | 0.0292 us | 0.0244 us |
| SubstitutionOnlyBufferingLong3distance | 4.396 us | 0.0501 us | 0.0492 us |
| Method | Mean | Error | StdDev |
|--------------------------------------- |---------:|----------:|----------:|
| SubstitutionOnlyBufferingShort | 2.628 us | 0.0389 us | 0.0570 us |
| SubstitutionOnlyBufferingLong | 2.031 us | 0.0404 us | 0.0580 us |
| SubstitutionOnlyBufferingLong3distance | 2.808 us | 0.0548 us | 0.0988 us |
| Method | Mean | Error | StdDev | Median |
|--------------------------------------- |---------:|----------:|----------:|---------:|
| SubstitutionOnlyBufferingShort | 2.434 us | 0.0264 us | 0.0220 us | 2.438 us |
| SubstitutionOnlyBufferingLong | 1.843 us | 0.0366 us | 0.0740 us | 1.810 us |
| SubstitutionOnlyBufferingLong3distance | 2.471 us | 0.0494 us | 0.1105 us | 2.468 us |
| Method | Mean | Error | StdDev | Median |
|--------------------------------------- |---------:|----------:|----------:|---------:|
| SubstitutionOnlyBufferingShort | 2.582 us | 0.0515 us | 0.1233 us | 2.543 us |
| SubstitutionOnlyBufferingLong | 2.053 us | 0.0407 us | 0.0743 us | 2.019 us |
| SubstitutionOnlyBufferingLong3distance | 2.819 us | 0.0559 us | 0.1285 us | 2.782 us |
| Method | Mean | Error | StdDev | Median |
|--------------------------------------- |---------:|----------:|----------:|---------:|
| SubstitutionOnlyBufferingShort | 2.690 us | 0.0536 us | 0.1539 us | 2.720 us |
| SubstitutionOnlyBufferingLong | 2.042 us | 0.0405 us | 0.1074 us | 2.001 us |
| SubstitutionOnlyBufferingLong3distance | 2.824 us | 0.0563 us | 0.1412 us | 2.754 us |
*/
_ = BenchmarkRunner.Run<BenchmarkFuzzySearch>();
Loading

0 comments on commit c51b7ed

Please sign in to comment.