FileBufferingReadStream CopyToAsync Performance problem. #24032
Labels
area-networking
Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
bug
This issue describes a behavior which is not expected - a bug.
Milestone
When buffering a response using FileBufferingReadStream: initially, the stream's Position and Length are both 0. Based on the code in https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs#L160-L188, CopyToAsync chooses a buffer size of 1.
When the stream is say, 16KB, this triggers a FileStream.GetLengthInternal() call 16,384 times and causes a big CPU spike for requests that are buffered to disk.
Please see attached profiler trace.

We are currently solving this issue by calling the overload of CopyToAsync that accepts an explicit buffer size (we chose 16KB).
The text was updated successfully, but these errors were encountered: