From 129cbfb51f1ff8e0bf2b052263442ed534d19098 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 24 Aug 2023 02:29:59 +0100 Subject: [PATCH] refactor: revert 50d9cad2f3225d1155e2a91d9e7110aee6ac8006 --- CHANGELOG.md | 1 - X10D/src/IO/StreamExtensions.cs | 20 ++------------------ 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65394b2..5f31c88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,7 +38,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 BigEndian/LittleEndian methods. - X10D: `Stream.GetHash<>` and `Stream.TryWriteHash<>` now throw ArgumentException in lieu of TypeInitializationException. -- X10D: `Stream.GetHash<>` and `Stream.TryWriteHash<>` are now more efficient on second and subsequent calls. ### Removed diff --git a/X10D/src/IO/StreamExtensions.cs b/X10D/src/IO/StreamExtensions.cs index 4bf3df8..15e50e0 100644 --- a/X10D/src/IO/StreamExtensions.cs +++ b/X10D/src/IO/StreamExtensions.cs @@ -1,5 +1,4 @@ -using System.Collections.Concurrent; -using System.Reflection; +using System.Reflection; using System.Security.Cryptography; namespace X10D.IO; @@ -9,8 +8,6 @@ namespace X10D.IO; /// public static partial class StreamExtensions { - private static readonly ConcurrentDictionary HashAlgorithmCache = new(); - /// /// Returns the hash of the current stream as an array of bytes using the specified hash algorithm. /// @@ -40,11 +37,6 @@ public static partial class StreamExtensions throw new IOException(ExceptionMessages.StreamDoesNotSupportReading); } - if (HashAlgorithmCache.TryGetValue(typeof(T), out HashAlgorithm? cachedHashAlgorithm)) - { - return cachedHashAlgorithm.ComputeHash(stream); - } - Type type = typeof(T); MethodInfo? createMethod = type.GetMethods(BindingFlags.Public | BindingFlags.Static) @@ -60,7 +52,6 @@ public static partial class StreamExtensions throw new ArgumentException(ExceptionMessages.HashAlgorithmCreateReturnedNull); } - HashAlgorithmCache.TryAdd(type, crypt); return crypt.ComputeHash(stream); } @@ -100,13 +91,6 @@ public static partial class StreamExtensions throw new IOException(ExceptionMessages.StreamDoesNotSupportReading); } - Span buffer = stackalloc byte[(int)stream.Length]; - if (HashAlgorithmCache.TryGetValue(typeof(T), out HashAlgorithm? cachedHashAlgorithm)) - { - _ = stream.Read(buffer); // we don't care about the number of bytes read. we can ignore MustUseReturnValue - return cachedHashAlgorithm.TryComputeHash(buffer, destination, out bytesWritten); - } - Type type = typeof(T); MethodInfo? createMethod = type.GetMethods(BindingFlags.Public | BindingFlags.Static) @@ -127,7 +111,7 @@ public static partial class StreamExtensions throw new ArgumentException(ExceptionMessages.StreamTooLarge); } - HashAlgorithmCache.TryAdd(type, crypt); + Span buffer = stackalloc byte[(int)stream.Length]; _ = stream.Read(buffer); // we don't care about the number of bytes read. we can ignore MustUseReturnValue return crypt.TryComputeHash(buffer, destination, out bytesWritten); }