Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use Stream.CopyTo{Async} in NonCryptographicHashAlgorithm.Append{Async}#103669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
stephentoub merged 1 commit intodotnet:mainfromstephentoub:hashcopyto
Jun 19, 2024

Conversation

stephentoub
Copy link
Member

@stephentoubstephentoub commentedJun 18, 2024
edited
Loading

Rather than using a manual loop to copy data from a stream to a buffer, use theStream.CopyTo{Async} method. Downside is there's an extra 32 byte allocation for the temporary destination passthrough Stream object. Upside is it allows the source Stream's to optimize via its CopyTo{Async} override, e.g. MemoryStream.CopyTo just makes a single Write call with its buffer. The base Stream.CopyTo also uses a larger rented buffer size than these APIs were using.

Related to#103539

Before:

MethodKindMeanAllocated
HashMemoryStream79.76 us568 B
HashFileStream837.08 us568 B

After:

MethodKindMeanAllocated
HashMemoryStream59.76 us600 B
HashFileStream131.51 us600 B
usingBenchmarkDotNet.Attributes;usingBenchmarkDotNet.Running;usingSystem.IO.Hashing;usingSystem.Security.Cryptography;BenchmarkSwitcher.FromAssembly(typeof(Tests).Assembly).Run(args);[MemoryDiagnoser][HideColumns("Job","Error","StdDev","Median","RatioSD")]publicclassTests{privateStream_stream;privatebyte[]_bytes=RandomNumberGenerator.GetBytes(1024*1024);[Params("MemoryStream","FileStream")]publicstringKind{get;set;}[GlobalSetup]publicvoidSetup(){switch(Kind){case"MemoryStream":_stream=newMemoryStream(_bytes);break;case"FileStream":stringpath=Path.GetRandomFileName();File.WriteAllBytes(path,_bytes);_stream=newFileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read,0,FileOptions.DeleteOnClose);break;}}[GlobalCleanup]publicvoidCleanup()=>_stream.Dispose();[Benchmark]publiculongHash(){_stream.Position=0;varhash=newXxHash3();hash.Append(_stream);returnhash.GetCurrentHashAsUInt64();}}

@stephentoubstephentoub merged commitf8e575c intodotnet:mainJun 19, 2024
80 of 83 checks passed
@stephentoubstephentoub deleted the hashcopyto branchJune 19, 2024 23:46
@dotnet-policy-serviceDotnet Policy Service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-io-hashing,@bartonjs,@vcsjones
See info inarea-owners.md if you want to be subscribed.

@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsJul 20, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@bartonjsbartonjsbartonjs approved these changes

Assignees

@stephentoubstephentoub

Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@stephentoub@bartonjs@adamsitnik

[8]ページ先頭

©2009-2025 Movatter.jp