Buffer pooling for JSON serialization - #642
Conversation
| /// Serialize the document object into <paramref name="writer"/>. | ||
| /// </summary> | ||
| /// <param name="document"></param> | ||
| /// <param name="writer"></param> |
There was a problem hiding this comment.
@jeremydmiller Can we change the seam? From semver perspective this is going to be a breaking change (major). We could provide a new interface that would be implemented by serializers. For now I assume it's ok to change this.
There was a problem hiding this comment.
It's not a major change if it's additive. I say yes.
There was a problem hiding this comment.
It broke DocJsonBodyArgument as it was selecting the method by name ;-)
|
|
||
| /// <summary> | ||
| /// Configures the store to use char buffer pooling, greatly reducing allocations for serializing documents and events. | ||
| /// </summary> |
There was a problem hiding this comment.
Should the default be to use char buffer pooling? I'm not a fan of having a bool default to true so perhaps consider flipping the name and logic around to something like SuppressCharBufferPooling.
There was a problem hiding this comment.
In majority of cases I prefer to use opt-in, then opt-out in a next version of a product. I'm not changing the behavior now as it's easier to test with no caching and focus on a few tests with caching. I'll add this as the point to this PR though.
There was a problem hiding this comment.
That sounds like a good plan
|
@jeremydmiller The initial phase of implementation is done in let's say 60%. I use only one test for testing the augmented Also, I've just found |
|
I followed the way Marten handles dirty checking. This lead me to fetching documents which again uses strings. If we wanted to make no allocations that would require to change the fetch mechanism as well and use the char buffers in there. ☝️ This is a some kind of note to myself. I'll try to play a little bit with it and post my findings in here. |
|
Ok, I went through the API again. Basically, I'm looking forward to hearing from you as it requires a lot of effort @jeremydmiller |
…ew cached char buffers. SprocCall can create a param with its size set
…ed down via session to UpdateBatch
|
@Scooletz I'm pulling this into master as much as anything to make rebase's not be a problem. The one and only change I made was to turn on character pooling by default. That exposed a single test failure, but that one was strictly a testing problem. I say it looks good as is. The next thing I'd love to do is to add some profiling w/ Benchmark.Net to check the allocations with and without the pooling turned on. After that, I'd like to retrofit some tests that hit this w/ multiple threads pretty hard. Correct me if I'm wrong, but this is only applied to writing documents in the |
|
👍 for Yes, this is only applied to writing documents in the |
Addresses #640
This PR Introduces
CharArrayTextWriterthat by provides access to itschar[]. Additionally changes the way that driver parameters are created, enables reusing memory and reducing allocations for serializing.Plan
DocJsonBodyArgumentto use pooled writerchangeEventStreamAppenderto use pooled writersDocumentStoreopt-inoropt-outbehaviorNotes:
EventStreamAppendercan't be changed now as Npgsql does not support writingArraySegment<char>asjsonb,jsonnorstring. I took a look at the implementation of the driver at it would require a lot of work as the WriteBuffer does not have overloads for this. If we had it, then appending events would be easy.