question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add-Type ReadAllText perf improvement

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lzybkrcommented, Nov 4, 2017

I don’t think we’ve typically viewed the public api surface of a cmdlet implementation as a real C# api, so I’m fine with changes like this.

There might be a rare exception, and if we find out we made a mistake, it’s generally much easier to fix that these days, as compared to waiting multiple years between releases.

0reactions
SteveL-MSFTcommented, Jan 3, 2018

@PowerShell/powershell-committee reviewed this and is ok with the changes already made to remove AddTypeCommandBase class

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading from file not fast enough, how would I speed it up?
ReadAllText was a very good solution for me. I used following code for 3.000.000 row text file and it took 4-5 seconds to...
Read more >
Thread: VB.NET: Read large text file fast, without lag
Hello. I am having some troubles reading a text file which is quite large: about 500 MB. I have done it like that:...
Read more >
File.ReadAllText Method (System.IO)
Opens a text file, reads all the text in the file into a string, and then closes the file.
Read more >
LightNode/README.md at master
for improvement LightNode debugging --> <add type="Glimpse.Core.Policy.StatusCodePolicy, Glimpse. ... Performance source code is in LightNode/Performance.
Read more >
C#: How to read a file? | Fastest way to read a file in C
The ReadAllText method reads the contents of a file and returns it ... to reduce the memory usage and improve the performance of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found