Skip to content

Commit

Permalink
Use StringPool from CommunityToolkit in ToUpperASCIIFast extension
Browse files Browse the repository at this point in the history
from be0736b (+ cherry-picked ccba93440) to this commit:
- `Database` reported runtime 0.64 s --> 0.68 s
- profiled `ParseCGIRecord` mean runtime 0.190 ms --> 0.179 ms
- `GameDBHelper` 1M lookups reported runtime 1.03 s --> 0.69 s
- managed allocations (w/o the 1M lookups) 131 MiB --> 123 MiB (fewer
strings but more char[]s?)

since `CompactGameInfo` obviously can't store `Span`s, I tried with
`StringSegment`, but it made both lookups *and init* slower, thanks MS
  • Loading branch information
YoshiRulz committed Dec 16, 2024
1 parent c2200cb commit 3095aca
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project>
<ItemGroup>
<PackageVersion Include="CommunityToolkit.HighPerformance" Version="8.3.2" />
<PackageVersion Include="Cyotek.Drawing.BitmapFont" Version="2.0.4" />
<PackageVersion Include="DotNetAnalyzers.DocumentationAnalyzers" Version="1.0.0-beta.59" />
<PackageVersion Include="Google.FlatBuffers" Version="23.5.26" /> <!-- last version with .NET Standard 2.0 support -->
Expand Down
1 change: 1 addition & 0 deletions src/BizHawk.Common/BizHawk.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PolySharpUsePublicAccessibilityForGeneratedTypes>true</PolySharpUsePublicAccessibilityForGeneratedTypes>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.HighPerformance" />
<PackageReference Include="Microsoft.Bcl.HashCode" />
<PackageReference Include="Microsoft.Win32.Registry" />
<PackageReference Include="PolySharp" />
Expand Down
4 changes: 3 additions & 1 deletion src/BizHawk.Common/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Linq;
using System.Runtime.CompilerServices;

using CommunityToolkit.HighPerformance.Buffers;

namespace BizHawk.Common.StringExtensions
{
public static class StringExtensions
Expand Down Expand Up @@ -199,7 +201,7 @@ public static string ToUpperASCIIFast(this string str)
var c = str[i];
a[i] = c is >= 'a' and <= 'z' ? unchecked((char) (c & ASCII_UPCASE_MASK)) : c;
}
return new(a);
return StringPool.Shared.GetOrAdd(a);
}
return str;
}
Expand Down

0 comments on commit 3095aca

Please sign in to comment.