Fix: Address missing sizeHint in BuffersExtensions write logic#126776
Fix: Address missing sizeHint in BuffersExtensions write logic#126776Plerx2493 wants to merge 1 commit intodotnet:mainfrom
sizeHint in BuffersExtensions write logic#126776Conversation
|
Tagging subscribers to this area: @dotnet/area-system-memory |
There was a problem hiding this comment.
Pull request overview
Refactors BuffersExtensions.Write to pass an explicit sizeHint to IBufferWriter<T>.GetSpan(...), improving allocation behavior by requesting spans sized to the data being written.
Changes:
- Update initial
Writepath to callGetSpan(value.Length)instead ofGetSpan(). - Update multi-segment write loop to call
GetSpan(input.Length)for remaining data rather thanGetSpan().
| public static void Write<T>(this IBufferWriter<T> writer, ReadOnlySpan<T> value) | ||
| { | ||
| Span<T> destination = writer.GetSpan(); | ||
| Span<T> destination = writer.GetSpan(value.Length); | ||
|
|
||
| // Fast path, try copying to the available memory directly |
There was a problem hiding this comment.
This change alters the observable behavior of Write for writers that allocate based on sizeHint (e.g., the existing TestBufferWriterMultiSegment in System.Memory tests). With GetSpan(value.Length) / GetSpan(input.Length), those tests will no longer write one byte per segment (they'll typically write the whole span in a single segment), so current assertions like Comitted.Count == 12 will fail. Please update/add tests to validate the new contract (that the sizeHint passed equals the remaining input length) rather than relying on the old sizeHint == 0 growth behavior.
|
As it seems i was confused by the naming of the parameter and this would be the wrong behaivior.
|
Refactor
BuffersExtensionsto improve memory allocation efficiency. This update ensures theIBufferWriterreceives the correctsizeHintbased on the provided span size during data write operations.