|
| 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 | +using System.Runtime.CompilerServices; |
| 7 | + |
| 8 | +// Copied from https://github.com/dotnet/runtime/blob/a9ed4168626c14b4d74db0d8c205c69e56fc45ed/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.cs |
| 9 | +// with unused members removed. |
| 10 | + |
| 11 | +namespace System.Collections.Generic; |
| 12 | + |
| 13 | +internal ref partial struct ValueListBuilder<T> |
| 14 | +{ |
| 15 | + private Span<T> _span; |
| 16 | + private T[]? _arrayFromPool; |
| 17 | + private int _pos; |
| 18 | + |
| 19 | + public ValueListBuilder(Span<T> initialSpan) |
| 20 | + { |
| 21 | + _span = initialSpan; |
| 22 | + _arrayFromPool = null; |
| 23 | + _pos = 0; |
| 24 | + } |
| 25 | + |
| 26 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 27 | + public void Add(T item) |
| 28 | + { |
| 29 | + int pos = _pos; |
| 30 | + |
| 31 | + // Workaround for https://github.com/dotnet/runtime/issues/72004 |
| 32 | + Span<T> span = _span; |
| 33 | + if ((uint)pos < (uint)span.Length) |
| 34 | + { |
| 35 | + span[pos] = item; |
| 36 | + _pos = pos + 1; |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + AddWithResize(item); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 45 | + private void AddWithResize(T item) |
| 46 | + { |
| 47 | + Debug.Assert(_pos == _span.Length); |
| 48 | + int pos = _pos; |
| 49 | + Grow(1); |
| 50 | + _span[pos] = item; |
| 51 | + _pos = pos + 1; |
| 52 | + } |
| 53 | + |
| 54 | + public ReadOnlySpan<T> AsSpan() => _span.Slice(0, _pos); |
| 55 | + |
| 56 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 57 | + public void Dispose() |
| 58 | + { |
| 59 | + T[]? toReturn = _arrayFromPool; |
| 60 | + if (toReturn != null) |
| 61 | + { |
| 62 | + _arrayFromPool = null; |
| 63 | + ArrayPool<T>.Shared.Return(toReturn); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Note that consuming implementations depend on the list only growing if it's absolutely |
| 68 | + // required. If the list is already large enough to hold the additional items be added, |
| 69 | + // it must not grow. The list is used in a number of places where the reference is checked |
| 70 | + // and it's expected to match the initial reference provided to the constructor if that |
| 71 | + // span was sufficiently large. |
| 72 | + private void Grow(int additionalCapacityRequired = 1) |
| 73 | + { |
| 74 | + const int ArrayMaxLength = 0x7FFFFFC7; // same as Array.MaxLength |
| 75 | + |
| 76 | + // Double the size of the span. If it's currently empty, default to size 4, |
| 77 | + // although it'll be increased in Rent to the pool's minimum bucket size. |
| 78 | + int nextCapacity = Math.Max(_span.Length != 0 ? _span.Length * 2 : 4, _span.Length + additionalCapacityRequired); |
| 79 | + |
| 80 | + // If the computed doubled capacity exceeds the possible length of an array, then we |
| 81 | + // want to downgrade to either the maximum array length if that's large enough to hold |
| 82 | + // an additional item, or the current length + 1 if it's larger than the max length, in |
| 83 | + // which case it'll result in an OOM when calling Rent below. In the exceedingly rare |
| 84 | + // case where _span.Length is already int.MaxValue (in which case it couldn't be a managed |
| 85 | + // array), just use that same value again and let it OOM in Rent as well. |
| 86 | + if ((uint)nextCapacity > ArrayMaxLength) |
| 87 | + { |
| 88 | + nextCapacity = Math.Max(Math.Max(_span.Length + 1, ArrayMaxLength), _span.Length); |
| 89 | + } |
| 90 | + |
| 91 | + T[] array = ArrayPool<T>.Shared.Rent(nextCapacity); |
| 92 | + _span.CopyTo(array); |
| 93 | + |
| 94 | + T[]? toReturn = _arrayFromPool; |
| 95 | + _span = _arrayFromPool = array; |
| 96 | + if (toReturn != null) |
| 97 | + { |
| 98 | + ArrayPool<T>.Shared.Return(toReturn); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments