-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for dehydrated runtime data structures #77884
Changes from all commits
204d46a
3d9c1fd
f1e2b3d
2a8111a
7b63510
24eb4a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using Debug = System.Diagnostics.Debug; | ||
|
||
namespace Internal.Runtime | ||
{ | ||
/// <summary> | ||
/// Provides functionality to encode/decode dehydrated data instruction stream. | ||
/// </summary> | ||
/// <remarks> | ||
/// The instructions use a variable length encoding and are split in two parts: | ||
/// the instruction command kind and command data (payload). | ||
/// The payload is an integer. If the instruction kind and payload can fit into a single | ||
/// byte, the encoding is one byte. Bigger payloads produce bigger instructions. | ||
/// </remarks> | ||
internal static class DehydratedDataCommand | ||
{ | ||
public const byte Copy = 0x00; | ||
public const byte ZeroFill = 0x01; | ||
public const byte RelPtr32Reloc = 0x02; | ||
public const byte PtrReloc = 0x03; | ||
|
||
private const byte DehydratedDataCommandMask = 0x03; | ||
private const int DehydratedDataCommandPayloadShift = 2; | ||
|
||
private const int MaxRawShortPayload = (1 << (8 - DehydratedDataCommandPayloadShift)) - 1; | ||
private const int MaxExtraPayloadBytes = 3; | ||
private const int MaxShortPayload = MaxRawShortPayload - MaxExtraPayloadBytes; | ||
|
||
public static byte EncodeShort(int command, int commandData) | ||
{ | ||
Debug.Assert((command & DehydratedDataCommandMask) == command); | ||
Debug.Assert(commandData <= MaxShortPayload); | ||
return (byte)(command | (commandData << DehydratedDataCommandPayloadShift)); | ||
} | ||
|
||
public static int Encode(int command, int commandData, byte[] buffer) | ||
{ | ||
Debug.Assert((command & DehydratedDataCommandMask) == command); | ||
int remainingData = commandData - MaxShortPayload; | ||
if (remainingData <= 0) | ||
{ | ||
buffer[0] = EncodeShort(command, commandData); | ||
return 1; | ||
} | ||
|
||
int numExtraBytes = 0; | ||
for (; remainingData != 0; remainingData >>= 8) | ||
buffer[++numExtraBytes] = (byte)remainingData; | ||
if (numExtraBytes > MaxExtraPayloadBytes) | ||
throw new InvalidOperationException(); // decoder can only decode this many extra bytes | ||
|
||
buffer[0] = (byte)(command | ((MaxShortPayload + numExtraBytes) << DehydratedDataCommandPayloadShift)); | ||
return 1 + numExtraBytes; | ||
} | ||
|
||
public static unsafe byte* Decode(byte* pB, out int command, out int payload) | ||
{ | ||
byte b = *pB; | ||
command = b & DehydratedDataCommandMask; | ||
payload = b >> DehydratedDataCommandPayloadShift; | ||
int extraBytes = payload - MaxShortPayload; | ||
if (extraBytes > 0) | ||
{ | ||
payload = *++pB; | ||
if (extraBytes > 1) | ||
{ | ||
payload += *++pB << 8; | ||
if (extraBytes > 2) | ||
payload += *++pB << 16; | ||
} | ||
|
||
payload += MaxShortPayload; | ||
} | ||
|
||
return pB + 1; | ||
} | ||
|
||
#if false | ||
static void Main() | ||
{ | ||
int command, payload; | ||
|
||
byte[] buf = new byte[5]; | ||
Debug.Assert(Encode(1, 0, buf) == 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is under I'm still debating whether to check it in, delete it, or spend more time converting it to a unit test that is never going to fail because nobody will want to touch this anyway. |
||
Debug.Assert(buf[0] == 1); | ||
Debug.Assert(D(buf, out command, out payload) == 1 && command == 1 && payload == 0); | ||
Debug.Assert(Encode(1, 1, buf) == 1); | ||
Debug.Assert(buf[0] == (1 | (1 << DehydratedDataCommandPayloadShift))); | ||
Debug.Assert(D(buf, out command, out payload) == 1 && command == 1 && payload == 1); | ||
Debug.Assert(Encode(1, 60, buf) == 1); | ||
Debug.Assert(buf[0] == (1 | (60 << DehydratedDataCommandPayloadShift))); | ||
Debug.Assert(D(buf, out command, out payload) == 1 && command == 1 && payload == 60); | ||
Debug.Assert(Encode(1, 61, buf) == 2); | ||
Debug.Assert(buf[0] == (1 | ((MaxShortPayload + 1) << DehydratedDataCommandPayloadShift))); | ||
Debug.Assert(buf[1] == 1); | ||
Debug.Assert(D(buf, out command, out payload) == 2 && command == 1 && payload == 61); | ||
|
||
Debug.Assert(Encode(3, 256, buf) == 2); | ||
Debug.Assert(D(buf, out command, out payload) == 2 && command == 3 && payload == 256); | ||
Debug.Assert(Encode(3, 6500, buf) == 3); | ||
Debug.Assert(D(buf, out command, out payload) == 3 && command == 3 && payload == 6500); | ||
Debug.Assert(Encode(3, 65000, buf) == 3); | ||
Debug.Assert(D(buf, out command, out payload) == 3 && command == 3 && payload == 65000); | ||
Debug.Assert(Encode(3, 100000, buf) == 4); | ||
Debug.Assert(D(buf, out command, out payload) == 4 && command == 3 && payload == 100000); | ||
|
||
static unsafe int D(byte[] bytes, out int command, out int payload) | ||
{ | ||
fixed (byte* pBytes = bytes) | ||
return (int)(Decode(pBytes, out command, out payload) - pBytes); | ||
} | ||
} | ||
#endif | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace ILCompiler.DependencyAnalysis | ||
{ | ||
public abstract class DehydratableObjectNode : ObjectNode | ||
{ | ||
public sealed override ObjectNodeSection GetSection(NodeFactory factory) | ||
{ | ||
return factory.MetadataManager.IsDataDehydrated ? ObjectNodeSection.HydrationTargetSection : GetDehydratedSection(factory); | ||
} | ||
|
||
public sealed override ObjectData GetData(NodeFactory factory, bool relocsOnly = false) | ||
{ | ||
ObjectData result = GetDehydratableData(factory, relocsOnly); | ||
|
||
// If we're not generating full data yet, or dehydration is not active, | ||
// return the ObjectData as is. | ||
if (relocsOnly || !factory.MetadataManager.IsDataDehydrated) | ||
return result; | ||
|
||
// Otherwise return the dehydrated data | ||
return factory.MetadataManager.PrepareForDehydration(this, result); | ||
} | ||
|
||
protected abstract ObjectNodeSection GetDehydratedSection(NodeFactory factory); | ||
protected abstract ObjectData GetDehydratableData(NodeFactory factory, bool relocsOnly = false); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Buffer.MemoryCopy
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't call that from Test.CoreLib. I'm also a little bit worried - this code runs during very early startup - casting, typeof,
newobj
and many other things are not available. Buffer.MemoryCopy feels a bit higher up the stack that I'm not sure it would be safe to call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this runs very early. I am not sure if Buffer.MemoryCopy does anything complex, but since the data here is unmanaged, it would eventually just call something like
InternalCalls.memmove
.Are these copied chunks long enough to involve memmove? If they are typically just 10-20 bytes, it may not get any faster.