[WIP] Optimize JsonWriterUtf8 WriteValue overloads#2332
Conversation
I am not sure. I think we should compare the performance of refactoring the code this way. Update this performance benchmark to write bool values instead of long values and see how performance is affected: Also add the DisassemblyDiagnoser attribute to the benchmark test to see the assembly that gets generated. It will help us see if this approach is worth it. If you need to see how to run the benchmarks, see https://github.com/dotnet/corefxlab#measuring-performance |
| WriteJsonValue(JsonConstants.FalseValue); | ||
| int seperatorSize = ItemSeperatorSize(); | ||
| int spacingSize = SpacingSize(); | ||
| int literalSize = LiteralSize(literal); |
There was a problem hiding this comment.
This seems unnecessary. We should just use literal.Length where its needed.
| Span<byte> byteBuffer = EnsureBuffer(bytesNeeded); | ||
|
|
||
| int idx = 0; | ||
| idx += this.WriteItemSeperator(byteBuffer.Slice(idx, seperatorSize)); |
There was a problem hiding this comment.
Can we do all these in a single method to avoid the multiple span slices and additions to idx?
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| private int WriteSpacing(Span<byte> byteBuffer) |
There was a problem hiding this comment.
Wouldn't the return values of these methods always equal what the "{X}Size()" methods return?
WriteSpacing would return whatever SpacingSize does. So, why do we need to return an integer here?
|
Thanks for the review and the pointer to measuring performance. I am experimenting with performance now. |
|
What changes did you make to get those results? It seems to be an out-lier. All other UTF-8 benchmarks show SystemTextJson is faster. We should try to optimize the implementation further for this case as well. |
Does this pattern sounds like a way to go?