diff --git a/src/Nethermind/Nethermind.State/StorageTree.cs b/src/Nethermind/Nethermind.State/StorageTree.cs index 472441c9e3b..b6f9148203a 100644 --- a/src/Nethermind/Nethermind.State/StorageTree.cs +++ b/src/Nethermind/Nethermind.State/StorageTree.cs @@ -5,6 +5,8 @@ using System.Collections.Frozen; using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics; using Nethermind.Core.Crypto; using Nethermind.Core.Extensions; using Nethermind.Logging; @@ -46,12 +48,15 @@ public StorageTree(IScopedTrieStore? trieStore, Hash256 rootHash, ILogManager? l TrieType = TrieType.Storage; } - private static void ComputeKey(in UInt256 index, ref Span key) + private static void ComputeKey(in UInt256 index, in Span key) { index.ToBigEndian(key); - // in situ calculation - KeccakHash.ComputeHashBytesToSpan(key, key); + ValueHash256 keyHash = KeccakCache.Compute(key); + + // Assign to update the argument + Unsafe.As>(ref MemoryMarshal.GetReference(key)) + = Unsafe.As>(ref keyHash); } [SkipLocalsInit] @@ -63,7 +68,7 @@ public byte[] Get(in UInt256 index, Hash256? storageRoot = null) } Span key = stackalloc byte[32]; - ComputeKey(index, ref key); + ComputeKey(index, key); return GetArray(key, storageRoot); } @@ -92,7 +97,7 @@ public void Set(in UInt256 index, byte[] value) else { Span key = stackalloc byte[32]; - ComputeKey(index, ref key); + ComputeKey(index, in key); SetInternal(key, value); } }