Skip to content

Commit 09e7818

Browse files
Added simple GetHashCodeFromChars extension.
1 parent 23f22bd commit 09e7818

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

Benchmarks/EnumParseTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33

44
namespace Open.Text.Benchmarks;
55

6+
/*
7+
| Method | UseValid | IgnoreCase | Mean | Error | StdDev | Ratio | RatioSD | Allocated |
8+
|------------------------ |--------- |----------- |---------:|---------:|---------:|------:|--------:|----------:|
9+
| CompiledSwitchByLengths | True | False | 12.63 ns | 0.285 ns | 0.776 ns | 1.00 | 0.00 | - |
10+
| EnumValueParse | True | False | 50.22 ns | 0.543 ns | 0.481 ns | 3.98 | 0.17 | - |
11+
| FastEnumParse | True | False | 46.10 ns | 0.780 ns | 0.730 ns | 3.62 | 0.22 | - |
12+
| | | | | | | | | |
13+
| CompiledSwitchByLengths | True | True | 17.29 ns | 0.185 ns | 0.173 ns | 1.00 | 0.00 | - |
14+
| EnumValueParse | True | True | 59.01 ns | 0.340 ns | 0.284 ns | 3.41 | 0.04 | - |
15+
| FastEnumParse | True | True | 72.12 ns | 0.451 ns | 0.352 ns | 4.17 | 0.05 | - |
16+
*/
17+
618
[MemoryDiagnoser]
719
public class EnumParseTests
820
{
@@ -16,7 +28,7 @@ void SetTests()
1628
? (ignoreCase ? new ValidTestsIC() : new ValidTests())
1729
: (ignoreCase ? new InvalidTestsIC() : new InvalidTests());
1830

19-
[Params(true, false)]
31+
[Params(true/*, false*/)]
2032
public bool UseValid
2133
{
2234
get => useValid;

Benchmarks/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using BenchmarkDotNet.Running;
22
using Open.Text.Benchmarks;
33

4-
//BenchmarkRunner.Run<EnumParseTests>();
5-
BenchmarkRunner.Run<EnumToStringTests>();
4+
BenchmarkRunner.Run<EnumParseTests>();
5+
//enchmarkRunner.Run<EnumToStringTests>();
66
//BenchmarkRunner.Run<CharAssumptionTests>();
77
//BenchmarkRunner.Run<EnumAttributeTests>();
88
//BenchmarkRunner.Run<IsDefinedTests>();

Source/Extensions._.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,4 +433,25 @@ public static string Supplant<T>(this string format, T[]? values, CultureInfo? c
433433
1 => string.Format(cultureInfo ?? CultureInfo.InvariantCulture, format, values[0]),
434434
_ => string.Format(cultureInfo ?? CultureInfo.InvariantCulture, format, values as object[] ?? values.Cast<object>().ToArray()),
435435
};
436+
437+
/// <summary>
438+
/// A hashing algorithm that is nearly as fast as the default string.GetHashCode()
439+
/// but can be used for any sequence of characters reliably.
440+
/// </summary>
441+
/// <remarks>
442+
/// Setting the <paramref name="maxChars"/> parameter to a lower number
443+
/// will dramatically increase the speed as more characters requires more iterations.
444+
/// </remarks>
445+
public static int GetHashCodeFromChars(this ReadOnlySpan<char> chars, int maxChars = int.MaxValue)
446+
{
447+
int hash_value = 0;
448+
int length = Math.Min(chars.Length, maxChars);
449+
for (var i = 0; i < length; i++)
450+
{
451+
ref readonly char c = ref chars[i];
452+
hash_value = (hash_value * 31) ^ c;
453+
}
454+
455+
return hash_value;
456+
}
436457
}

0 commit comments

Comments
 (0)