Newtonsoft.Json
Newtonsoft.Json copied to clipboard
[Proposal] Please, implement special high-performance JsonMemoryStreamReader for single-byte-encoded message streams
Hello,
Working with heavy loaded message streams we are pretty sure that every message is an ASCII encoded single-byte string. We receive them as byte[] or as MemoryStream (both are very close siblings). For example, most crypto exchanges use messages with these properties.
Now we have a few options:
- to convert this array to a string (that allocates twice as many bytes as initial array) and to throw away this string just after deserialization
- To wrap byte[] array to MemoryStream and after that to wrap it again to a StreamReader. Which can be used in JsonTextReader.
Second option is a little bit better: it does not use intermediate temporary string. But it creates many short-living intermediate objects.
I found that it is easy to create special implementation of JsonMsAsciiReader that directly uses raw MemoryStream (and thows an exception if it observes any byte over 127).
But this implementation still uses internal field
private char[]? _chars;
And it has to convert every byte to a corresponding 2-byte char.
So I feel that in this usecase performance can be further improved if you provide special deeply optimized JsonMemoryStreamReader that will work with raw bytes.
Thank you in advance and, BTW, thank you very much for your magnificent library!
P.S. Here it is an implementation of a 'half optimized' class JsonMsAsciiReader : JsonMsAsciiReader.cs