Avoid buffer allocations in CodeGenContext.GetCode(). Avoid stream allocations in PTC.PtcInfo.

Refactoring/nits.
This commit is contained in:
LDj3SNuD 2021-01-30 04:32:53 +01:00
parent b767d1a6b0
commit f44271a617
6 changed files with 78 additions and 90 deletions

View file

@ -86,8 +86,8 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
{ {
NumberLocals(cfg); NumberLocals(cfg);
using (AllocationContext context = new AllocationContext(stackAlloc, regMasks, _intervals.Count)) using AllocationContext context = new AllocationContext(stackAlloc, regMasks, _intervals.Count);
{
BuildIntervals(cfg, context); BuildIntervals(cfg, context);
for (int index = 0; index < _intervals.Count; index++) for (int index = 0; index < _intervals.Count; index++)
@ -131,7 +131,6 @@ namespace ARMeilleure.CodeGen.RegisterAllocators
return new AllocationResult(context.IntUsedRegisters, context.VecUsedRegisters, context.StackAlloc.TotalSize); return new AllocationResult(context.IntUsedRegisters, context.VecUsedRegisters, context.StackAlloc.TotalSize);
} }
}
private void AllocateInterval(AllocationContext context, LiveInterval current, int cIndex) private void AllocateInterval(AllocationContext context, LiveInterval current, int cIndex)
{ {

View file

@ -302,13 +302,11 @@ namespace ARMeilleure.CodeGen.X86
{ {
Assembler assembler = new Assembler(codeStream, _ptcInfo); Assembler assembler = new Assembler(codeStream, _ptcInfo);
Span<byte> buffer;
for (int index = 0; index < _jumps.Count; index++) for (int index = 0; index < _jumps.Count; index++)
{ {
Jump jump = _jumps[index]; Jump jump = _jumps[index];
buffer = new byte[jump.JumpPosition - _stream.Position]; Span<byte> buffer = new byte[jump.JumpPosition - _stream.Position];
_stream.Read(buffer); _stream.Read(buffer);
_stream.Seek(_ptcDisabled ? ReservedBytesForJump : jump.InstSize, SeekOrigin.Current); _stream.Seek(_ptcDisabled ? ReservedBytesForJump : jump.InstSize, SeekOrigin.Current);
@ -325,13 +323,7 @@ namespace ARMeilleure.CodeGen.X86
} }
} }
buffer = new byte[_stream.Length - _stream.Position]; _stream.CopyTo(codeStream);
_stream.Read(buffer);
codeStream.Write(buffer);
_ptcInfo?.WriteCode(codeStream);
return codeStream.ToArray(); return codeStream.ToArray();
} }

View file

@ -190,6 +190,11 @@ namespace ARMeilleure.CodeGen.X86
byte[] code = context.GetCode(); byte[] code = context.GetCode();
if (ptcInfo != null)
{
ptcInfo.Code = code;
}
Logger.EndPass(PassName.CodeGeneration); Logger.EndPass(PassName.CodeGeneration);
return new CompiledFunction(code, unwindInfo); return new CompiledFunction(code, unwindInfo);

View file

