FluentFTP icon indicating copy to clipboard operation
FluentFTP copied to clipboard

Override Read(Span), ReadAsync(Memory), Write(ROS), WriteAsync(ROM) in streams

Open bbowyersmyth opened this issue 1 year ago • 3 comments

Available in NETSTANDARD2_1 and NET5_0_OR_GREATER, the base class functions in Stream for backwards compatibility rent an array and copy the span to it and then call the old array based functions.

As an optimization FtpDataStream and FtpSocketStream can override these functions and pass the call directly onto the wrapped stream.

Closes #1570

bbowyersmyth avatar May 08 '24 22:05 bbowyersmyth

The changes look good. But are these new methods automatically used by the .NET Framework or the Stream system? As we are not invoking it directly.

robinrodricks avatar May 09 '24 04:05 robinrodricks

@robinrodricks Yes stream and other places in system will now prefer to use the new overloads if they don't already have an array. For example FileStream.CopyToAsync uses the memory overload https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Windows.cs#L299C39-L299C49

So they may already be getting called. Will know if we can put a break point in there when the tests are run. Otherwise we will need to add more.

bbowyersmyth avatar May 09 '24 06:05 bbowyersmyth

I have also been investigating bypassing the BUF copying inherent in the FtpDataStream and FtpSocketStream on the way to (or from) network stream.

Newer .NET versions give one this option:

Prefer the memory-based overloads of ReadAsync/WriteAsync methods in stream-based classes

For example: await base.ReadAsync(buffer.AsMemory(offset, count), token); instead of await base.ReadAsync(buffer, offset, count, token);

Would of course be a code change and another set of #IF/#ENDIF brackets.

FanDjango avatar May 09 '24 19:05 FanDjango

Added 2 more UploadDownload tests as the existing ones weren't using the streams directly. Fixed a couple of compiler warnings in the file also.

bbowyersmyth avatar May 11 '24 03:05 bbowyersmyth

This looks interesting. I'm merging it for now. If we have serious issues arising from this change, we can revert it.

Thanks for your contribution!

robinrodricks avatar May 12 '24 03:05 robinrodricks