diff --git a/.semrelease/this_release b/.semrelease/this_release index e9cd5b0..a2e9703 100644 --- a/.semrelease/this_release +++ b/.semrelease/this_release @@ -1,3 +1,5 @@ # This file has been cleaned up post-releasing version 0.0.2. # Generate its content quickly using the following command: # git log origin..HEAD | grep "^\s" > .semrelease/this_release +#!VERSION=0.0.3 +- impr: Expose shared static Factory on XxHash128Hasher, XxHash3Hasher and Crc32Hasher for cached reuse via Signum. diff --git a/Ddth.Signum.Tests/HasherTests.cs b/Ddth.Signum.Tests/HasherTests.cs index 167937c..5f099ef 100644 --- a/Ddth.Signum.Tests/HasherTests.cs +++ b/Ddth.Signum.Tests/HasherTests.cs @@ -11,6 +11,31 @@ public static IEnumerable Hashers() yield return new object[] { (Func)(() => new Crc32Hasher()), 4 }; } + [Fact] + public void Factory_CreatesNewInstanceOfExpectedType() + { + Assert.IsType(XxHash128Hasher.Factory()); + Assert.IsType(XxHash3Hasher.Factory()); + Assert.IsType(Crc32Hasher.Factory()); + + // Each invocation returns a distinct instance. + Assert.NotSame(XxHash3Hasher.Factory(), XxHash3Hasher.Factory()); + } + + [Fact] + public void Factory_MatchesEquivalentInlineFactory() + { + Assert.Equal( + Signum.ChecksumHex("hello", () => new XxHash3Hasher()), + Signum.ChecksumHex("hello", XxHash3Hasher.Factory)); + } + + [Fact] + public void XxHash128Factory_MatchesDefault() + { + Assert.Equal(Signum.ChecksumHex("hello"), Signum.ChecksumHex("hello", XxHash128Hasher.Factory)); + } + [Theory] [MemberData(nameof(Hashers))] public void Hasher_ProducesExpectedWidth(Func factory, int expectedWidth) diff --git a/Ddth.Signum/Crc32Hasher.cs b/Ddth.Signum/Crc32Hasher.cs index 92e79cf..768b527 100644 --- a/Ddth.Signum/Crc32Hasher.cs +++ b/Ddth.Signum/Crc32Hasher.cs @@ -9,6 +9,9 @@ namespace Ddth.Signum; /// public sealed class Crc32Hasher : IHasher { + /// Shared factory that creates a new instance. + public static readonly Func Factory = () => new Crc32Hasher(); + private readonly Crc32 _inner = new(); /// diff --git a/Ddth.Signum/Signum.cs b/Ddth.Signum/Signum.cs index d85cbbe..efe85b4 100644 --- a/Ddth.Signum/Signum.cs +++ b/Ddth.Signum/Signum.cs @@ -13,7 +13,8 @@ namespace Ddth.Signum; /// public static class Signum { - private static readonly Fingerprinter Default = new(); + private static readonly Fingerprinter Default = + new(new FingerprintOptions { HasherFactory = XxHash128Hasher.Factory }); private static readonly ConcurrentDictionary, Fingerprinter> Cache = new(); diff --git a/Ddth.Signum/XxHash128Hasher.cs b/Ddth.Signum/XxHash128Hasher.cs index aca53f9..dbb4b88 100644 --- a/Ddth.Signum/XxHash128Hasher.cs +++ b/Ddth.Signum/XxHash128Hasher.cs @@ -8,6 +8,9 @@ namespace Ddth.Signum; /// public sealed class XxHash128Hasher : IHasher { + /// Shared factory that creates a new instance. + public static readonly Func Factory = () => new XxHash128Hasher(); + private readonly XxHash128 _inner = new(); /// diff --git a/Ddth.Signum/XxHash3Hasher.cs b/Ddth.Signum/XxHash3Hasher.cs index ee023dd..b2794d2 100644 --- a/Ddth.Signum/XxHash3Hasher.cs +++ b/Ddth.Signum/XxHash3Hasher.cs @@ -8,6 +8,9 @@ namespace Ddth.Signum; /// public sealed class XxHash3Hasher : IHasher { + /// Shared factory that creates a new instance. + public static readonly Func Factory = () => new XxHash3Hasher(); + private readonly XxHash3 _inner = new(); /// diff --git a/README.md b/README.md index 1ec8261..fd7d133 100644 --- a/README.md +++ b/README.md @@ -70,8 +70,10 @@ The library ships three hashers (all from `System.IO.Hashing`): `XxHash128Hasher `FingerprintOptions.HasherFactory`, or via the optional parameter on the `Signum` helper: ```csharp -// Use a built-in hasher through the static helper: -string hex = Signum.ChecksumHex(myObject, () => new XxHash3Hasher()); +// Use a built-in hasher through the static helper. Each hasher exposes a shared +// `Factory` delegate, which lets the Signum helper cache and reuse a single +// Fingerprinter per hasher type: +string hex = Signum.ChecksumHex(myObject, XxHash3Hasher.Factory); ``` Provide your own `IHasher` to use any other algorithm: