Added a simple PtcFormatter library for deserialization/serialization, which does not require reflection, in use at PtcJumpTable and PtcProfiler; improves maintainability and simplicity/readability of affected code.

This commit is contained in:
LDj3SNuD
2021-02-14 03:04:12 +01:00
parent 18f576f49d
commit 8b9c28ac7d
6 changed files with 171 additions and 203 deletions

View File

@ -6,9 +6,12 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Threading;
using static ARMeilleure.Translation.PTC.PtcFormatter;
namespace ARMeilleure.Translation.PTC
{
public static class PtcProfiler
@ -31,7 +34,7 @@ namespace ARMeilleure.Translation.PTC
private static byte[] _lastHash;
internal static Dictionary<ulong, (ExecutionMode mode, bool highCq)> ProfiledFuncs { get; private set; }
internal static Dictionary<ulong, FuncProfile> ProfiledFuncs { get; private set; }
internal static bool Enabled { get; private set; }
@ -49,7 +52,7 @@ namespace ARMeilleure.Translation.PTC
_disposed = false;
ProfiledFuncs = new Dictionary<ulong, (ExecutionMode, bool)>();
ProfiledFuncs = new Dictionary<ulong, FuncProfile>();
Enabled = false;
}
@ -62,7 +65,7 @@ namespace ARMeilleure.Translation.PTC
lock (_lock)
{
ProfiledFuncs.TryAdd(address, (mode, highCq: false));
ProfiledFuncs.TryAdd(address, new FuncProfile(mode, highCq: false));
}
}
}
@ -77,7 +80,7 @@ namespace ARMeilleure.Translation.PTC
{
Debug.Assert(ProfiledFuncs.ContainsKey(address));
ProfiledFuncs[address] = (mode, highCq: true);
ProfiledFuncs[address] = new FuncProfile(mode, highCq: true);
}
}
}
@ -97,7 +100,7 @@ namespace ARMeilleure.Translation.PTC
if (!funcs.ContainsKey(address))
{
profiledFuncsToTranslate.Enqueue((address, profiledFunc.Value.mode, profiledFunc.Value.highCq));
profiledFuncsToTranslate.Enqueue((address, profiledFunc.Value.Mode, profiledFunc.Value.HighCq));
}
}
@ -194,7 +197,7 @@ namespace ARMeilleure.Translation.PTC
}
catch
{
ProfiledFuncs = new Dictionary<ulong, (ExecutionMode, bool)>();
ProfiledFuncs = new Dictionary<ulong, FuncProfile>();
InvalidateCompressedStream(compressedStream);
@ -230,26 +233,9 @@ namespace ARMeilleure.Translation.PTC
}
}
private static Dictionary<ulong, (ExecutionMode, bool)> Deserialize(Stream stream)
private static Dictionary<ulong, FuncProfile> Deserialize(Stream stream)
{
using (BinaryReader reader = new BinaryReader(stream, EncodingCache.UTF8NoBOM, true))
{
var profiledFuncs = new Dictionary<ulong, (ExecutionMode, bool)>();
int profiledFuncsCount = reader.ReadInt32();
for (int i = 0; i < profiledFuncsCount; i++)
{
ulong address = reader.ReadUInt64();
ExecutionMode mode = (ExecutionMode)reader.ReadInt32();
bool highCq = reader.ReadBoolean();
profiledFuncs.Add(address, (mode, highCq));
}
return profiledFuncs;
}
return DeserializeDictionary<ulong, FuncProfile>(stream, (stream) => DeserializeStructure<FuncProfile>(stream));
}
private static void InvalidateCompressedStream(FileStream compressedStream)
@ -348,20 +334,9 @@ namespace ARMeilleure.Translation.PTC
}
}
private static void Serialize(Stream stream, Dictionary<ulong, (ExecutionMode mode, bool highCq)> profiledFuncs)
private static void Serialize(Stream stream, Dictionary<ulong, FuncProfile> profiledFuncs)
{
using (BinaryWriter writer = new BinaryWriter(stream, EncodingCache.UTF8NoBOM, true))
{
writer.Write((int)profiledFuncs.Count);
foreach (var kv in profiledFuncs)
{
writer.Write((ulong)kv.Key); // address
writer.Write((int)kv.Value.mode);
writer.Write((bool)kv.Value.highCq);
}
}
SerializeDictionary<ulong, FuncProfile>(stream, profiledFuncs, (stream, structure) => SerializeStructure<FuncProfile>(stream, ref structure));
}
private struct Header
@ -371,6 +346,19 @@ namespace ARMeilleure.Translation.PTC
public uint InfoFileVersion;
}
[StructLayout(LayoutKind.Sequential, Pack = 1/*, Size = 5*/)]
internal struct FuncProfile
{
public ExecutionMode Mode;
public bool HighCq;
public FuncProfile(ExecutionMode mode, bool highCq)
{
Mode = mode;
HighCq = highCq;
}
}
internal static void Start()
{
if (Ptc.State == PtcState.Enabled ||