ReadOnlySpan.Fast.cs 14 KB

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