Closed colgreen closed 1 year ago
@colgreen Hi! Thank you, this looks really nice. It took me a while to digest all these changes, but they are looking good. Oh and benchmark results looks awesome - thanks. I'm gonna merge this and release new version soon.
Added in 0.4.6.
@colgreen Hi! Thank you, this looks really nice. It took me a while to digest all these changes, but they are looking good. Oh and benchmark results looks awesome - thanks. I'm gonna merge this and release new version soon.
No problem. Thanks for taking a look and merging 👍 🚀
This PR takes the code from PR #39, and further reduces memory allocations. This is done by writing JSON directly into a MemoryStream (actually a RecyclableMemoryStream) as UTF8 bytes. This provide the following benefits:
1) Avoids the final call to stringBuilder.toString() and the large temporary JSON string that allocates (and the copying of characters from the StringBuilder into that string). Instead, the memory stream can be passed directly to JsonSerializer.Deserialize().
2) The characters are written to the memory stream utf8 encoded, which writes one byte per character (for typical payloads) instead of two bytes per character for strings.
These factors are particularly beneficial when handling large binary hex values.
Here are some BenchmarkDotNet benchmarks comparing performance in these these three branches:
The benchmark used here processes 100 rows. This new PR uses 108.01 / 409.46 = 26.37% of the memory allocations, compared to the current master branch. Speed is slightly faster too.
Here's a benchmark for processing 1MiB of binary (2,000,000 hex characters)...
Memory allocations are reduced from 34MB to 37 bytes! Performance is 3.56 faster.
/cc @georgebarbu