< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 48
Coverable lines: 48
Total lines: 101
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
RentWriterAndBuffer(...)100%110%
RentWriterAndBuffer(...)0%440%
RentWriter(...)0%440%
ReturnWriterAndBuffer(...)0%220%
ReturnWriter(...)100%110%
.ctor()100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriterCache.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Buffers;
 5using System.Diagnostics;
 6
 7namespace System.Text.Json
 8{
 9    /// <summary>
 10    /// Defines a thread-local cache for JsonSerializer to store reusable Utf8JsonWriter/IBufferWriter instances.
 11    /// </summary>
 12    internal static class Utf8JsonWriterCache
 13    {
 14        [ThreadStatic]
 15        private static ThreadLocalState? t_threadLocalState;
 16
 17        public static Utf8JsonWriter RentWriterAndBuffer(JsonSerializerOptions options, out PooledByteBufferWriter buffe
 018            RentWriterAndBuffer(options.GetWriterOptions(), options.DefaultBufferSize, out bufferWriter);
 19
 20        public static Utf8JsonWriter RentWriterAndBuffer(JsonWriterOptions options, int defaultBufferSize, out PooledByt
 021        {
 022            ThreadLocalState state = t_threadLocalState ??= new();
 23            Utf8JsonWriter writer;
 24
 025            if (state.RentedWriters++ == 0)
 026            {
 27                // First JsonSerializer call in the stack -- initialize & return the cached instances.
 028                bufferWriter = state.BufferWriter;
 029                writer = state.Writer;
 30
 031                bufferWriter.InitializeEmptyInstance(defaultBufferSize);
 032                writer.ConfigureForCacheReuse(bufferWriter, options);
 033            }
 34            else
 035            {
 36                // We're in a recursive JsonSerializer call -- return fresh instances.
 037                bufferWriter = new PooledByteBufferWriter(defaultBufferSize);
 038                writer = new Utf8JsonWriter(bufferWriter, options);
 039            }
 40
 041            return writer;
 042        }
 43
 44        public static Utf8JsonWriter RentWriter(JsonSerializerOptions options, IBufferWriter<byte> bufferWriter)
 045        {
 046            ThreadLocalState state = t_threadLocalState ??= new();
 47            Utf8JsonWriter writer;
 48
 049            if (state.RentedWriters++ == 0)
 050            {
 51                // First JsonSerializer call in the stack -- initialize & return the cached instance.
 052                writer = state.Writer;
 053                writer.ConfigureForCacheReuse(bufferWriter, options.GetWriterOptions());
 054            }
 55            else
 056            {
 57                // We're in a recursive JsonSerializer call -- return a fresh instance.
 058                writer = new Utf8JsonWriter(bufferWriter, options.GetWriterOptions());
 059            }
 60
 061            return writer;
 062        }
 63
 64        public static void ReturnWriterAndBuffer(Utf8JsonWriter writer, PooledByteBufferWriter bufferWriter)
 065        {
 066            Debug.Assert(t_threadLocalState is not null);
 067            ThreadLocalState state = t_threadLocalState;
 68
 069            writer.ResetAllStateForCacheReuse();
 070            bufferWriter.ClearAndReturnBuffers();
 71
 072            int rentedWriters = --state.RentedWriters;
 073            Debug.Assert((rentedWriters == 0) == (ReferenceEquals(state.BufferWriter, bufferWriter) && ReferenceEquals(s
 074        }
 75
 76        public static void ReturnWriter(Utf8JsonWriter writer)
 077        {
 078            Debug.Assert(t_threadLocalState is not null);
 079            ThreadLocalState state = t_threadLocalState;
 80
 081            writer.ResetAllStateForCacheReuse();
 82
 083            int rentedWriters = --state.RentedWriters;
 084            Debug.Assert((rentedWriters == 0) == ReferenceEquals(state.Writer, writer));
 085        }
 86
 87        private sealed class ThreadLocalState
 88        {
 89            public readonly PooledByteBufferWriter BufferWriter;
 90            public readonly Utf8JsonWriter Writer;
 91            public int RentedWriters;
 92
 093            public ThreadLocalState()
 094            {
 095                BufferWriter = PooledByteBufferWriter.CreateEmptyInstanceForCaching();
 096                Writer = Utf8JsonWriter.CreateEmptyInstanceForCaching();
 097            }
 98        }
 99    }
 100}
 101