Skip to content
This repository was archived by the owner on Aug 2, 2023. It is now read-only.

Cache the span by using BufferWriter<T> within JsonWriter<T>#2374

Merged
ahsonkhan merged 5 commits into
dotnet:masterfrom
ahsonkhan:BufferWriterSupport
Jun 28, 2018
Merged

Cache the span by using BufferWriter<T> within JsonWriter<T>#2374
ahsonkhan merged 5 commits into
dotnet:masterfrom
ahsonkhan:BufferWriterSupport

Conversation

@ahsonkhan
Copy link
Copy Markdown
Contributor

Borrows most of the changes from #2366, and supersedes #2358

  • Utilizing BufferWriter to cache the span being written to and add the Flush method.
  • Keeping a single type for now, as it will only support UTF-8 (will remove UTF-16 support later).
  • Reducing the JsonWriter struct size by utilizing the highest bit of _indent for _firstItem bool.
  • Fix typo in BufferWriter.Write(byte)

TODO: Add helper factory convenience methods.

Let me know if I missed anything. Any suggestions to reduce the overhead further are welcome :)

  • ~10% regression for hello world (overhead of creating BufferWriter and no benefit from caching the span)
  • ~7% improvement for writing larger json payload, say 10-50 elements (overhead amortized, benefit of caching span kicks in)
  • ~20% improvement for writing large json payload, say 1000 array elements

cc @benaadams, @JeremyKuhne

BenchmarkDotNet=v0.10.14.534-nightly, OS=Windows 10.0.14393.2312 (1607/AnniversaryUpdate/Redstone1)
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
Frequency=3328126 Hz, Resolution=300.4694 ns, Timer=TSC
.NET Core SDK=2.2.100-preview1-009015
  [Host]     : .NET Core 2.2.0-preview1-26609-02 (CoreCLR 4.6.26606.04, CoreFX 4.6.26606.04), 64bit RyuJIT
  DefaultJob : .NET Core 2.2.0-preview1-26609-02 (CoreCLR 4.6.26606.04, CoreFX 4.6.26606.04), 64bit RyuJIT

BEFORE (master):

Method IsUTF8Encoded Formatted size Mean Error StdDev Allocated
WriterSystemTextJsonArrayOnly True False 1 83.15 ns 0.9320 ns 0.8262 ns 0 B
WriterSystemTextJsonArrayOnly True False 10 231.66 ns 4.6436 ns 6.0380 ns 0 B
WriterSystemTextJsonArrayOnly True False 1000 19,022.72 ns 369.7130 ns 467.5686 ns 0 B
WriterSystemTextJsonBasic True False ? 975.70 ns 19.5078 ns 43.2279 ns 0 B
WriterSystemTextJsonHelloWorld True False ? 104.12 ns 1.8728 ns 2.0039 ns 0 B

AFTER (this PR):

Method IsUTF8Encoded Formatted size Mean Error StdDev Allocated
WriterSystemTextJsonArrayOnly True False 1 91.47 ns 2.301 ns 3.445 ns 0 B
WriterSystemTextJsonArrayOnly True False 10 214.79 ns 4.653 ns 6.673 ns 0 B
WriterSystemTextJsonArrayOnly True False 1000 15,995.46 ns 269.920 ns 239.277 ns 0 B
WriterSystemTextJsonBasic True False ? 908.57 ns 17.670 ns 21.035 ns 0 B
WriterSystemTextJsonHelloWorld True False ? 113.91 ns 2.311 ns 5.799 ns 0 B

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant