Skip to content

Commit

Permalink
Avoid copying method descriptions twice
Browse files Browse the repository at this point in the history
  • Loading branch information
verdie-g committed Jun 30, 2024
1 parent 17997b0 commit 4ba1149
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions EventPipe/StackResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,8 @@ public void ResolveEventStackTraces(List<Event> events)
}
}

ulong[] allMethodAddresses = new ulong[_methodDescriptions.Count];
Dictionary<ulong, MethodDescription> allMethodDescriptions = new(_methodDescriptions.Count);
for (int i = 0; i < _methodDescriptions.Count; i += 1)
{
var methodDescription = _methodDescriptions[i];
allMethodAddresses[i] = methodDescription.Address;
allMethodDescriptions[methodDescription.Address] = methodDescription;
}

Array.Sort(allMethodAddresses);
var methodDescriptions = CollectionsMarshal.AsSpan(_methodDescriptions);
methodDescriptions.Sort((x, y) => (int)(x.Address - y.Address));

foreach (var evt in events)
{
Expand All @@ -79,8 +71,7 @@ public void ResolveEventStackTraces(List<Event> events)
continue;
}

group.StackTrace ??= ResolveStackTrace(evt.StackIndex, group.Addresses,
allMethodAddresses, allMethodDescriptions);
group.StackTrace ??= ResolveStackTrace(evt.StackIndex, group.Addresses, methodDescriptions);

evt.StackIndex = group.StackTrace.Index;
evt.StackTrace = group.StackTrace;
Expand All @@ -90,8 +81,7 @@ public void ResolveEventStackTraces(List<Event> events)
private static StackTrace ResolveStackTrace(
int stackIndex,
ulong[] addresses,
ulong[] allMethodAddresses,
Dictionary<ulong, MethodDescription> allMethodDescriptions)
ReadOnlySpan<MethodDescription> methodDescriptions)
{
if (addresses.Length == 0)
{
Expand All @@ -101,19 +91,18 @@ private static StackTrace ResolveStackTrace(
var stackTrace = new MethodDescription[addresses.Length];
for (int i = 0; i < stackTrace.Length; i += 1)
{
stackTrace[i] = ResolveSymbol(addresses[i], allMethodAddresses, allMethodDescriptions);
stackTrace[i] = ResolveSymbol(methodDescriptions, addresses[i]);

}

return new StackTrace(stackIndex, stackTrace);
}

private static MethodDescription ResolveSymbol(
ulong address,
ulong[] allMethodAddresses,
Dictionary<ulong, MethodDescription> allMethodDescriptions)
ReadOnlySpan<MethodDescription> methodDescriptions,
ulong address)
{
int methodAddressIdx = Array.BinarySearch(allMethodAddresses, address);
int methodAddressIdx = BinarySearchMethodAddress(methodDescriptions, address);
if (methodAddressIdx < 0)
{
methodAddressIdx = ~methodAddressIdx - 1;
Expand All @@ -123,9 +112,7 @@ private static MethodDescription ResolveSymbol(
}
}

var methodAddress = allMethodAddresses[methodAddressIdx];
var methodDescription = allMethodDescriptions[methodAddress];

var methodDescription = methodDescriptions[methodAddressIdx];
if (methodDescription.Address + methodDescription.Size < address)
{
return UnresolvedMethodDescription;
Expand All @@ -134,6 +121,32 @@ private static MethodDescription ResolveSymbol(
return methodDescription;
}

private static int BinarySearchMethodAddress(ReadOnlySpan<MethodDescription> methodDescriptions, ulong address)
{
int lo = 0;
int hi = methodDescriptions.Length - 1;
while (lo <= hi)
{
int i = lo + (hi - lo) / 2;

if (methodDescriptions[i].Address == address)
{
return i;
}

if (methodDescriptions[i].Address < address)
{
lo = i + 1;
}
else
{
hi = i - 1;
}
}

return ~lo;
}

private sealed class StackTraceGroup
{
public required List<int> StackIndexes { get; init; }
Expand Down

0 comments on commit 4ba1149

Please sign in to comment.