Skip to content

Commit

Permalink
Move a few stackallocs out of loops (dotnet#34149)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Mar 27, 2020
1 parent 0c9094e commit 1d9067b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -467,42 +467,59 @@ private unsafe void GetSessionInfo(SessionInfoCallback action, ref List<SessionI
// does not have this issue.
#if (TARGET_WINDOWS && (ES_SESSION_INFO || !ES_BUILD_STANDALONE))
int buffSize = 256; // An initial guess that probably works most of the time.
byte* buffer;
while (true)
byte* stackSpace = stackalloc byte[buffSize];
byte* buffer = stackSpace;
try
{
byte* space = stackalloc byte[buffSize];
buffer = space;
int hr = 0;
while (true)
{
int hr = 0;

fixed (Guid* provider = &m_providerId)
fixed (Guid* provider = &m_providerId)
{
hr = Interop.Advapi32.EnumerateTraceGuidsEx(Interop.Advapi32.TRACE_QUERY_INFO_CLASS.TraceGuidQueryInfo,
provider, sizeof(Guid), buffer, buffSize, out buffSize);
}
if (hr == 0)
break;
if (hr != Interop.Errors.ERROR_INSUFFICIENT_BUFFER)
return;

if (buffer != stackSpace)
{
byte* toFree = buffer;
buffer = null;
Marshal.FreeHGlobal((IntPtr)toFree);
}
buffer = (byte*)Marshal.AllocHGlobal(buffSize);
}

var providerInfos = (Interop.Advapi32.TRACE_GUID_INFO*)buffer;
var providerInstance = (Interop.Advapi32.TRACE_PROVIDER_INSTANCE_INFO*)&providerInfos[1];
int processId = unchecked((int)Interop.Kernel32.GetCurrentProcessId());
// iterate over the instances of the EventProvider in all processes
for (int i = 0; i < providerInfos->InstanceCount; i++)
{
hr = Interop.Advapi32.EnumerateTraceGuidsEx(Interop.Advapi32.TRACE_QUERY_INFO_CLASS.TraceGuidQueryInfo,
provider, sizeof(Guid), buffer, buffSize, out buffSize);
if (providerInstance->Pid == processId)
{
var enabledInfos = (Interop.Advapi32.TRACE_ENABLE_INFO*)&providerInstance[1];
// iterate over the list of active ETW sessions "listening" to the current provider
for (int j = 0; j < providerInstance->EnableCount; j++)
action(enabledInfos[j].LoggerId, enabledInfos[j].MatchAllKeyword, ref sessionList);
}
if (providerInstance->NextOffset == 0)
break;
Debug.Assert(0 <= providerInstance->NextOffset && providerInstance->NextOffset < buffSize);
byte* structBase = (byte*)providerInstance;
providerInstance = (Interop.Advapi32.TRACE_PROVIDER_INSTANCE_INFO*)&structBase[providerInstance->NextOffset];
}
if (hr == 0)
break;
if (hr != Interop.Errors.ERROR_INSUFFICIENT_BUFFER)
return;
}

var providerInfos = (Interop.Advapi32.TRACE_GUID_INFO*)buffer;
var providerInstance = (Interop.Advapi32.TRACE_PROVIDER_INSTANCE_INFO*)&providerInfos[1];
int processId = unchecked((int)Interop.Kernel32.GetCurrentProcessId());
// iterate over the instances of the EventProvider in all processes
for (int i = 0; i < providerInfos->InstanceCount; i++)
finally
{
if (providerInstance->Pid == processId)
if (buffer != null && buffer != stackSpace)
{
var enabledInfos = (Interop.Advapi32.TRACE_ENABLE_INFO*)&providerInstance[1];
// iterate over the list of active ETW sessions "listening" to the current provider
for (int j = 0; j < providerInstance->EnableCount; j++)
action(enabledInfos[j].LoggerId, enabledInfos[j].MatchAllKeyword, ref sessionList);
Marshal.FreeHGlobal((IntPtr)buffer);
}
if (providerInstance->NextOffset == 0)
break;
Debug.Assert(0 <= providerInstance->NextOffset && providerInstance->NextOffset < buffSize);
byte* structBase = (byte*)providerInstance;
providerInstance = (Interop.Advapi32.TRACE_PROVIDER_INSTANCE_INFO*)&structBase[providerInstance->NextOffset];
}
#else
#if !ES_BUILD_PCL && TARGET_WINDOWS // TODO command arguments don't work on PCL builds...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ private bool TryGetNamedPropertyValue(
out JsonElement value)
{
ReadOnlySpan<byte> documentSpan = _utf8Json.Span;
Span<byte> utf8UnescapedStack = stackalloc byte[JsonConstants.StackallocThreshold];

// Move to the row before the EndObject
int index = endIndex - DbRow.Size;
Expand Down Expand Up @@ -184,8 +185,8 @@ private bool TryGetNamedPropertyValue(

try
{
Span<byte> utf8Unescaped = remaining <= JsonConstants.StackallocThreshold ?
stackalloc byte[remaining] :
Span<byte> utf8Unescaped = remaining <= utf8UnescapedStack.Length ?
utf8UnescapedStack :
(rented = ArrayPool<byte>.Shared.Rent(remaining));

// Only unescape the part we haven't processed.
Expand Down

0 comments on commit 1d9067b

Please sign in to comment.