Add-Type ReadAllText perf improvement
See original GitHub issueAdd-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:
- Created 6 years ago
- Comments:9 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
@PowerShell/powershell-committee reviewed this and is ok with the changes already made to remove AddTypeCommandBase class