Skip to content

Add-Type ReadAllText perf improvement #5158

@PaulHigin

Description

@PaulHigin

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();

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedPS-Committee has reviewed this and made a decisionResolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Engine-Performancecore PowerShell engine, interpreter, and runtime performance

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions