Add span-based CopyTo and CopyToAsync methods #27639
Conversation
Thanks for working on this. A large part of the value of such APIs is derived types providing a better implementation. Can you please do so in this PR for the Streams in CoreLib, e.g.MemoryStream? |
ps MemoryStream was one example. Others include NullStream, FileStream, UnmanagedMemoryStream, and probably another one or more I'm forgetting. Thanks. |
To avoid code duplication
Stream API is changed
src/System.Private.CoreLib/shared/System/IO/StreamHelpers.CopyValidation.cs
Outdated
Show resolved
Hide resolved
src/System.Private.CoreLib/shared/System/IO/StreamHelpers.CopyValidation.cs
Show resolved
Hide resolved
Thanks for continuing to work on this, @manandre. Still some open issues that looks like may have been missed:
Thanks. |
Can we safely rely on
Let's discuss it:
|
Thank you for your contribution. As announced in dotnet/coreclr#27549 this repository will be moving to dotnet/runtime on November 13. If you would like to continue working on this PR after this date, the easiest way to move the change to dotnet/runtime is:
|
Yes. What validation an override chooses to perform is up to that override, based on the logic the override is going to use in its implementation. When this overload delegates to it, it's up to the override to provide the validation needed for the arguments provided to it, and we just need to validate the others.
Yes.
For the sync overload, it can just create a span from its memory and call the action with that.
Don't worry about these; they're very specialized streams and it's unlikely this method is ever going to be used on them, and if it they happen to be, the overhead of using the base shouldn't be impactful.
There's a lot of work happening in FileStream's existing CopyToAsync. Probably not worth overriding. |
@stephentoub Can you please do a final review in order to give us a chance to get this pull request merged before the migration to dotnet/runtime? 🤞 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Several remaining comments to be addressed. Otherwise, LGTM. Thanks.
src/System.Private.CoreLib/shared/System/IO/UnmanagedMemoryStream.cs
Outdated
Show resolved
Hide resolved
src/System.Private.CoreLib/shared/System/IO/UnmanagedMemoryStream.cs
Outdated
Show resolved
Hide resolved
Co-Authored-By: Stephen Toub <stoub@microsoft.com>
Co-Authored-By: Stephen Toub <stoub@microsoft.com>
/azp run coreclr-ci |
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
/azp run coreclr-ci |
Commenter does not have sufficient privileges for PR 27639 in repo dotnet/coreclr |
@stephentoub Help 🆘 |
Failures were unrelated. Thanks! |
* Add span-based CopyTo and CopyToAsync methods * Update according to feedback * Add span-based CopyTo overrides for MemoryStream * Improve span-based CopyTo arguments validation To avoid code duplication * Update according to second review Stream API is changed * Resolve InternalReadSpan/Memory inlining * Refactor ValidateCopyToArgs * Update according to third review * Update after fourth review * Override span CopyTo for UnmanagedMemoryStream * Apply suggestions from code review Co-Authored-By: Stephen Toub <stoub@microsoft.com> * Update after fifth review * Add cross sync/async support for span-based CopyTo * Call sync action directly in async context * Rework cross sync/async support for span-based CopyTo Co-Authored-By: Stephen Toub <stoub@microsoft.com> Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
* Add span-based CopyTo and CopyToAsync methods * Update according to feedback * Add span-based CopyTo overrides for MemoryStream * Improve span-based CopyTo arguments validation To avoid code duplication * Update according to second review Stream API is changed * Resolve InternalReadSpan/Memory inlining * Refactor ValidateCopyToArgs * Update according to third review * Update after fourth review * Override span CopyTo for UnmanagedMemoryStream * Apply suggestions from code review Co-Authored-By: Stephen Toub <stoub@microsoft.com> * Update after fifth review * Add cross sync/async support for span-based CopyTo * Call sync action directly in async context * Rework cross sync/async support for span-based CopyTo Co-Authored-By: Stephen Toub <stoub@microsoft.com> Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
* Add span-based CopyTo and CopyToAsync methods * Update according to feedback * Add span-based CopyTo overrides for MemoryStream * Improve span-based CopyTo arguments validation To avoid code duplication * Update according to second review Stream API is changed * Resolve InternalReadSpan/Memory inlining * Refactor ValidateCopyToArgs * Update according to third review * Update after fourth review * Override span CopyTo for UnmanagedMemoryStream * Apply suggestions from code review Co-Authored-By: Stephen Toub <stoub@microsoft.com> * Update after fifth review * Add cross sync/async support for span-based CopyTo * Call sync action directly in async context * Rework cross sync/async support for span-based CopyTo Co-Authored-By: Stephen Toub <stoub@microsoft.com> Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
* Add span-based CopyTo and CopyToAsync methods * Update according to feedback * Add span-based CopyTo overrides for MemoryStream * Improve span-based CopyTo arguments validation To avoid code duplication * Update according to second review Stream API is changed * Resolve InternalReadSpan/Memory inlining * Refactor ValidateCopyToArgs * Update according to third review * Update after fourth review * Override span CopyTo for UnmanagedMemoryStream * Apply suggestions from code review Co-Authored-By: Stephen Toub <stoub@microsoft.com> * Update after fifth review * Add cross sync/async support for span-based CopyTo * Call sync action directly in async context * Rework cross sync/async support for span-based CopyTo Co-Authored-By: Stephen Toub <stoub@microsoft.com> Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
* Add span-based CopyTo and CopyToAsync methods * Update according to feedback * Add span-based CopyTo overrides for MemoryStream * Improve span-based CopyTo arguments validation To avoid code duplication * Update according to second review Stream API is changed * Resolve InternalReadSpan/Memory inlining * Refactor ValidateCopyToArgs * Update according to third review * Update after fourth review * Override span CopyTo for UnmanagedMemoryStream * Apply suggestions from code review Co-Authored-By: Stephen Toub <stoub@microsoft.com> * Update after fifth review * Add cross sync/async support for span-based CopyTo * Call sync action directly in async context * Rework cross sync/async support for span-based CopyTo Co-Authored-By: Stephen Toub <stoub@microsoft.com> Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Contributes to https://github.com/dotnet/corefx/issues/21472