You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The System.Buffers.ArrayBufferWriter<T> class has a ResetWrittenCount method, which resets the write position to zero without clearing the buffer, thus allowing a writer to be reused without clearing the buffer. Without this method, we must create a new ArrayPoolBufferWriter<T> instance each time, which carries the GC overhead of allocating the new object, but also the overhead of returning the buffer to the pool, only to immediately get it back again.
usingvarbuffer=newArrayPoolBufferWriter<byte>();usingvarjsonWriter=newUtf8JsonWriter(buffer);for(vari=0;i<100;i++){// Serialize JSON into buffer.JsonSerializer.Serialize(jsonWriter,obj,_jsonOptions);// Compress JSON into output buffer.compressionStream.Write(buffer.WrittenSpan);// Reset the JSON writer and buffer.buffer.ResetWrittenCount();jsonWriter.Reset();}
Breaking change?
No
Alternatives
There is no way to reuse the buffer without incurring the cost of clearing the buffer or cycling the buffer through the array pool.
Additional context
No response
Help us help you
Yes, I'd like to be assigned to work on this item
The text was updated successfully, but these errors were encountered:
Overview
The
System.Buffers.ArrayBufferWriter<T>
class has aResetWrittenCount
method, which resets the write position to zero without clearing the buffer, thus allowing a writer to be reused without clearing the buffer. Without this method, we must create a newArrayPoolBufferWriter<T>
instance each time, which carries the GC overhead of allocating the new object, but also the overhead of returning the buffer to the pool, only to immediately get it back again.API breakdown
Usage example
Breaking change?
No
Alternatives
There is no way to reuse the buffer without incurring the cost of clearing the buffer or cycling the buffer through the array pool.
Additional context
No response
Help us help you
Yes, I'd like to be assigned to work on this item
The text was updated successfully, but these errors were encountered: