From e68376d70be387fbd55932c8c3c1a0d25ea3a17a Mon Sep 17 00:00:00 2001 From: LDj3SNuD Date: Mon, 15 Feb 2021 03:46:38 +0100 Subject: [PATCH] Nits. --- ARMeilleure/Translation/PTC/Ptc.cs | 20 +++++++------------- ARMeilleure/Translation/PTC/PtcFormatter.cs | 10 +++++----- ARMeilleure/Translation/PTC/PtcJumpTable.cs | 20 ++++++++++---------- ARMeilleure/Translation/PTC/PtcProfiler.cs | 2 +- 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/ARMeilleure/Translation/PTC/Ptc.cs b/ARMeilleure/Translation/PTC/Ptc.cs index 447496cbf..163e3efae 100644 --- a/ARMeilleure/Translation/PTC/Ptc.cs +++ b/ARMeilleure/Translation/PTC/Ptc.cs @@ -18,6 +18,8 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; +using static ARMeilleure.Translation.PTC.PtcFormatter; + namespace ARMeilleure.Translation.PTC { public static class Ptc @@ -206,11 +208,7 @@ namespace ARMeilleure.Translation.PTC using (FileStream compressedStream = new(fileName, FileMode.Open)) using (DeflateStream deflateStream = new(compressedStream, CompressionMode.Decompress, true)) { - int hashSize = Unsafe.SizeOf(); - - Span currentSizeHashBytes = new byte[hashSize]; - compressedStream.Read(currentSizeHashBytes); - Hash128 currentSizeHash = MemoryMarshal.Read(currentSizeHashBytes); + Hash128 currentSizeHash = DeserializeStructure(compressedStream); Span sizeBytes = new byte[sizeof(int)]; compressedStream.Read(sizeBytes); @@ -244,11 +242,10 @@ namespace ARMeilleure.Translation.PTC return false; } - stream.Seek(0L, SeekOrigin.Begin); + int hashSize = Unsafe.SizeOf(); - Span currentHashBytes = new byte[hashSize]; - stream.Read(currentHashBytes); - Hash128 currentHash = MemoryMarshal.Read(currentHashBytes); + stream.Seek(0L, SeekOrigin.Begin); + Hash128 currentHash = DeserializeStructure(stream); ReadOnlySpan streamBytes = new(stream.PositionPointer, (int)(stream.Length - stream.Position)); Hash128 expectedHash = XXHash128.ComputeHash(streamBytes); @@ -447,11 +444,8 @@ namespace ARMeilleure.Translation.PTC ReadOnlySpan streamBytes = new(stream.PositionPointer, (int)(stream.Length - stream.Position)); Hash128 hash = XXHash128.ComputeHash(streamBytes); - Span hashBytes = new byte[hashSize]; - MemoryMarshal.Write(hashBytes, ref hash); - stream.Seek(0L, SeekOrigin.Begin); - stream.Write(hashBytes); + SerializeStructure(stream, hash); translatedFuncsCount = GetInfosEntriesCount(); diff --git a/ARMeilleure/Translation/PTC/PtcFormatter.cs b/ARMeilleure/Translation/PTC/PtcFormatter.cs index e4d450593..7346b4844 100644 --- a/ARMeilleure/Translation/PTC/PtcFormatter.cs +++ b/ARMeilleure/Translation/PTC/PtcFormatter.cs @@ -90,11 +90,11 @@ namespace ARMeilleure.Translation.PTC [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void SerializeDictionary(Stream stream, Dictionary dictionary, Action valueAction) where TKey : unmanaged { - SerializeStructure(stream, ref Unsafe.AsRef(dictionary.Count)); + SerializeStructure(stream, dictionary.Count); foreach ((TKey key, TValue value) in dictionary) { - SerializeStructure(stream, ref Unsafe.AsRef(key)); + SerializeStructure(stream, key); valueAction(stream, value); } } @@ -102,16 +102,16 @@ namespace ARMeilleure.Translation.PTC [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void SerializeList(Stream stream, List list) where T : unmanaged { - SerializeStructure(stream, ref Unsafe.AsRef(list.Count)); + SerializeStructure(stream, list.Count); foreach (T item in list) { - SerializeStructure(stream, ref Unsafe.AsRef(item)); + SerializeStructure(stream, item); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void SerializeStructure(Stream stream, ref T structure) where T : unmanaged + public static void SerializeStructure(Stream stream, T structure) where T : unmanaged { Span spanT = MemoryMarshal.CreateSpan(ref structure, 1); stream.Write(MemoryMarshal.AsBytes(spanT)); diff --git a/ARMeilleure/Translation/PTC/PtcJumpTable.cs b/ARMeilleure/Translation/PTC/PtcJumpTable.cs index 4c0c15057..75dcba50c 100644 --- a/ARMeilleure/Translation/PTC/PtcJumpTable.cs +++ b/ARMeilleure/Translation/PTC/PtcJumpTable.cs @@ -85,24 +85,24 @@ namespace ARMeilleure.Translation.PTC { int size = 0; - size += GetSerializeSizeList>(ptcJumpTable._jumpTable); - size += GetSerializeSizeList>(ptcJumpTable._dynamicTable); + size += GetSerializeSizeList(ptcJumpTable._jumpTable); + size += GetSerializeSizeList(ptcJumpTable._dynamicTable); - size += GetSerializeSizeList(ptcJumpTable.Targets); - size += GetSerializeSizeDictionary>(ptcJumpTable.Dependants, (list) => GetSerializeSizeList(list)); - size += GetSerializeSizeDictionary>(ptcJumpTable.Owners, (list) => GetSerializeSizeList(list)); + size += GetSerializeSizeList(ptcJumpTable.Targets); + size += GetSerializeSizeDictionary(ptcJumpTable.Dependants, (list) => GetSerializeSizeList(list)); + size += GetSerializeSizeDictionary(ptcJumpTable.Owners, (list) => GetSerializeSizeList(list)); return size; } public static void Serialize(Stream stream, PtcJumpTable ptcJumpTable) { - SerializeList>(stream, ptcJumpTable._jumpTable); - SerializeList>(stream, ptcJumpTable._dynamicTable); + SerializeList(stream, ptcJumpTable._jumpTable); + SerializeList(stream, ptcJumpTable._dynamicTable); - SerializeList(stream, ptcJumpTable.Targets); - SerializeDictionary>(stream, ptcJumpTable.Dependants, (stream, list) => SerializeList(stream, list)); - SerializeDictionary>(stream, ptcJumpTable.Owners, (stream, list) => SerializeList(stream, list)); + SerializeList(stream, ptcJumpTable.Targets); + SerializeDictionary(stream, ptcJumpTable.Dependants, (stream, list) => SerializeList(stream, list)); + SerializeDictionary(stream, ptcJumpTable.Owners, (stream, list) => SerializeList(stream, list)); } public void Initialize(JumpTable jumpTable) diff --git a/ARMeilleure/Translation/PTC/PtcProfiler.cs b/ARMeilleure/Translation/PTC/PtcProfiler.cs index 246556a47..8782a7945 100644 --- a/ARMeilleure/Translation/PTC/PtcProfiler.cs +++ b/ARMeilleure/Translation/PTC/PtcProfiler.cs @@ -336,7 +336,7 @@ namespace ARMeilleure.Translation.PTC private static void Serialize(Stream stream, Dictionary profiledFuncs) { - SerializeDictionary(stream, profiledFuncs, (stream, structure) => SerializeStructure(stream, ref structure)); + SerializeDictionary(stream, profiledFuncs, (stream, structure) => SerializeStructure(stream, structure)); } private struct Header