| | | 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 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace 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 |
| | 0 | 18 | | RentWriterAndBuffer(options.GetWriterOptions(), options.DefaultBufferSize, out bufferWriter); |
| | | 19 | | |
| | | 20 | | public static Utf8JsonWriter RentWriterAndBuffer(JsonWriterOptions options, int defaultBufferSize, out PooledByt |
| | 28 | 21 | | { |
| | 28 | 22 | | ThreadLocalState state = t_threadLocalState ??= new(); |
| | | 23 | | Utf8JsonWriter writer; |
| | | 24 | | |
| | 28 | 25 | | if (state.RentedWriters++ == 0) |
| | 28 | 26 | | { |
| | | 27 | | // First JsonSerializer call in the stack -- initialize & return the cached instances. |
| | 28 | 28 | | bufferWriter = state.BufferWriter; |
| | 28 | 29 | | writer = state.Writer; |
| | | 30 | | |
| | 28 | 31 | | bufferWriter.InitializeEmptyInstance(defaultBufferSize); |
| | 28 | 32 | | writer.Reset(bufferWriter, options); |
| | 28 | 33 | | } |
| | | 34 | | else |
| | 0 | 35 | | { |
| | | 36 | | // We're in a recursive JsonSerializer call -- return fresh instances. |
| | 0 | 37 | | bufferWriter = new PooledByteBufferWriter(defaultBufferSize); |
| | 0 | 38 | | writer = new Utf8JsonWriter(bufferWriter, options); |
| | 0 | 39 | | } |
| | | 40 | | |
| | 28 | 41 | | return writer; |
| | 28 | 42 | | } |
| | | 43 | | |
| | | 44 | | public static Utf8JsonWriter RentWriter(JsonSerializerOptions options, IBufferWriter<byte> bufferWriter) |
| | 0 | 45 | | { |
| | 0 | 46 | | ThreadLocalState state = t_threadLocalState ??= new(); |
| | | 47 | | Utf8JsonWriter writer; |
| | | 48 | | |
| | 0 | 49 | | if (state.RentedWriters++ == 0) |
| | 0 | 50 | | { |
| | | 51 | | // First JsonSerializer call in the stack -- initialize & return the cached instance. |
| | 0 | 52 | | writer = state.Writer; |
| | 0 | 53 | | writer.Reset(bufferWriter, options.GetWriterOptions()); |
| | 0 | 54 | | } |
| | | 55 | | else |
| | 0 | 56 | | { |
| | | 57 | | // We're in a recursive JsonSerializer call -- return a fresh instance. |
| | 0 | 58 | | writer = new Utf8JsonWriter(bufferWriter, options.GetWriterOptions()); |
| | 0 | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | return writer; |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public static void ReturnWriterAndBuffer(Utf8JsonWriter writer, PooledByteBufferWriter bufferWriter) |
| | 28 | 65 | | { |
| | 28 | 66 | | Debug.Assert(t_threadLocalState != null); |
| | 28 | 67 | | ThreadLocalState state = t_threadLocalState; |
| | | 68 | | |
| | 28 | 69 | | writer.ResetAllStateForCacheReuse(); |
| | 28 | 70 | | bufferWriter.ClearAndReturnBuffers(); |
| | | 71 | | |
| | 28 | 72 | | int rentedWriters = --state.RentedWriters; |
| | 28 | 73 | | Debug.Assert((rentedWriters == 0) == (ReferenceEquals(state.BufferWriter, bufferWriter) && ReferenceEquals(s |
| | 28 | 74 | | } |
| | | 75 | | |
| | | 76 | | public static void ReturnWriter(Utf8JsonWriter writer) |
| | 0 | 77 | | { |
| | 0 | 78 | | Debug.Assert(t_threadLocalState != null); |
| | 0 | 79 | | ThreadLocalState state = t_threadLocalState; |
| | | 80 | | |
| | 0 | 81 | | writer.ResetAllStateForCacheReuse(); |
| | | 82 | | |
| | 0 | 83 | | int rentedWriters = --state.RentedWriters; |
| | 0 | 84 | | Debug.Assert((rentedWriters == 0) == ReferenceEquals(state.Writer, writer)); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | private sealed class ThreadLocalState |
| | | 88 | | { |
| | | 89 | | public readonly PooledByteBufferWriter BufferWriter; |
| | | 90 | | public readonly Utf8JsonWriter Writer; |
| | | 91 | | public int RentedWriters; |
| | | 92 | | |
| | 1 | 93 | | public ThreadLocalState() |
| | 1 | 94 | | { |
| | 1 | 95 | | BufferWriter = PooledByteBufferWriter.CreateEmptyInstanceForCaching(); |
| | 1 | 96 | | Writer = Utf8JsonWriter.CreateEmptyInstanceForCaching(); |
| | 1 | 97 | | } |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |