ReadOnlySpan.Fast.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.Diagnostics;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.Versioning;
  7. using System.Text;
  8. using EditorBrowsableAttribute = System.ComponentModel.EditorBrowsableAttribute;
  9. using EditorBrowsableState = System.ComponentModel.EditorBrowsableState;
  10. using Internal.Runtime.CompilerServices;
  11. #pragma warning disable 0809 //warning CS0809: Obsolete member 'Span<T>.Equals(object)' overrides non-obsolete member 'object.Equals(object)'
  12. #if BIT64
  13. using nuint = System.UInt64;
  14. #else
  15. using nuint = System.UInt32;
  16. #endif
  17. namespace System
  18. {
  19. /// <summary>
  20. /// ReadOnlySpan represents a contiguous region of arbitrary memory. Unlike arrays, it can point to either managed
  21. /// or native memory, or to memory allocated on the stack. It is type- and memory-safe.
  22. /// </summary>
  23. [NonVersionable]
  24. public readonly ref partial struct ReadOnlySpan<T>
  25. {
  26. /// <summary>A byref or a native ptr.</summary>
  27. internal readonly ByReference<T> _pointer;
  28. /// <summary>The number of elements this ReadOnlySpan contains.</summary>
  29. #if PROJECTN
  30. [Bound]
  31. #endif
  32. private readonly int _length;
  33. /// <summary>
  34. /// Creates a new read-only span over the entirety of the target array.
  35. /// </summary>
  36. /// <param name="array">The target array.</param>
  37. /// <remarks>Returns default when <paramref name="array"/> is null.</remarks>
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. public ReadOnlySpan(T[]? array)
  40. {
  41. if (array == null)
  42. {
  43. this = default;
  44. return; // returns default
  45. }
  46. _pointer = new ByReference<T>(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()));
  47. _length = array.Length;
  48. }
  49. /// <summary>
  50. /// Creates a new read-only span over the portion of the target array beginning
  51. /// at 'start' index and ending at 'end' index (exclusive).
  52. /// </summary>
  53. /// <param name="array">The target array.</param>
  54. /// <param name="start">The index at which to begin the read-only span.</param>
  55. /// <param name="length">The number of items in the read-only span.</param>
  56. /// <remarks>Returns default when <paramref name="array"/> is null.</remarks>
  57. /// <exception cref="System.ArgumentOutOfRangeException">
  58. /// Thrown when the specified <paramref name="start"/> or end index is not in the range (&lt;0 or &gt;Length).
  59. /// </exception>
  60. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  61. public ReadOnlySpan(T[]? array, int start, int length)
  62. {
  63. if (array == null)
  64. {
  65. if (start != 0 || length != 0)
  66. ThrowHelper.ThrowArgumentOutOfRangeException();
  67. this = default;
  68. return; // returns default
  69. }
  70. #if BIT64
  71. // See comment in Span<T>.Slice for how this works.
  72. if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)array.Length)
  73. ThrowHelper.ThrowArgumentOutOfRangeException();
  74. #else
  75. if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
  76. ThrowHelper.ThrowArgumentOutOfRangeException();
  77. #endif
  78. _pointer = new ByReference<T>(ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), start));
  79. _length = length;
  80. }
  81. /// <summary>
  82. /// Creates a new read-only span over the target unmanaged buffer. Clearly this
  83. /// is quite dangerous, because we are creating arbitrarily typed T's
  84. /// out of a void*-typed block of memory. And the length is not checked.
  85. /// But if this creation is correct, then all subsequent uses are correct.
  86. /// </summary>
  87. /// <param name="pointer">An unmanaged pointer to memory.</param>
  88. /// <param name="length">The number of <typeparamref name="T"/> elements the memory contains.</param>
  89. /// <exception cref="System.ArgumentException">
  90. /// Thrown when <typeparamref name="T"/> is reference type or contains pointers and hence cannot be stored in unmanaged memory.
  91. /// </exception>
  92. /// <exception cref="System.ArgumentOutOfRangeException">
  93. /// Thrown when the specified <paramref name="length"/> is negative.
  94. /// </exception>
  95. [CLSCompliant(false)]
  96. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  97. public unsafe ReadOnlySpan(void* pointer, int length)
  98. {
  99. if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
  100. ThrowHelper.ThrowInvalidTypeWithPointersNotSupported(typeof(T));
  101. if (length < 0)
  102. ThrowHelper.ThrowArgumentOutOfRangeException();
  103. _pointer = new ByReference<T>(ref Unsafe.As<byte, T>(ref *(byte*)pointer));
  104. _length = length;
  105. }
  106. // Constructor for internal use only.
  107. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  108. internal ReadOnlySpan(ref T ptr, int length)
  109. {
  110. Debug.Assert(length >= 0);
  111. _pointer = new ByReference<T>(ref ptr);
  112. _length = length;
  113. }
  114. /// <summary>
  115. /// Returns the specified element of the read-only span.
  116. /// </summary>
  117. /// <param name="index"></param>
  118. /// <returns></returns>
  119. /// <exception cref="System.IndexOutOfRangeException">
  120. /// Thrown when index less than 0 or index greater than or equal to Length
  121. /// </exception>
  122. public ref readonly T this[int index]
  123. {
  124. #if PROJECTN
  125. [BoundsChecking]
  126. get
  127. {
  128. return ref Unsafe.Add(ref _pointer.Value, index);
  129. }
  130. #else
  131. [Intrinsic]
  132. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  133. [NonVersionable]
  134. get
  135. {
  136. if ((uint)index >= (uint)_length)
  137. ThrowHelper.ThrowIndexOutOfRangeException();
  138. return ref Unsafe.Add(ref _pointer.Value, index);
  139. }
  140. #endif
  141. }
  142. /// <summary>
  143. /// Returns a reference to the 0th element of the Span. If the Span is empty, returns null reference.
  144. /// It can be used for pinning and is required to support the use of span within a fixed statement.
  145. /// </summary>
  146. [EditorBrowsable(EditorBrowsableState.Never)]
  147. public unsafe ref readonly T GetPinnableReference()
  148. {
  149. // Ensure that the native code has just one forward branch that is predicted-not-taken.
  150. ref T ret = ref Unsafe.AsRef<T>(null);
  151. if (_length != 0) ret = ref _pointer.Value;
  152. return ref ret;
  153. }
  154. /// <summary>
  155. /// Copies the contents of this read-only span into destination span. If the source
  156. /// and destinations overlap, this method behaves as if the original values in
  157. /// a temporary location before the destination is overwritten.
  158. ///
  159. /// <param name="destination">The span to copy items into.</param>
  160. /// <exception cref="System.ArgumentException">
  161. /// Thrown when the destination Span is shorter than the source Span.
  162. /// </exception>
  163. /// </summary>
  164. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  165. public void CopyTo(Span<T> destination)
  166. {
  167. // Using "if (!TryCopyTo(...))" results in two branches: one for the length
  168. // check, and one for the result of TryCopyTo. Since these checks are equivalent,
  169. // we can optimize by performing the check once ourselves then calling Memmove directly.
  170. if ((uint)_length <= (uint)destination.Length)
  171. {
  172. Buffer.Memmove(ref destination._pointer.Value, ref _pointer.Value, (nuint)_length);
  173. }
  174. else
  175. {
  176. ThrowHelper.ThrowArgumentException_DestinationTooShort();
  177. }
  178. }
  179. /// <summary>
  180. /// Copies the contents of this read-only span into destination span. If the source
  181. /// and destinations overlap, this method behaves as if the original values in
  182. /// a temporary location before the destination is overwritten.
  183. /// </summary>
  184. /// <returns>If the destination span is shorter than the source span, this method
  185. /// return false and no data is written to the destination.</returns>
  186. /// <param name="destination">The span to copy items into.</param>
  187. public bool TryCopyTo(Span<T> destination)
  188. {
  189. bool retVal = false;
  190. if ((uint)_length <= (uint)destination.Length)
  191. {
  192. Buffer.Memmove(ref destination._pointer.Value, ref _pointer.Value, (nuint)_length);
  193. retVal = true;
  194. }
  195. return retVal;
  196. }
  197. /// <summary>
  198. /// Returns true if left and right point at the same memory and have the same length. Note that
  199. /// this does *not* check to see if the *contents* are equal.
  200. /// </summary>
  201. public static bool operator ==(ReadOnlySpan<T> left, ReadOnlySpan<T> right)
  202. {
  203. return left._length == right._length && Unsafe.AreSame<T>(ref left._pointer.Value, ref right._pointer.Value);
  204. }
  205. /// <summary>
  206. /// For <see cref="ReadOnlySpan{Char}"/>, returns a new instance of string that represents the characters pointed to by the span.
  207. /// Otherwise, returns a <see cref="string"/> with the name of the type and the number of elements.
  208. /// </summary>
  209. public override string ToString()
  210. {
  211. if (typeof(T) == typeof(char))
  212. {
  213. return new string(new ReadOnlySpan<char>(ref Unsafe.As<T, char>(ref _pointer.Value), _length));
  214. }
  215. #if FEATURE_UTF8STRING
  216. else if (typeof(T) == typeof(Char8))
  217. {
  218. // TODO_UTF8STRING: Call into optimized transcoding routine when it's available.
  219. return Encoding.UTF8.GetString(new ReadOnlySpan<byte>(ref Unsafe.As<T, byte>(ref _pointer.Value), _length));
  220. }
  221. #endif // FEATURE_UTF8STRING
  222. return string.Format("System.ReadOnlySpan<{0}>[{1}]", typeof(T).Name, _length);
  223. }
  224. /// <summary>
  225. /// Forms a slice out of the given read-only span, beginning at 'start'.
  226. /// </summary>
  227. /// <param name="start">The index at which to begin this slice.</param>
  228. /// <exception cref="System.ArgumentOutOfRangeException">
  229. /// Thrown when the specified <paramref name="start"/> index is not in range (&lt;0 or &gt;Length).
  230. /// </exception>
  231. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  232. public ReadOnlySpan<T> Slice(int start)
  233. {
  234. if ((uint)start > (uint)_length)
  235. ThrowHelper.ThrowArgumentOutOfRangeException();
  236. return new ReadOnlySpan<T>(ref Unsafe.Add(ref _pointer.Value, start), _length - start);
  237. }
  238. /// <summary>
  239. /// Forms a slice out of the given read-only span, beginning at 'start', of given length
  240. /// </summary>
  241. /// <param name="start">The index at which to begin this slice.</param>
  242. /// <param name="length">The desired length for the slice (exclusive).</param>
  243. /// <exception cref="System.ArgumentOutOfRangeException">
  244. /// Thrown when the specified <paramref name="start"/> or end index is not in range (&lt;0 or &gt;Length).
  245. /// </exception>
  246. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  247. public ReadOnlySpan<T> Slice(int start, int length)
  248. {
  249. #if BIT64
  250. // See comment in Span<T>.Slice for how this works.
  251. if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
  252. ThrowHelper.ThrowArgumentOutOfRangeException();
  253. #else
  254. if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
  255. ThrowHelper.ThrowArgumentOutOfRangeException();
  256. #endif
  257. return new ReadOnlySpan<T>(ref Unsafe.Add(ref _pointer.Value, start), length);
  258. }
  259. /// <summary>
  260. /// Copies the contents of this read-only span into a new array. This heap
  261. /// allocates, so should generally be avoided, however it is sometimes
  262. /// necessary to bridge the gap with APIs written in terms of arrays.
  263. /// </summary>
  264. public T[] ToArray()
  265. {
  266. if (_length == 0)
  267. return Array.Empty<T>();
  268. var destination = new T[_length];
  269. Buffer.Memmove(ref Unsafe.As<byte, T>(ref destination.GetRawSzArrayData()), ref _pointer.Value, (nuint)_length);
  270. return destination;
  271. }
  272. }
  273. }