Skip to content

System.Diagnostics.StackTrace slow on .NET Core #10209

@martinmine

Description

@martinmine

Consider the following program:

using System;
using System.Diagnostics;

static void Main()
{
    var start = DateTime.Now;

    for (var i = 0; i < 5000; i++)
    {
        new StackTrace(0, true);
    }

    var timeSpent = DateTime.Now - start;
    Console.WriteLine($"Spent time: {timeSpent.TotalMilliseconds} ms");
    Console.ReadKey();
}

This takes 4133.3539 ms in .NET Core 2.0, while targeting .NET 4.7.1 this takes 108.1025 ms to execute. Making a full dump on this with procdump, I can see the following stack trace typically being called:

System.Private.CoreLib.dll!00007ffee4a6bfb9()	Unknown
[Managed to Native Transition]	
System.Private.CoreLib.dll!System.IO.FileStream.SeekCore(long offset, System.IO.SeekOrigin origin) Line 608	C#
System.Private.CoreLib.dll!System.IO.FileStream.Seek(long offset, System.IO.SeekOrigin origin) Line 542	C#
System.Reflection.Metadata.dll!System.Reflection.PortableExecutable.PEHeaders.PEHeaders(System.IO.Stream peStream, int size, bool isLoadedImage)	Unknown
System.Reflection.Metadata.dll!System.Reflection.PortableExecutable.PEReader.InitializePEHeaders()	Unknown
System.Reflection.Metadata.dll!System.Reflection.PortableExecutable.PEReader.ReadDebugDirectory()	Unknown
System.Reflection.Metadata.dll!System.Reflection.PortableExecutable.PEReader.TryOpenAssociatedPortablePdb(string peImagePath, System.Func<string, System.IO.Stream> pdbFileStreamProvider, out System.Reflection.Metadata.MetadataReaderProvider pdbReaderProvider, out string pdbPath)	Unknown
System.Diagnostics.StackTrace.dll!System.Diagnostics.StackTraceSymbols.TryOpenReaderFromAssemblyFile(string assemblyPath, System.IntPtr loadedPeAddress, int loadedPeSize) Line 198	C#
System.Diagnostics.StackTrace.dll!System.Diagnostics.StackTraceSymbols.TryGetReader(string assemblyPath, System.IntPtr loadedPeAddress, int loadedPeSize, System.IntPtr inMemoryPdbAddress, int inMemoryPdbSize) Line 129	C#
System.Diagnostics.StackTrace.dll!System.Diagnostics.StackTraceSymbols.GetSourceLineInfo(string assemblyPath, System.IntPtr loadedPeAddress, int loadedPeSize, System.IntPtr inMemoryPdbAddress, int inMemoryPdbSize, int methodToken, int ilOffset, out string sourceFile, out int sourceLine, out int sourceColumn) Line 59	C#
System.Private.CoreLib.dll!System.Diagnostics.StackFrameHelper.InitializeSourceInfo(int iSkip, bool fNeedFileInfo, System.Exception exception) Line 140	C#
System.Private.CoreLib.dll!System.Diagnostics.StackTrace.CaptureStackTrace(int iSkip, bool fNeedFileInfo, System.Threading.Thread targetThread, System.Exception e) Line 426	C#
System.Private.CoreLib.dll!System.Diagnostics.StackTrace.StackTrace(int skipFrames, bool fNeedFileInfo) Line 307	C#

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions