ReadOnlySpan.Fast.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.FromEnd ? _length - index.Value : index.Value;
  147. return ref this [actualIndex];
  148. }
  149. }
  150. public ReadOnlySpan<T> this[Range range]
  151. {
  152. get
  153. {
  154. int start = range.Start.FromEnd ? _length - range.Start.Value : range.Start.Value;
  155. int end = range.End.FromEnd ? _length - range.End.Value : range.End.Value;
  156. return Slice(start, end - start);
  157. }
  158. }
  159. /// <summary>
  160. /// Returns a reference to the 0th element of the Span. If the Span is empty, returns null reference.
  161. /// It can be used for pinning and is required to support the use of span within a fixed statement.
  162. /// </summary>
  163. [EditorBrowsable(EditorBrowsableState.Never)]
  164. public unsafe ref readonly T GetPinnableReference()
  165. {
  166. // Ensure that the native code has just one forward branch that is predicted-not-taken.
  167. ref T ret = ref Unsafe.AsRef<T>(null);
  168. if (_length != 0) ret = ref _pointer.Value;
  169. return ref ret;
  170. }
  171. /// <summary>
  172. /// Copies the contents of this read-only span into destination span. If the source
  173. /// and destinations overlap, this method behaves as if the original values in
  174. /// a temporary location before the destination is overwritten.
  175. ///
  176. /// <param name="destination">The span to copy items into.</param>
  177. /// <exception cref="System.ArgumentException">
  178. /// Thrown when the destination Span is shorter than the source Span.
  179. /// </exception>
  180. /// </summary>
  181. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  182. public void CopyTo(Span<T> destination)
  183. {
  184. // Using "if (!TryCopyTo(...))" results in two branches: one for the length
  185. // check, and one for the result of TryCopyTo. Since these checks are equivalent,
  186. // we can optimize by performing the check once ourselves then calling Memmove directly.
  187. if ((uint)_length <= (uint)destination.Length)
  188. {
  189. Buffer.Memmove(ref destination._pointer.Value, ref _pointer.Value, (nuint)_length);
  190. }
  191. else
  192. {
  193. ThrowHelper.ThrowArgumentException_DestinationTooShort();
  194. }
  195. }
  196. /// <summary>
  197. /// Copies the contents of this read-only span into destination span. If the source
  198. /// and destinations overlap, this method behaves as if the original values in
  199. /// a temporary location before the destination is overwritten.
  200. /// </summary>
  201. /// <returns>If the destination span is shorter than the source span, this method
  202. /// return false and no data is written to the destination.</returns>
  203. /// <param name="destination">The span to copy items into.</param>
  204. public bool TryCopyTo(Span<T> destination)
  205. {
  206. bool retVal = false;
  207. if ((uint)_length <= (uint)destination.Length)
  208. {
  209. Buffer.Memmove(ref destination._pointer.Value, ref _pointer.Value, (nuint)_length);
  210. retVal = true;
  211. }
  212. return retVal;
  213. }
  214. /// <summary>
  215. /// Returns true if left and right point at the same memory and have the same length. Note that
  216. /// this does *not* check to see if the *contents* are equal.
  217. /// </summary>
  218. public static bool operator ==(ReadOnlySpan<T> left, ReadOnlySpan<T> right)
  219. {
  220. return left._length == right._length && Unsafe.AreSame<T>(ref left._pointer.Value, ref right._pointer.Value);
  221. }
  222. /// <summary>
  223. /// For <see cref="ReadOnlySpan{Char}"/>, returns a new instance of string that represents the characters pointed to by the span.
  224. /// Otherwise, returns a <see cref="string"/> with the name of the type and the number of elements.
  225. /// </summary>
  226. public override string ToString()
  227. {
  228. if (typeof(T) == typeof(char))
  229. {
  230. unsafe
  231. {
  232. fixed (char* src = &Unsafe.As<T, char>(ref _pointer.Value))
  233. return new string(src, 0, _length);
  234. }
  235. }
  236. return string.Format("System.ReadOnlySpan<{0}>[{1}]", typeof(T).Name, _length);
  237. }
  238. /// <summary>
  239. /// Forms a slice out of the given read-only span, beginning at 'start'.
  240. /// </summary>
  241. /// <param name="start">The index at which to begin this slice.</param>
  242. /// <exception cref="System.ArgumentOutOfRangeException">
  243. /// Thrown when the specified <paramref name="start"/> index is not in range (&lt;0 or &gt;=Length).
  244. /// </exception>
  245. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  246. public ReadOnlySpan<T> Slice(int start)
  247. {
  248. if ((uint)start > (uint)_length)
  249. ThrowHelper.ThrowArgumentOutOfRangeException();
  250. return new ReadOnlySpan<T>(ref Unsafe.Add(ref _pointer.Value, start), _length - start);
  251. }
  252. /// <summary>
  253. /// Forms a slice out of the given read-only span, beginning at 'start', of given length
  254. /// </summary>
  255. /// <param name="start">The index at which to begin this slice.</param>
  256. /// <param name="length">The desired length for the slice (exclusive).</param>
  257. /// <exception cref="System.ArgumentOutOfRangeException">
  258. /// Thrown when the specified <paramref name="start"/> or end index is not in range (&lt;0 or &gt;=Length).
  259. /// </exception>
  260. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  261. public ReadOnlySpan<T> Slice(int start, int length)
  262. {
  263. #if BIT64
  264. // See comment in Span<T>.Slice for how this works.
  265. if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
  266. ThrowHelper.ThrowArgumentOutOfRangeException();
  267. #else
  268. if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
  269. ThrowHelper.ThrowArgumentOutOfRangeException();
  270. #endif
  271. return new ReadOnlySpan<T>(ref Unsafe.Add(ref _pointer.Value, start), length);
  272. }
  273. /// <summary>
  274. /// Copies the contents of this read-only span into a new array. This heap
  275. /// allocates, so should generally be avoided, however it is sometimes
  276. /// necessary to bridge the gap with APIs written in terms of arrays.
  277. /// </summary>
  278. public T[] ToArray()
  279. {
  280. if (_length == 0)
  281. return Array.Empty<T>();
  282. var destination = new T[_length];
  283. Buffer.Memmove(ref Unsafe.As<byte, T>(ref destination.GetRawSzArrayData()), ref _pointer.Value, (nuint)_length);
  284. return destination;
  285. }
  286. }
  287. }