Repository navigation
Description
Background and motivation
Zero-byte reads are a technique used in our networking stacks (System.Net, ASP.NET, YARP) to defer renting/allocating memory until there's data available on the socket.
This can greatly reduce memory usage in applications with a lot of concurrent operations.
In the case of YARP, we've seen a 10x reduction in process memory consumption when used with WebSockets.
With Json, this would help in cases where the whole object hasn't been read into memory yet.
An API that would benefit greatly is JsonSerializer.DeserializeAsyncEnumerable which can be long-lived and spend a lot of time just waiting on IO.
Starting with .NET 7.0, the streams returned by HttpClient are able to take advantage of this technique.
System.Net.Http.Json extensions are therefore a prime candidate where we should strongly consider enabling this option by default.
Prior art:
StreamPipeReaderOptions.UseZeroByteReadsfromSystem.IO.Pipelines(Add ability to perform zero byte reads in StreamPipeReader #37539)SocketTransportOptions.WaitForDataBeforeAllocatingBufferfrom Kestrel
@davidfowl I assume this wouldn't do much in ASP.NET since the HttpContext.Body already does zero-byte reads internally, right? That is unless the json implementation would be able to defer allocating its own buffers on top of using ZBRs on the underlying transport.
API Proposal
namespace System.Text.Json;
public sealed class JsonSerializerOptions
{
// New property on existing type
public bool UseZeroByteReads { get; set; }
}API Usage
private static readonly JsonSerializerOptions s_jsonOptions = new() { UseZeroByteReads = true };
public static IAsyncEnumerable<Foo> DeserializeAsync(Stream stream) =>
JsonSerializer.DeserializeAsyncEnumerable<Foo>(stream, s_jsonOptions);Alternative Designs
If the json implementation isn't capable of deferring allocations of its own buffers while waiting for more data, the user could achieve similar performance gains by implementing their own Stream wrapper that performed zero-byte reads and passing that to JsonSerializer instead.
Activity
krwq commentedon Nov 8, 2022
@MihaZupan Stream.Read/ReadAsync are documented that zero bytes means EOF (last sentence specifically):
https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.read?view=net-7.0
If that is no longer true should we change the docs?
IMO we're working correctly here, I think it makes more sense for you to create stream wrapper if you prefer different to default behavior.
Possibly it makes more sense to add that flag to Stream itself?
stephentoub commentedon Nov 8, 2022
The zero here refers to the input, i.e. asking for zero bytes.
krwq commentedon Nov 23, 2022
@MihaZupan I'm marking this as Future for now. If this is impacting your scenario significantly or if you're planning to contribute please let me know and we can bump priority. We will unlikely have time to address this in 8.0 otherwise.
MihaZupan commentedon Jul 25, 2023
I suppose another alternative design here would be for
System.Text.Jsonto always use zero-byte reads and there is no API to toggle that behavior.MihaZupan commentedon Jul 27, 2023
Any thoughts on this @krwq? We might not need an API review.
ilmax commentedon Jul 27, 2023
I feel like this doesn't belong to the JsonSerializerOptions. It's something too low level compared to what's in the option IMO. As a user I am expecting to find details about serialization convention, case handling and what not there, not low level details about how the serializer allocates memory while reading from a stream.
My suggestion is to always do zero bytes read and eventually, if you really want to have a switch, implement it via AppContext or something alike.
Just my 2 cent :)
MihaZupan commentedon Oct 13, 2023
Any thoughts on this @dotnet/area-system-text-json: #77935 (comment)?
En3Tho commentedon Oct 13, 2023
I agree with @ilmax here. I think if profitable, STJ should just implement this. Zero byte reads are now used in other places in BCL by default.