This commit is contained in:
LDj3SNuD 2021-02-15 03:46:38 +01:00
parent d49d8d6579
commit e68376d70b
4 changed files with 23 additions and 29 deletions

View file

@ -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<Hash128>();
Span<byte> currentSizeHashBytes = new byte[hashSize];
compressedStream.Read(currentSizeHashBytes);
Hash128 currentSizeHash = MemoryMarshal.Read<Hash128>(currentSizeHashBytes);
Hash128 currentSizeHash = DeserializeStructure<Hash128>(compressedStream);
Span<byte> 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<Hash128>();
Span<byte> currentHashBytes = new byte[hashSize];
stream.Read(currentHashBytes);
Hash128 currentHash = MemoryMarshal.Read<Hash128>(currentHashBytes);
stream.Seek(0L, SeekOrigin.Begin);
Hash128 currentHash = DeserializeStructure<Hash128>(stream);
ReadOnlySpan<byte> streamBytes = new(stream.PositionPointer, (int)(stream.Length - stream.Position));
Hash128 expectedHash = XXHash128.ComputeHash(streamBytes);
@ -447,11 +444,8 @@ namespace ARMeilleure.Translation.PTC
ReadOnlySpan<byte> streamBytes = new(stream.PositionPointer, (int)(stream.Length - stream.Position));
Hash128 hash = XXHash128.ComputeHash(streamBytes);
Span<byte> hashBytes = new byte[hashSize];
MemoryMarshal.Write<Hash128>(hashBytes, ref hash);
stream.Seek(0L, SeekOrigin.Begin);
stream.Write(hashBytes);
SerializeStructure(stream, hash);
translatedFuncsCount = GetInfosEntriesCount();

View file

@ -90,11 +90,11 @@ namespace ARMeilleure.Translation.PTC
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SerializeDictionary<TKey, TValue>(Stream stream, Dictionary<TKey, TValue> dictionary, Action<Stream, TValue> valueAction) where TKey : unmanaged
{
SerializeStructure<int>(stream, ref Unsafe.AsRef(dictionary.Count));
SerializeStructure<int>(stream, dictionary.Count);
foreach ((TKey key, TValue value) in dictionary)
{
SerializeStructure<TKey>(stream, ref Unsafe.AsRef(key));
SerializeStructure<TKey>(stream, key);
valueAction(stream, value);
}
}
@ -102,16 +102,16 @@ namespace ARMeilleure.Translation.PTC
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SerializeList<T>(Stream stream, List<T> list) where T : unmanaged
{
SerializeStructure<int>(stream, ref Unsafe.AsRef(list.Count));
SerializeStructure<int>(stream, list.Count);
foreach (T item in list)
{
SerializeStructure<T>(stream, ref Unsafe.AsRef(item));
SerializeStructure<T>(stream, item);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SerializeStructure<T>(Stream stream, ref T structure) where T : unmanaged
public static void SerializeStructure<T>(Stream stream, T structure) where T : unmanaged
{
Span<T> spanT = MemoryMarshal.CreateSpan(ref structure, 1);
stream.Write(MemoryMarshal.AsBytes(spanT));

View file

@ -85,24 +85,24 @@ namespace ARMeilleure.Translation.PTC
{
int size = 0;
size += GetSerializeSizeList<TableEntry<DirectHostAddress>>(ptcJumpTable._jumpTable);
size += GetSerializeSizeList<TableEntry<IndirectHostAddress>>(ptcJumpTable._dynamicTable);
size += GetSerializeSizeList(ptcJumpTable._jumpTable);
size += GetSerializeSizeList(ptcJumpTable._dynamicTable);
size += GetSerializeSizeList<ulong>(ptcJumpTable.Targets);
size += GetSerializeSizeDictionary<ulong, List<int>>(ptcJumpTable.Dependants, (list) => GetSerializeSizeList<int>(list));
size += GetSerializeSizeDictionary<ulong, List<int>>(ptcJumpTable.Owners, (list) => GetSerializeSizeList<int>(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<TableEntry<DirectHostAddress>>(stream, ptcJumpTable._jumpTable);
SerializeList<TableEntry<IndirectHostAddress>>(stream, ptcJumpTable._dynamicTable);
SerializeList(stream, ptcJumpTable._jumpTable);
SerializeList(stream, ptcJumpTable._dynamicTable);
SerializeList<ulong>(stream, ptcJumpTable.Targets);
SerializeDictionary<ulong, List<int>>(stream, ptcJumpTable.Dependants, (stream, list) => SerializeList<int>(stream, list));
SerializeDictionary<ulong, List<int>>(stream, ptcJumpTable.Owners, (stream, list) => SerializeList<int>(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)

View file

@ -336,7 +336,7 @@ namespace ARMeilleure.Translation.PTC
private static void Serialize(Stream stream, Dictionary<ulong, FuncProfile> profiledFuncs)
{
SerializeDictionary<ulong, FuncProfile>(stream, profiledFuncs, (stream, structure) => SerializeStructure<FuncProfile>(stream, ref structure));
SerializeDictionary(stream, profiledFuncs, (stream, structure) => SerializeStructure(stream, structure));
}
private struct Header