@ -198,11 +198,7 @@ namespace ARMeilleure.Translation.PTC
using (FileStream compressedStream = new FileStream(fileName, FileMode.Open)) using (FileStream compressedStream = new FileStream(fileName, FileMode.Open))
using (DeflateStream deflateStream = new DeflateStream(compressedStream, CompressionMode.Decompress, true)) using (DeflateStream deflateStream = new DeflateStream(compressedStream, CompressionMode.Decompress, true))
{ {
IntPtr intPtr = IntPtr.Zero; using MD5 md5 = MD5.Create();
try
{
MD5 md5 = MD5.Create();
int hashSize = md5.HashSize / 8; int hashSize = md5.HashSize / 8;
@ -223,6 +219,10 @@ namespace ARMeilleure.Translation.PTC
int size = BitConverter.ToInt32(sizeBytes, 0); int size = BitConverter.ToInt32(sizeBytes, 0);
IntPtr intPtr = IntPtr.Zero;
try
{
intPtr = Marshal.AllocHGlobal(size); intPtr = Marshal.AllocHGlobal(size);
using (UnmanagedMemoryStream stream = new UnmanagedMemoryStream((byte*)intPtr.ToPointer(), size, size, FileAccess.ReadWrite)) using (UnmanagedMemoryStream stream = new UnmanagedMemoryStream((byte*)intPtr.ToPointer(), size, size, FileAccess.ReadWrite))
@ -245,8 +245,6 @@ namespace ARMeilleure.Translation.PTC
byte[] expectedHash = md5.ComputeHash(stream); byte[] expectedHash = md5.ComputeHash(stream);
md5.Dispose();
if (!CompareHash(currentHash, expectedHash)) if (!CompareHash(currentHash, expectedHash))
{ {
InvalidateCompressedStream(compressedStream); InvalidateCompressedStream(compressedStream);
@ -413,11 +411,7 @@ namespace ARMeilleure.Translation.PTC
{ {
int translatedFuncsCount; int translatedFuncsCount;
IntPtr intPtr = IntPtr.Zero; using MD5 md5 = MD5.Create();
try
{
MD5 md5 = MD5.Create();
int hashSize = md5.HashSize / 8; int hashSize = md5.HashSize / 8;
@ -427,6 +421,10 @@ namespace ARMeilleure.Translation.PTC
Debug.Assert(sizeBytes.Length == sizeof(int)); Debug.Assert(sizeBytes.Length == sizeof(int));
byte[] sizeHash = md5.ComputeHash(sizeBytes); byte[] sizeHash = md5.ComputeHash(sizeBytes);
IntPtr intPtr = IntPtr.Zero;
try
{
intPtr = Marshal.AllocHGlobal(size); intPtr = Marshal.AllocHGlobal(size);
using (UnmanagedMemoryStream stream = new UnmanagedMemoryStream((byte*)intPtr.ToPointer(), size, size, FileAccess.ReadWrite)) using (UnmanagedMemoryStream stream = new UnmanagedMemoryStream((byte*)intPtr.ToPointer(), size, size, FileAccess.ReadWrite))
@ -445,8 +443,6 @@ namespace ARMeilleure.Translation.PTC
stream.Seek((long)hashSize, SeekOrigin.Begin); stream.Seek((long)hashSize, SeekOrigin.Begin);
byte[] hash = md5.ComputeHash(stream); byte[] hash = md5.ComputeHash(stream);
md5.Dispose();
stream.Seek(0L, SeekOrigin.Begin); stream.Seek(0L, SeekOrigin.Begin);
stream.Write(hash, 0, hashSize); stream.Write(hash, 0, hashSize);
@ -860,11 +856,11 @@ namespace ARMeilleure.Translation.PTC
_infosWriter.Write((ulong)guestSize); // InfoEntry.GuestSize _infosWriter.Write((ulong)guestSize); // InfoEntry.GuestSize
_infosWriter.Write((bool)highCq); // InfoEntry.HighCq _infosWriter.Write((bool)highCq); // InfoEntry.HighCq
_infosWriter.Write((bool)false); // InfoEntry.Stubbed _infosWriter.Write((bool)false); // InfoEntry.Stubbed
_infosWriter.Write((int)ptcInfo.CodeStream.Length); // InfoEntry.CodeLen _infosWriter.Write((int)ptcInfo.Code.Length); // InfoEntry.CodeLen
_infosWriter.Write((int)ptcInfo.RelocEntriesCount); // InfoEntry.RelocEntriesCount _infosWriter.Write((int)ptcInfo.RelocEntriesCount); // InfoEntry.RelocEntriesCount
// WriteCode. // WriteCode.
ptcInfo.CodeStream.WriteTo(_codesStream); _codesStream.Write(ptcInfo.Code, 0, ptcInfo.Code.Length);
// WriteReloc. // WriteReloc.
ptcInfo.RelocStream.WriteTo(_relocsStream); ptcInfo.RelocStream.WriteTo(_relocsStream);

View file

@ -9,7 +9,8 @@ namespace ARMeilleure.Translation.PTC
private readonly BinaryWriter _relocWriter; private readonly BinaryWriter _relocWriter;
private readonly BinaryWriter _unwindInfoWriter; private readonly BinaryWriter _unwindInfoWriter;
public MemoryStream CodeStream { get; } public byte[] Code { get; set; }
public MemoryStream RelocStream { get; } public MemoryStream RelocStream { get; }
public MemoryStream UnwindInfoStream { get; } public MemoryStream UnwindInfoStream { get; }
@ -17,7 +18,6 @@ namespace ARMeilleure.Translation.PTC
public PtcInfo() public PtcInfo()
{ {
CodeStream = new MemoryStream();
RelocStream = new MemoryStream(); RelocStream = new MemoryStream();
UnwindInfoStream = new MemoryStream(); UnwindInfoStream = new MemoryStream();
@ -27,11 +27,6 @@ namespace ARMeilleure.Translation.PTC
RelocEntriesCount = 0; RelocEntriesCount = 0;
} }
public void WriteCode(MemoryStream codeStream)
{
codeStream.WriteTo(CodeStream);
}
public void WriteRelocEntry(RelocEntry relocEntry) public void WriteRelocEntry(RelocEntry relocEntry)
{ {
_relocWriter.Write((int)relocEntry.Position); _relocWriter.Write((int)relocEntry.Position);
@ -60,7 +55,6 @@ namespace ARMeilleure.Translation.PTC
_relocWriter.Dispose(); _relocWriter.Dispose();
_unwindInfoWriter.Dispose(); _unwindInfoWriter.Dispose();
CodeStream.Dispose();
RelocStream.Dispose(); RelocStream.Dispose();
UnwindInfoStream.Dispose(); UnwindInfoStream.Dispose();
} }

View file

@ -246,8 +246,10 @@ namespace ARMeilleure.Translation
ResetPool(highCq ? 1 : 0); ResetPool(highCq ? 1 : 0);
} }
else using (PtcInfo ptcInfo = new PtcInfo()) else
{ {
using PtcInfo ptcInfo = new PtcInfo();
func = Compiler.Compile<GuestFunction>(cfg, argTypes, OperandType.I64, options, ptcInfo); func = Compiler.Compile<GuestFunction>(cfg, argTypes, OperandType.I64, options, ptcInfo);
ResetPool(highCq ? 1 : 0); ResetPool(highCq ? 1 : 0);