Add-Type uses File.ReadAllText() API in a loop for reading in multiple source files. This can be non-performant for multiple reasons:
a. ReadAllText() uses a small buffer that increases IO calls.
b. It performs StringBuilder.ToString() internally which allocates a string that is not used, making possible LOH allocation and adding GC pressure.
Consider using a different pattern:
StringBuilder sourceCode = new StringBuilder();
foreach (string file in paths)
{
foreach (string line in File.ReadAllLines(file))
{
sourceCode.AppendLine(line);
}
}
String result = sourceCode.ToString();
Add-Type uses File.ReadAllText() API in a loop for reading in multiple source files. This can be non-performant for multiple reasons:
a. ReadAllText() uses a small buffer that increases IO calls.
b. It performs StringBuilder.ToString() internally which allocates a string that is not used, making possible LOH allocation and adding GC pressure.
Consider using a different pattern: