1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:45:42 +00:00

refactor: revert 50d9cad2f3

This commit is contained in:
Oliver Booth 2023-08-24 02:29:59 +01:00
parent f238d420f4
commit 129cbfb51f
Signed by: oliverbooth
GPG Key ID: B89D139977693FED
2 changed files with 2 additions and 19 deletions

View File

@ -38,7 +38,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
BigEndian/LittleEndian methods. BigEndian/LittleEndian methods.
- X10D: `Stream.GetHash<>` and `Stream.TryWriteHash<>` now throw ArgumentException in lieu of - X10D: `Stream.GetHash<>` and `Stream.TryWriteHash<>` now throw ArgumentException in lieu of
TypeInitializationException. TypeInitializationException.
- X10D: `Stream.GetHash<>` and `Stream.TryWriteHash<>` are now more efficient on second and subsequent calls.
### Removed ### Removed

View File

@ -1,5 +1,4 @@
using System.Collections.Concurrent; using System.Reflection;
using System.Reflection;
using System.Security.Cryptography; using System.Security.Cryptography;
namespace X10D.IO; namespace X10D.IO;
@ -9,8 +8,6 @@ namespace X10D.IO;
/// </summary> /// </summary>
public static partial class StreamExtensions public static partial class StreamExtensions
{ {
private static readonly ConcurrentDictionary<Type, HashAlgorithm> HashAlgorithmCache = new();
/// <summary> /// <summary>
/// Returns the hash of the current stream as an array of bytes using the specified hash algorithm. /// Returns the hash of the current stream as an array of bytes using the specified hash algorithm.
/// </summary> /// </summary>
@ -40,11 +37,6 @@ public static partial class StreamExtensions
throw new IOException(ExceptionMessages.StreamDoesNotSupportReading); throw new IOException(ExceptionMessages.StreamDoesNotSupportReading);
} }
if (HashAlgorithmCache.TryGetValue(typeof(T), out HashAlgorithm? cachedHashAlgorithm))
{
return cachedHashAlgorithm.ComputeHash(stream);
}
Type type = typeof(T); Type type = typeof(T);
MethodInfo? createMethod = type.GetMethods(BindingFlags.Public | BindingFlags.Static) MethodInfo? createMethod = type.GetMethods(BindingFlags.Public | BindingFlags.Static)
@ -60,7 +52,6 @@ public static partial class StreamExtensions
throw new ArgumentException(ExceptionMessages.HashAlgorithmCreateReturnedNull); throw new ArgumentException(ExceptionMessages.HashAlgorithmCreateReturnedNull);
} }
HashAlgorithmCache.TryAdd(type, crypt);
return crypt.ComputeHash(stream); return crypt.ComputeHash(stream);
} }
@ -100,13 +91,6 @@ public static partial class StreamExtensions
throw new IOException(ExceptionMessages.StreamDoesNotSupportReading); throw new IOException(ExceptionMessages.StreamDoesNotSupportReading);
} }
Span<byte> 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); Type type = typeof(T);
MethodInfo? createMethod = type.GetMethods(BindingFlags.Public | BindingFlags.Static) MethodInfo? createMethod = type.GetMethods(BindingFlags.Public | BindingFlags.Static)
@ -127,7 +111,7 @@ public static partial class StreamExtensions
throw new ArgumentException(ExceptionMessages.StreamTooLarge); throw new ArgumentException(ExceptionMessages.StreamTooLarge);
} }
HashAlgorithmCache.TryAdd(type, crypt); Span<byte> buffer = stackalloc byte[(int)stream.Length];
_ = stream.Read(buffer); // we don't care about the number of bytes read. we can ignore MustUseReturnValue _ = stream.Read(buffer); // we don't care about the number of bytes read. we can ignore MustUseReturnValue
return crypt.TryComputeHash(buffer, destination, out bytesWritten); return crypt.TryComputeHash(buffer, destination, out bytesWritten);
} }