ArrayPoolBufferWriter{T}.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Buffers;
  6. using System.Runtime.CompilerServices;
  7. namespace CommunityToolkit.Mvvm.Messaging.Internals;
  8. /// <summary>
  9. /// A simple buffer writer implementation using pooled arrays.
  10. /// </summary>
  11. /// <typeparam name="T">The type of items to store in the list.</typeparam>
  12. /// <remarks>
  13. /// This type is a <see langword="ref"/> <see langword="struct"/> to avoid the object allocation and to
  14. /// enable the pattern-based <see cref="IDisposable"/> support. We aren't worried with consumers not
  15. /// using this type correctly since it's private and only accessible within the parent type.
  16. /// </remarks>
  17. internal ref struct ArrayPoolBufferWriter<T>
  18. {
  19. /// <summary>
  20. /// The default buffer size to use to expand empty arrays.
  21. /// </summary>
  22. private const int DefaultInitialBufferSize = 128;
  23. /// <summary>
  24. /// The underlying <typeparamref name="T"/> array.
  25. /// </summary>
  26. private T[] array;
  27. /// <summary>
  28. /// The span mapping to <see cref="array"/>.
  29. /// </summary>
  30. /// <remarks>All writes are done through this to avoid covariance checks.</remarks>
  31. private Span<T> span;
  32. /// <summary>
  33. /// The starting offset within <see cref="array"/>.
  34. /// </summary>
  35. private int index;
  36. /// <summary>
  37. /// Creates a new instance of the <see cref="ArrayPoolBufferWriter{T}"/> struct.
  38. /// </summary>
  39. /// <returns>A new <see cref="ArrayPoolBufferWriter{T}"/> instance.</returns>
  40. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  41. public static ArrayPoolBufferWriter<T> Create()
  42. {
  43. ArrayPoolBufferWriter<T> instance;
  44. instance.span = instance.array = ArrayPool<T>.Shared.Rent(DefaultInitialBufferSize);
  45. instance.index = 0;
  46. return instance;
  47. }
  48. /// <summary>
  49. /// Gets a <see cref="ReadOnlySpan{T}"/> with the current items.
  50. /// </summary>
  51. public ReadOnlySpan<T> Span
  52. {
  53. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54. get => this.span.Slice(0, this.index);
  55. }
  56. /// <summary>
  57. /// Adds a new item to the current collection.
  58. /// </summary>
  59. /// <param name="item">The item to add.</param>
  60. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  61. public void Add(T item)
  62. {
  63. Span<T> span = this.span;
  64. int index = this.index;
  65. if ((uint)index < (uint)span.Length)
  66. {
  67. span[index] = item;
  68. this.index = index + 1;
  69. }
  70. else
  71. {
  72. ResizeBufferAndAdd(item);
  73. }
  74. }
  75. /// <summary>
  76. /// Resets the underlying array and the stored items.
  77. /// </summary>
  78. public void Reset()
  79. {
  80. Array.Clear(this.array, 0, this.index);
  81. this.index = 0;
  82. }
  83. /// <summary>
  84. /// Resizes <see cref="array"/> when there is no space left for new items, then adds one
  85. /// </summary>
  86. /// <param name="item">The item to add.</param>
  87. [MethodImpl(MethodImplOptions.NoInlining)]
  88. private void ResizeBufferAndAdd(T item)
  89. {
  90. T[] rent = ArrayPool<T>.Shared.Rent(this.index << 2);
  91. Array.Copy(this.array, 0, rent, 0, this.index);
  92. Array.Clear(this.array, 0, this.index);
  93. ArrayPool<T>.Shared.Return(this.array);
  94. this.span = this.array = rent;
  95. this.span[this.index++] = item;
  96. }
  97. /// <inheritdoc cref="IDisposable.Dispose"/>
  98. public void Dispose()
  99. {
  100. Array.Clear(this.array, 0, this.index);
  101. ArrayPool<T>.Shared.Return(this.array);
  102. }
  103. }