Use exponential buffer growth in LoopbackServer test helper#127779
Merged
Use exponential buffer growth in LoopbackServer test helper#127779
Conversation
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/1a097ac5-951c-467e-a977-07eabc32449a Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the HTTP/1.1 loopback test helper used across System.Net.Http functional tests to grow two internal read buffers exponentially instead of by fixed 4 KB increments, aiming to reduce repeated copying when handling large payloads.
Changes:
- Changes
Connection.ReadToEndAsyncto resize its temporary buffer with* 2growth. - Changes
Connection.ReadLineBytesAsyncto resize_readBufferwith* 2growth. - Keeps the change scoped to the shared
LoopbackServertest infrastructure used by networking tests.
Comments suppressed due to low confidence (1)
src/libraries/Common/tests/System/Net/Http/LoopbackServer.cs:643
- When
dataLengthis 0 (for example, the previous line ended exactly at the end of_readBuffer), this branch allocates a larger array but never installs it or resets the indices. The subsequent read then uses a count of 0 andReadLineBytesAsynctreats that as EOF, so the helper can stop parsing even though more request data is still available.
byte[] newBuffer = new byte[_readBuffer.Length * 2];
int dataLength = _readEnd - _readStart;
if (dataLength > 0)
{
Array.Copy(_readBuffer, _readStart, newBuffer, 0, dataLength);
_readStart = 0;
_readEnd = dataLength;
_readBuffer = newBuffer;
startSearch = dataLength;
…n ReadToEndAsync/ReadLineBytesAsync Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/64def83f-7ce6-49e3-8227-26dd9cd45de5 Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
MihaZupan
approved these changes
May 5, 2026
Member
|
/ba-g infra failures: Remote machine provider issue: The remote provider was unable to process the request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
LoopbackServer.Connectiongrows its read buffers by a fixed+BufferSize(4000 bytes) on each resize, causing O(n²) copying for large payloads. Changed to*2doubling in bothReadToEndAsyncandReadLineBytesAsyncusingArray.Resize.ReadToEndAsync:bytesRead == buffer.Lengthtooffset == buffer.Length(the previous check would stop triggering after the first resize, since subsequent reads only fill the remaining buffer space)new byte[] + CopyTowithArray.Resize(ref buffer, buffer.Length * 2)totalLengthvariable; the finalGetStringnow usesoffsetdirectlyReadLineBytesAsync:new byte[] + Array.Copy(into new buffer) with in-placeArray.Copyto compact live data, followed byArray.Resize(ref _readBuffer, _readBuffer.Length * 2)_readStart,_readEnd,startSearch) are now always applied unconditionally after the compact step