Skip to content

Simplify File.WriteAllTextAsync - #55644

Merged
stephentoub merged 1 commit into
dotnet:mainfrom
stephentoub:writealltextasync
Jul 15, 2021
Merged

Simplify File.WriteAllTextAsync#55644
stephentoub merged 1 commit into
dotnet:mainfrom
stephentoub:writealltextasync

Conversation

@stephentoub

Copy link
Copy Markdown
Member

Avoid the unnecessary ArrayPool usage and extra layer of buffering. The code ends up being much simpler, and also a bit faster.

MethodToolchainMeanRatioAllocated
WriteAllTextAsync\main\CoreRun.exe1.560 ms1.0022 KB
WriteAllTextAsync\pr\CoreRun.exe1.486 ms0.9719 KB
usingSystem.IO;usingSystem.Linq;usingSystem.Threading;usingSystem.Threading.Tasks;usingBenchmarkDotNet.Attributes;usingBenchmarkDotNet.Running;[MemoryDiagnoser]publicclassProgram{publicstaticvoidMain(string[]args)=>BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);privatestaticstrings_contents=string.Concat(Enumerable.Range(0,100_000).Select(i =>(char)('a'+(i%26))));privatestaticstrings_path=Path.GetRandomFileName();privatestaticCancellationTokenSources_cts=new();[Benchmark]publicTaskWriteAllTextAsync()=>File.WriteAllTextAsync(s_path,s_contents,s_cts.Token);}

Avoid the unnecessary ArrayPool usage and extra layer of buffering.
@ghost

Copy link
Copy Markdown

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@stephentoubstephentoub added this to the 6.0.0 milestone Jul 14, 2021
@ghost

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details

Avoid the unnecessary ArrayPool usage and extra layer of buffering. The code ends up being much simpler, and also a bit faster.

MethodToolchainMeanRatioAllocated
WriteAllTextAsync\main\CoreRun.exe1.560 ms1.0022 KB
WriteAllTextAsync\pr\CoreRun.exe1.486 ms0.9719 KB
usingSystem.IO;usingSystem.Linq;usingSystem.Threading;usingSystem.Threading.Tasks;usingBenchmarkDotNet.Attributes;usingBenchmarkDotNet.Running;[MemoryDiagnoser]publicclassProgram{publicstaticvoidMain(string[]args)=>BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);privatestaticstrings_contents=string.Concat(Enumerable.Range(0,100_000).Select(i =>(char)('a'+(i%26))));privatestaticstrings_path=Path.GetRandomFileName();privatestaticCancellationTokenSources_cts=new();[Benchmark]publicTaskWriteAllTextAsync()=>File.WriteAllTextAsync(s_path,s_contents,s_cts.Token);}
Author:stephentoub
Assignees:-
Labels:

area-System.IO

Milestone:-

@stephentoub
stephentoub merged commit 9717cf5 into dotnet:mainJul 15, 2021
@stephentoub
stephentoub deleted the writealltextasync branch July 15, 2021 02:41
@ghostghost locked as resolved and limited conversation to collaborators Aug 14, 2021
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@stephentoub