2
0

Span.Fast.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. /// Span 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 Span<T>
  24. {
  25. /// <summary>A byref or a native ptr.</summary>
  26. internal readonly ByReference<T> _pointer;
  27. /// <summary>The number of elements this Span contains.</summary>
  28. #if PROJECTN
  29. [Bound]
  30. #endif
  31. private readonly int _length;
  32. /// <summary>
  33. /// Creates a new 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. /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception>
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. public Span(T[] array)
  40. {
  41. if (array == null)
  42. {
  43. this = default;
  44. return; // returns default
  45. }
  46. if (default(T) == null && array.GetType() != typeof(T[]))
  47. ThrowHelper.ThrowArrayTypeMismatchException();
  48. _pointer = new ByReference<T>(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()));
  49. _length = array.Length;
  50. }
  51. /// <summary>
  52. /// Creates a new span over the portion of the target array beginning
  53. /// at 'start' index and ending at 'end' index (exclusive).
  54. /// </summary>
  55. /// <param name="array">The target array.</param>
  56. /// <param name="start">The index at which to begin the span.</param>
  57. /// <param name="length">The number of items in the span.</param>
  58. /// <remarks>Returns default when <paramref name="array"/> is null.</remarks>
  59. /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception>
  60. /// <exception cref="System.ArgumentOutOfRangeException">
  61. /// Thrown when the specified <paramref name="start"/> or end index is not in the range (&lt;0 or &gt;=Length).
  62. /// </exception>
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. public Span(T[] array, int start, int length)
  65. {
  66. if (array == null)
  67. {
  68. if (start != 0 || length != 0)
  69. ThrowHelper.ThrowArgumentOutOfRangeException();
  70. this = default;
  71. return; // returns default
  72. }
  73. if (default(T) == null && array.GetType() != typeof(T[]))
  74. ThrowHelper.ThrowArrayTypeMismatchException();
  75. #if BIT64
  76. // See comment in Span<T>.Slice for how this works.
  77. if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)array.Length)
  78. ThrowHelper.ThrowArgumentOutOfRangeException();
  79. #else
  80. if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
  81. ThrowHelper.ThrowArgumentOutOfRangeException();
  82. #endif
  83. _pointer = new ByReference<T>(ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), start));
  84. _length = length;
  85. }
  86. /// <summary>
  87. /// Creates a new span over the target unmanaged buffer. Clearly this
  88. /// is quite dangerous, because we are creating arbitrarily typed T's
  89. /// out of a void*-typed block of memory. And the length is not checked.
  90. /// But if this creation is correct, then all subsequent uses are correct.
  91. /// </summary>
  92. /// <param name="pointer">An unmanaged pointer to memory.</param>
  93. /// <param name="length">The number of <typeparamref name="T"/> elements the memory contains.</param>
  94. /// <exception cref="System.ArgumentException">
  95. /// Thrown when <typeparamref name="T"/> is reference type or contains pointers and hence cannot be stored in unmanaged memory.
  96. /// </exception>
  97. /// <exception cref="System.ArgumentOutOfRangeException">
  98. /// Thrown when the specified <paramref name="length"/> is negative.
  99. /// </exception>
  100. [CLSCompliant(false)]
  101. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  102. public unsafe Span(void* pointer, int length)
  103. {
  104. if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
  105. ThrowHelper.ThrowInvalidTypeWithPointersNotSupported(typeof(T));
  106. if (length < 0)
  107. ThrowHelper.ThrowArgumentOutOfRangeException();
  108. _pointer = new ByReference<T>(ref Unsafe.As<byte, T>(ref *(byte*)pointer));
  109. _length = length;
  110. }
  111. // Constructor for internal use only.
  112. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  113. internal Span(ref T ptr, int length)
  114. {
  115. Debug.Assert(length >= 0);
  116. _pointer = new ByReference<T>(ref ptr);
  117. _length = length;
  118. }
  119. /// <summary>
  120. /// Returns a reference to specified element of the Span.
  121. /// </summary>
  122. /// <param name="index"></param>
  123. /// <returns></returns>
  124. /// <exception cref="System.IndexOutOfRangeException">
  125. /// Thrown when index less than 0 or index greater than or equal to Length
  126. /// </exception>
  127. public ref T this[int index]
  128. {
  129. #if PROJECTN
  130. [BoundsChecking]
  131. get
  132. {
  133. return ref Unsafe.Add(ref _pointer.Value, index);
  134. }
  135. #else
  136. [Intrinsic]
  137. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  138. [NonVersionable]
  139. get
  140. {
  141. if ((uint)index >= (uint)_length)
  142. ThrowHelper.ThrowIndexOutOfRangeException();
  143. return ref Unsafe.Add(ref _pointer.Value, index);
  144. }
  145. #endif
  146. }
  147. public ref T this[Index index]
  148. {
  149. get
  150. {
  151. // Evaluate the actual index first because it helps performance
  152. int actualIndex = index.GetOffset(_length);
  153. return ref this [actualIndex];
  154. }
  155. }
  156. public Span<T> this[Range range] => Slice(range);
  157. /// <summary>
  158. /// Returns a reference to the 0th element of the Span. If the Span is empty, returns null reference.
  159. /// It can be used for pinning and is required to support the use of span within a fixed statement.
  160. /// </summary>
  161. [EditorBrowsable(EditorBrowsableState.Never)]
  162. public unsafe ref T GetPinnableReference()
  163. {
  164. // Ensure that the native code has just one forward branch that is predicted-not-taken.
  165. ref T ret = ref Unsafe.AsRef<T>(null);
  166. if (_length != 0) ret = ref _pointer.Value;
  167. return ref ret;
  168. }
  169. /// <summary>
  170. /// Clears the contents of this span.
  171. /// </summary>
  172. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  173. public void Clear()
  174. {
  175. if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
  176. {
  177. SpanHelpers.ClearWithReferences(ref Unsafe.As<T, IntPtr>(ref _pointer.Value), (nuint)_length * (nuint)(Unsafe.SizeOf<T>() / sizeof(nuint)));
  178. }
  179. else
  180. {
  181. SpanHelpers.ClearWithoutReferences(ref Unsafe.As<T, byte>(ref _pointer.Value), (nuint)_length * (nuint)Unsafe.SizeOf<T>());
  182. }
  183. }
  184. /// <summary>
  185. /// Fills the contents of this span with the given value.
  186. /// </summary>
  187. public void Fill(T value)
  188. {
  189. if (Unsafe.SizeOf<T>() == 1)
  190. {
  191. uint length = (uint)_length;
  192. if (length == 0)
  193. return;
  194. T tmp = value; // Avoid taking address of the "value" argument. It would regress performance of the loop below.
  195. Unsafe.InitBlockUnaligned(ref Unsafe.As<T, byte>(ref _pointer.Value), Unsafe.As<T, byte>(ref tmp), length);
  196. }
  197. else
  198. {
  199. // Do all math as nuint to avoid unnecessary 64->32->64 bit integer truncations
  200. nuint length = (uint)_length;
  201. if (length == 0)
  202. return;
  203. ref T r = ref _pointer.Value;
  204. // TODO: Create block fill for value types of power of two sizes e.g. 2,4,8,16
  205. nuint elementSize = (uint)Unsafe.SizeOf<T>();
  206. nuint i = 0;
  207. for (; i < (length & ~(nuint)7); i += 8)
  208. {
  209. Unsafe.AddByteOffset<T>(ref r, (i + 0) * elementSize) = value;
  210. Unsafe.AddByteOffset<T>(ref r, (i + 1) * elementSize) = value;
  211. Unsafe.AddByteOffset<T>(ref r, (i + 2) * elementSize) = value;
  212. Unsafe.AddByteOffset<T>(ref r, (i + 3) * elementSize) = value;
  213. Unsafe.AddByteOffset<T>(ref r, (i + 4) * elementSize) = value;
  214. Unsafe.AddByteOffset<T>(ref r, (i + 5) * elementSize) = value;
  215. Unsafe.AddByteOffset<T>(ref r, (i + 6) * elementSize) = value;
  216. Unsafe.AddByteOffset<T>(ref r, (i + 7) * elementSize) = value;
  217. }
  218. if (i < (length & ~(nuint)3))
  219. {
  220. Unsafe.AddByteOffset<T>(ref r, (i + 0) * elementSize) = value;
  221. Unsafe.AddByteOffset<T>(ref r, (i + 1) * elementSize) = value;
  222. Unsafe.AddByteOffset<T>(ref r, (i + 2) * elementSize) = value;
  223. Unsafe.AddByteOffset<T>(ref r, (i + 3) * elementSize) = value;
  224. i += 4;
  225. }
  226. for (; i < length; i++)
  227. {
  228. Unsafe.AddByteOffset<T>(ref r, i * elementSize) = value;
  229. }
  230. }
  231. }
  232. /// <summary>
  233. /// Copies the contents of this span into destination span. If the source
  234. /// and destinations overlap, this method behaves as if the original values in
  235. /// a temporary location before the destination is overwritten.
  236. /// </summary>
  237. /// <param name="destination">The span to copy items into.</param>
  238. /// <exception cref="System.ArgumentException">
  239. /// Thrown when the destination Span is shorter than the source Span.
  240. /// </exception>
  241. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  242. public void CopyTo(Span<T> destination)
  243. {
  244. // Using "if (!TryCopyTo(...))" results in two branches: one for the length
  245. // check, and one for the result of TryCopyTo. Since these checks are equivalent,
  246. // we can optimize by performing the check once ourselves then calling Memmove directly.
  247. if ((uint)_length <= (uint)destination.Length)
  248. {
  249. Buffer.Memmove(ref destination._pointer.Value, ref _pointer.Value, (nuint)_length);
  250. }
  251. else
  252. {
  253. ThrowHelper.ThrowArgumentException_DestinationTooShort();
  254. }
  255. }
  256. /// <summary>
  257. /// Copies the contents of this span into destination span. If the source
  258. /// and destinations overlap, this method behaves as if the original values in
  259. /// a temporary location before the destination is overwritten.
  260. /// </summary>
  261. /// <param name="destination">The span to copy items into.</param>
  262. /// <returns>If the destination span is shorter than the source span, this method
  263. /// return false and no data is written to the destination.</returns>
  264. public bool TryCopyTo(Span<T> destination)
  265. {
  266. bool retVal = false;
  267. if ((uint)_length <= (uint)destination.Length)
  268. {
  269. Buffer.Memmove(ref destination._pointer.Value, ref _pointer.Value, (nuint)_length);
  270. retVal = true;
  271. }
  272. return retVal;
  273. }
  274. /// <summary>
  275. /// Returns true if left and right point at the same memory and have the same length. Note that
  276. /// this does *not* check to see if the *contents* are equal.
  277. /// </summary>
  278. public static bool operator ==(Span<T> left, Span<T> right)
  279. {
  280. return left._length == right._length && Unsafe.AreSame<T>(ref left._pointer.Value, ref right._pointer.Value);
  281. }
  282. /// <summary>
  283. /// Defines an implicit conversion of a <see cref="Span{T}"/> to a <see cref="ReadOnlySpan{T}"/>
  284. /// </summary>
  285. public static implicit operator ReadOnlySpan<T>(Span<T> span) => new ReadOnlySpan<T>(ref span._pointer.Value, span._length);
  286. /// <summary>
  287. /// For <see cref="Span{Char}"/>, returns a new instance of string that represents the characters pointed to by the span.
  288. /// Otherwise, returns a <see cref="string"/> with the name of the type and the number of elements.
  289. /// </summary>
  290. public override string ToString()
  291. {
  292. if (typeof(T) == typeof(char))
  293. {
  294. unsafe
  295. {
  296. fixed (char* src = &Unsafe.As<T, char>(ref _pointer.Value))
  297. return new string(src, 0, _length);
  298. }
  299. }
  300. return string.Format("System.Span<{0}>[{1}]", typeof(T).Name, _length);
  301. }
  302. /// <summary>
  303. /// Forms a slice out of the given span, beginning at 'start'.
  304. /// </summary>
  305. /// <param name="start">The index at which to begin this slice.</param>
  306. /// <exception cref="System.ArgumentOutOfRangeException">
  307. /// Thrown when the specified <paramref name="start"/> index is not in range (&lt;0 or &gt;=Length).
  308. /// </exception>
  309. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  310. public Span<T> Slice(int start)
  311. {
  312. if ((uint)start > (uint)_length)
  313. ThrowHelper.ThrowArgumentOutOfRangeException();
  314. return new Span<T>(ref Unsafe.Add(ref _pointer.Value, start), _length - start);
  315. }
  316. /// <summary>
  317. /// Forms a slice out of the given span, beginning at 'start', of given length
  318. /// </summary>
  319. /// <param name="start">The index at which to begin this slice.</param>
  320. /// <param name="length">The desired length for the slice (exclusive).</param>
  321. /// <exception cref="System.ArgumentOutOfRangeException">
  322. /// Thrown when the specified <paramref name="start"/> or end index is not in range (&lt;0 or &gt;=Length).
  323. /// </exception>
  324. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  325. public Span<T> Slice(int start, int length)
  326. {
  327. #if BIT64
  328. // Since start and length are both 32-bit, their sum can be computed across a 64-bit domain
  329. // without loss of fidelity. The cast to uint before the cast to ulong ensures that the
  330. // extension from 32- to 64-bit is zero-extending rather than sign-extending. The end result
  331. // of this is that if either input is negative or if the input sum overflows past Int32.MaxValue,
  332. // that information is captured correctly in the comparison against the backing _length field.
  333. // We don't use this same mechanism in a 32-bit process due to the overhead of 64-bit arithmetic.
  334. if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
  335. ThrowHelper.ThrowArgumentOutOfRangeException();
  336. #else
  337. if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
  338. ThrowHelper.ThrowArgumentOutOfRangeException();
  339. #endif
  340. return new Span<T>(ref Unsafe.Add(ref _pointer.Value, start), length);
  341. }
  342. /// <summary>
  343. /// Forms a slice out of the given span, beginning at 'startIndex'
  344. /// </summary>
  345. /// <param name="startIndex">The index at which to begin this slice.</param>
  346. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  347. public Span<T> Slice(Index startIndex)
  348. {
  349. int actualIndex = startIndex.GetOffset(_length);
  350. return Slice(actualIndex);
  351. }
  352. /// <summary>
  353. /// Forms a slice out of the given span, beginning at range start index to the range end
  354. /// </summary>
  355. /// <param name="range">The range which has the start and end indexes used to slice the span.</param>
  356. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  357. public Span<T> Slice(Range range)
  358. {
  359. (int start, int length) = range.GetOffsetAndLength(_length);
  360. return new Span<T>(ref Unsafe.Add(ref _pointer.Value, start), length);
  361. }
  362. /// <summary>
  363. /// Copies the contents of this span into a new array. This heap
  364. /// allocates, so should generally be avoided, however it is sometimes
  365. /// necessary to bridge the gap with APIs written in terms of arrays.
  366. /// </summary>
  367. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  368. public T[] ToArray()
  369. {
  370. if (_length == 0)
  371. return Array.Empty<T>();
  372. var destination = new T[_length];
  373. Buffer.Memmove(ref Unsafe.As<byte, T>(ref destination.GetRawSzArrayData()), ref _pointer.Value, (nuint)_length);
  374. return destination;
  375. }
  376. }
  377. }