Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Avoid GC allocation when building the search path #3391

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions src/core/sys/windows/stacktrace.d
Original file line number Diff line number Diff line change
Expand Up @@ -345,26 +345,38 @@ extern(Windows) BOOL FixupDebugHeader(HANDLE hProcess, ULONG ActionCode,
return FALSE;
}

private string generateSearchPath()
private char[] generateSearchPathAlloc()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the point is to avoid GC allocation, please mark is @nogc so it is obvious.

{
__gshared string[3] defaultPathList = ["_NT_SYMBOL_PATH",
"_NT_ALTERNATE_SYMBOL_PATH",
"SYSTEMROOT"];
__gshared string[3] defaultPathList = [
"_NT_SYMBOL_PATH", "_NT_ALTERNATE_SYMBOL_PATH", "SYSTEMROOT"
];

string path;
char[2048] temp = void;
char[MAX_PATH] temp = void;
DWORD len;
DWORD total;
foreach (e; defaultPathList)
{
if ((len = GetEnvironmentVariableA(e.ptr, temp.ptr, temp.length)) > 0)
{
total += len + 1;
}
}

foreach ( e; defaultPathList )
if(total > 0)
{
if ( (len = GetEnvironmentVariableA( e.ptr, temp.ptr, temp.length )) > 0 )
auto buffer = cast(char[]) malloc(total + 1)[0 .. total + 1];
foreach (e; defaultPathList)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is wasteful to iterate defaultPathList twice. You can integrate this loop in the one above.

{
path ~= temp[0 .. len];
path ~= ";";
if ((len = GetEnvironmentVariableA(e.ptr, &buffer[len], buffer.length - len)) > 0)
{
buffer[len] = ';';
len += 1;
}
}
buffer[$-1] = '\0';
return buffer;
}
path ~= "\0";
return path;
return [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider return null

}


Expand Down Expand Up @@ -400,9 +412,12 @@ shared static this()
symOptions |= SYMOPT_DEFERRED_LOAD;
symOptions = dbghelp.SymSetOptions( symOptions );

debug(PRINTF) printf("Search paths: %s\n", generateSearchPath().ptr);
auto paths = generateSearchPathAlloc(buffer);
scope(exit) if(paths.length > 0) free(paths.ptr);

debug(PRINTF) printf("Search paths: %s\n", paths.ptr);

if (!dbghelp.SymInitialize(hProcess, generateSearchPath().ptr, TRUE))
if (!dbghelp.SymInitialize(hProcess, paths.ptr, TRUE))
return;

dbghelp.SymRegisterCallback64(hProcess, &FixupDebugHeader, 0);
Expand Down