-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improper use of RecyclableMemoryStream #149
Comments
Yes indeed. The fact is that there are 3 use cases for this
The last case 3. is the most efficient as you can use the For 1., I made a PR to use For 2., we have to pass a
What are you referring to? |
I'm referring to both Newtonsoft and System.Text.Json being able to accept streams, or in STJ's instance ReadonlySpans as well. If performance is a concern in this package (it may not be, it may be secondary to ease of use by novice developers), then persisting to string makes little sense from a performance perspective, not to mention payloads larger than 85kb ending up on the LOH. |
You can set |
I must have missed that part of the API. Thanks for clarifying! |
While the use of RecyclableMemoryStream is to be commended, the implementation negates any advantage of using it due to the use of ToArray().
Use GetBuffer or GetMemory instead.
Also, converting json to strings seems rather unnecessary as it just causes more allocations.
Directly from the RecyclableMemoryStream documentation;
ToArray - It looks similar to GetBuffer on the surface, but is actually significantly different. In ToArray the data is always copied into a new array that is exactly the right length for the full contents of the stream. This new buffer is never pooled. Users of this library should consider any call to ToArray to be a bug, as it wipes out many of the benefits of RecyclableMemoryStream completely. However, the method is included for completeness, especially if you are calling other APIs that only take a byte array with no length parameter. An event is logged on all ToArray calls.
The text was updated successfully, but these errors were encountered: