< Summary

Information
Line coverage
50%
Covered lines: 24
Uncovered lines: 24
Coverable lines: 48
Total lines: 100
Line coverage: 50%
Branch coverage
40%
Covered branches: 4
Total branches: 10
Branch coverage: 40%
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(...)75%4473.33%
RentWriter(...)0%440%
ReturnWriterAndBuffer(...)50%22100%
ReturnWriter(...)100%110%
.ctor()100%11100%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\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
 2821        {
 2822            ThreadLocalState state = t_threadLocalState ??= new();
 23            Utf8JsonWriter writer;
 24
 2825            if (state.RentedWriters++ == 0)
 2826            {
 27                // First JsonSerializer call in the stack -- initialize & return the cached instances.
 2828                bufferWriter = state.BufferWriter;
 2829                writer = state.Writer;
 30
 2831                bufferWriter.InitializeEmptyInstance(defaultBufferSize);
 2832                writer.Reset(bufferWriter, options);
 2833            }
 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
 2841            return writer;
 2842        }
 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.Reset(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)
 2865        {
 2866            Debug.Assert(t_threadLocalState != null);
 2867            ThreadLocalState state = t_threadLocalState;
 68
 2869            writer.ResetAllStateForCacheReuse();
 2870            bufferWriter.ClearAndReturnBuffers();
 71
 2872            int rentedWriters = --state.RentedWriters;
 2873            Debug.Assert((rentedWriters == 0) == (ReferenceEquals(state.BufferWriter, bufferWriter) && ReferenceEquals(s
 2874        }
 75
 76        public static void ReturnWriter(Utf8JsonWriter writer)
 077        {
 078            Debug.Assert(t_threadLocalState != 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
 193            public ThreadLocalState()
 194            {
 195                BufferWriter = PooledByteBufferWriter.CreateEmptyInstanceForCaching();
 196                Writer = Utf8JsonWriter.CreateEmptyInstanceForCaching();
 197            }
 98        }
 99    }
 100}