|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Copyright (c) Andrew Arnott. All rights reserved. |
| 3 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Buffers; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using System.Runtime.InteropServices; |
| 9 | + |
| 10 | +namespace MessagePack |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// A fast access struct that wraps <see cref="IBufferWriter{T}"/>. |
| 14 | + /// </summary> |
| 15 | + internal ref struct BufferWriter |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// The underlying <see cref="IBufferWriter{T}"/>. |
| 19 | + /// </summary> |
| 20 | + private IBufferWriter<byte> _output; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// The result of the last call to <see cref="IBufferWriter{T}.GetSpan(int)"/>, less any bytes already "consumed" with <see cref="Advance(int)"/>. |
| 24 | + /// Backing field for the <see cref="Span"/> property. |
| 25 | + /// </summary> |
| 26 | + private Span<byte> _span; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// The result of the last call to <see cref="IBufferWriter{T}.GetMemory(int)"/>, less any bytes already "consumed" with <see cref="Advance(int)"/>. |
| 30 | + /// </summary> |
| 31 | + private ArraySegment<byte> _segment; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// The number of uncommitted bytes (all the calls to <see cref="Advance(int)"/> since the last call to <see cref="Commit"/>). |
| 35 | + /// </summary> |
| 36 | + private int _buffered; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// The total number of bytes written with this writer. |
| 40 | + /// Backing field for the <see cref="BytesCommitted"/> property. |
| 41 | + /// </summary> |
| 42 | + private long _bytesCommitted; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Initializes a new instance of the <see cref="BufferWriter"/> struct. |
| 46 | + /// </summary> |
| 47 | + /// <param name="output">The <see cref="IBufferWriter{T}"/> to be wrapped.</param> |
| 48 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 49 | + public BufferWriter(IBufferWriter<byte> output) |
| 50 | + { |
| 51 | + _buffered = 0; |
| 52 | + _bytesCommitted = 0; |
| 53 | + _output = output; |
| 54 | + |
| 55 | + var memory = _output.GetMemory(); |
| 56 | + MemoryMarshal.TryGetArray(memory, out _segment); |
| 57 | + _span = memory.Span; |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Gets the result of the last call to <see cref="IBufferWriter{T}.GetSpan(int)"/>. |
| 62 | + /// </summary> |
| 63 | + public Span<byte> Span => _span; |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Gets the total number of bytes written with this writer. |
| 67 | + /// </summary> |
| 68 | + public long BytesCommitted => _bytesCommitted; |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Gets the <see cref="IBufferWriter{T}"/> underlying this instance. |
| 72 | + /// </summary> |
| 73 | + internal IBufferWriter<byte> UnderlyingWriter => _output; |
| 74 | + |
| 75 | + public Span<byte> GetSpan(int sizeHint) |
| 76 | + { |
| 77 | + Ensure(sizeHint); |
| 78 | + return this.Span; |
| 79 | + } |
| 80 | + |
| 81 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 82 | + public ref byte GetPointer(int sizeHint) |
| 83 | + { |
| 84 | + Ensure(sizeHint); |
| 85 | + |
| 86 | + if (_segment.Array != null) |
| 87 | + { |
| 88 | + return ref _segment.Array[_segment.Offset + _buffered]; |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + return ref _span.GetPinnableReference(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Calls <see cref="IBufferWriter{T}.Advance(int)"/> on the underlying writer |
| 98 | + /// with the number of uncommitted bytes. |
| 99 | + /// </summary> |
| 100 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 101 | + public void Commit() |
| 102 | + { |
| 103 | + var buffered = _buffered; |
| 104 | + if (buffered > 0) |
| 105 | + { |
| 106 | + _bytesCommitted += buffered; |
| 107 | + _buffered = 0; |
| 108 | + _output.Advance(buffered); |
| 109 | + _span = default; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Used to indicate that part of the buffer has been written to. |
| 115 | + /// </summary> |
| 116 | + /// <param name="count">The number of bytes written to.</param> |
| 117 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 118 | + public void Advance(int count) |
| 119 | + { |
| 120 | + _buffered += count; |
| 121 | + _span = _span.Slice(count); |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Copies the caller's buffer into this writer and calls <see cref="Advance(int)"/> with the length of the source buffer. |
| 126 | + /// </summary> |
| 127 | + /// <param name="source">The buffer to copy in.</param> |
| 128 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 129 | + public void Write(ReadOnlySpan<byte> source) |
| 130 | + { |
| 131 | + if (_span.Length >= source.Length) |
| 132 | + { |
| 133 | + source.CopyTo(_span); |
| 134 | + Advance(source.Length); |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + WriteMultiBuffer(source); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// Acquires a new buffer if necessary to ensure that some given number of bytes can be written to a single buffer. |
| 144 | + /// </summary> |
| 145 | + /// <param name="count">The number of bytes that must be allocated in a single buffer.</param> |
| 146 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 147 | + public void Ensure(int count = 1) |
| 148 | + { |
| 149 | + if (_span.Length < count) |
| 150 | + { |
| 151 | + EnsureMore(count); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Gets a fresh span to write to, with an optional minimum size. |
| 157 | + /// </summary> |
| 158 | + /// <param name="count">The minimum size for the next requested buffer.</param> |
| 159 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 160 | + private void EnsureMore(int count = 0) |
| 161 | + { |
| 162 | + if (_buffered > 0) |
| 163 | + { |
| 164 | + Commit(); |
| 165 | + } |
| 166 | + |
| 167 | + var memory = _output.GetMemory(count); |
| 168 | + MemoryMarshal.TryGetArray(memory, out _segment); |
| 169 | + _span = memory.Span; |
| 170 | + } |
| 171 | + |
| 172 | + /// <summary> |
| 173 | + /// Copies the caller's buffer into this writer, potentially across multiple buffers from the underlying writer. |
| 174 | + /// </summary> |
| 175 | + /// <param name="source">The buffer to copy into this writer.</param> |
| 176 | + private void WriteMultiBuffer(ReadOnlySpan<byte> source) |
| 177 | + { |
| 178 | + while (source.Length > 0) |
| 179 | + { |
| 180 | + if (_span.Length == 0) |
| 181 | + { |
| 182 | + EnsureMore(); |
| 183 | + } |
| 184 | + |
| 185 | + var writable = Math.Min(source.Length, _span.Length); |
| 186 | + source.Slice(0, writable).CopyTo(_span); |
| 187 | + source = source.Slice(writable); |
| 188 | + Advance(writable); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments