Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 008c119

Browse files
authored
Update uses of AsBytes to use MemoryMarshal.AsBytes (#28460)
* Update uses of AsBytes to use MemoryMarshal.AsBytes * Cleanup commented out code * Remove AsBytes from MemoryExtensions Portable
1 parent 6ca628d commit 008c119

File tree

7 files changed

+16
-58
lines changed

7 files changed

+16
-58
lines changed

src/Common/src/CoreLib/System/Globalization/CompareInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@ internal static unsafe int GetIgnoreCaseHash(string source)
12491249
int charsWritten = source.AsSpan().ToUpperInvariant(span);
12501250

12511251
// Slice the array to the size returned by ToUpperInvariant.
1252-
int hash = Marvin.ComputeHash32(span.Slice(0, charsWritten).AsBytes(), Marvin.DefaultSeed);
1252+
int hash = Marvin.ComputeHash32(MemoryMarshal.AsBytes(span.Slice(0, charsWritten)), Marvin.DefaultSeed);
12531253

12541254
// Return the borrowed array if necessary.
12551255
if (borrowedArr != null)

src/System.Memory/ref/System.Memory.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace System
99
{
1010
public static partial class MemoryExtensions
1111
{
12-
public static System.ReadOnlySpan<byte> AsBytes<T>(this System.ReadOnlySpan<T> span) where T : struct { throw null; }
13-
public static System.Span<byte> AsBytes<T>(this System.Span<T> span) where T : struct { throw null; }
1412
public static System.Memory<T> AsMemory<T>(this System.ArraySegment<T> segment) { throw null; }
1513
public static System.Memory<T> AsMemory<T>(this System.ArraySegment<T> segment, int start) { throw null; }
1614
public static System.Memory<T> AsMemory<T>(this System.ArraySegment<T> segment, int start, int length) { throw null; }

src/System.Memory/src/System/MemoryExtensions.Portable.cs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -273,50 +273,6 @@ public static bool StartsWith(this ReadOnlySpan<char> span, ReadOnlySpan<char> v
273273
return sourceString.StartsWith(valueString, comparisonType);
274274
}
275275

276-
/// <summary>
277-
/// Casts a Span of one primitive type <typeparamref name="T"/> to Span of bytes.
278-
/// That type may not contain pointers or references. This is checked at runtime in order to preserve type safety.
279-
/// </summary>
280-
/// <param name="span">The source slice, of type <typeparamref name="T"/>.</param>
281-
/// <exception cref="System.ArgumentException">
282-
/// Thrown when <typeparamref name="T"/> contains pointers.
283-
/// </exception>
284-
/// <exception cref="System.OverflowException">
285-
/// Thrown if the Length property of the new Span would exceed Int32.MaxValue.
286-
/// </exception>
287-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
288-
public static Span<byte> AsBytes<T>(this Span<T> span)
289-
where T : struct
290-
{
291-
if (SpanHelpers.IsReferenceOrContainsReferences<T>())
292-
ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T));
293-
294-
int newLength = checked(span.Length * Unsafe.SizeOf<T>());
295-
return new Span<byte>(Unsafe.As<Pinnable<byte>>(span.Pinnable), span.ByteOffset, newLength);
296-
}
297-
298-
/// <summary>
299-
/// Casts a ReadOnlySpan of one primitive type <typeparamref name="T"/> to ReadOnlySpan of bytes.
300-
/// That type may not contain pointers or references. This is checked at runtime in order to preserve type safety.
301-
/// </summary>
302-
/// <param name="span">The source slice, of type <typeparamref name="T"/>.</param>
303-
/// <exception cref="System.ArgumentException">
304-
/// Thrown when <typeparamref name="T"/> contains pointers.
305-
/// </exception>
306-
/// <exception cref="System.OverflowException">
307-
/// Thrown if the Length property of the new Span would exceed Int32.MaxValue.
308-
/// </exception>
309-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
310-
public static ReadOnlySpan<byte> AsBytes<T>(this ReadOnlySpan<T> span)
311-
where T : struct
312-
{
313-
if (SpanHelpers.IsReferenceOrContainsReferences<T>())
314-
ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T));
315-
316-
int newLength = checked(span.Length * Unsafe.SizeOf<T>());
317-
return new ReadOnlySpan<byte>(Unsafe.As<Pinnable<byte>>(span.Pinnable), span.ByteOffset, newLength);
318-
}
319-
320276
/// <summary>
321277
/// Creates a new readonly span over the portion of the target string.
322278
/// </summary>

src/System.Memory/tests/Performance/Perf.Span.IndexOf.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Runtime.InteropServices;
56
using Microsoft.Xunit.Performance;
67
using Xunit;
78

@@ -44,7 +45,7 @@ public void SpanIndexOfCharAsBytes(int size)
4445
{
4546
Span<char> charSpan = new char[size];
4647
charSpan[size / 2] = '5';
47-
Span<byte> byteSpan = charSpan.AsBytes();
48+
Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan);
4849

4950
int index = 0;
5051
foreach (BenchmarkIteration iteration in Benchmark.Iterations)
@@ -120,7 +121,7 @@ public void SpanLastIndexOfCharAsBytes(int size)
120121
{
121122
Span<char> charSpan = new char[size];
122123
charSpan[size / 2] = '5';
123-
Span<byte> byteSpan = charSpan.AsBytes();
124+
Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan);
124125

125126
int index = 0;
126127
foreach (BenchmarkIteration iteration in Benchmark.Iterations)

src/System.Memory/tests/Performance/Perf.Span.IndexOfAny.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Runtime.InteropServices;
56
using Microsoft.Xunit.Performance;
67
using Xunit;
78

@@ -44,7 +45,7 @@ public void SpanIndexOfAnyCharAsBytes_Two(int size)
4445
{
4546
Span<char> charSpan = new char[size];
4647
charSpan[size / 2] = '5';
47-
Span<byte> byteSpan = charSpan.AsBytes();
48+
Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan);
4849

4950
int index = 0;
5051
foreach (BenchmarkIteration iteration in Benchmark.Iterations)
@@ -118,7 +119,7 @@ public void SpanIndexOfAnyCharAsBytes_Three(int size)
118119
{
119120
Span<char> charSpan = new char[size];
120121
charSpan[size / 2] = '5';
121-
Span<byte> byteSpan = charSpan.AsBytes();
122+
Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan);
122123

123124
int index = 0;
124125
foreach (BenchmarkIteration iteration in Benchmark.Iterations)
@@ -193,7 +194,7 @@ public void SpanIndexOfAnyCharAsBytes_Many(int size)
193194
{
194195
Span<char> charSpan = new char[size];
195196
charSpan[size / 2] = '5';
196-
Span<byte> byteSpan = charSpan.AsBytes();
197+
Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan);
197198
ReadOnlySpan<byte> values = new ReadOnlySpan<byte>(new byte[] { 53, 54, 55, 56 }); // '5' = 53
198199

199200
int index = 0;
@@ -219,7 +220,7 @@ public void SpanIndexOfAnyCharAsBytes_NoSearchValue_Many(int size)
219220
{
220221
Span<char> charSpan = new char[size];
221222
charSpan[size / 2] = '5';
222-
Span<byte> byteSpan = charSpan.AsBytes();
223+
Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan);
223224
ReadOnlySpan<byte> values = new ReadOnlySpan<byte>(new byte[] { 54, 55, 56, 57 }); // '5' = 53
224225

225226
int index = 0;
@@ -245,7 +246,7 @@ public void SpanIndexOfAnyCharAsBytes_ContainsLastSearchValue_Many(int size)
245246
{
246247
Span<char> charSpan = new char[size];
247248
charSpan[size / 2] = '5';
248-
Span<byte> byteSpan = charSpan.AsBytes();
249+
Span<byte> byteSpan = MemoryMarshal.AsBytes(charSpan);
249250
ReadOnlySpan<byte> values = new ReadOnlySpan<byte>(new byte[] { 54, 55, 56, 53 }); // '5' = 53
250251

251252
int index = 0;

src/System.Net.Primitives/src/System/Net/IPAddress.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics;
77
using System.Net.Sockets;
88
using System.Runtime.CompilerServices;
9+
using System.Runtime.InteropServices;
910

1011
namespace System.Net
1112
{
@@ -610,7 +611,7 @@ public override int GetHashCode()
610611
const int addressAndScopeIdLength = IPAddressParserStatics.IPv6AddressBytes + sizeof(uint);
611612
Span<byte> addressAndScopeIdSpan = stackalloc byte[addressAndScopeIdLength];
612613

613-
new ReadOnlySpan<ushort>(_numbers).AsBytes().CopyTo(addressAndScopeIdSpan);
614+
MemoryMarshal.AsBytes(new ReadOnlySpan<ushort>(_numbers)).CopyTo(addressAndScopeIdSpan);
614615
Span<byte> scopeIdSpan = addressAndScopeIdSpan.Slice(IPAddressParserStatics.IPv6AddressBytes);
615616
bool scopeWritten = BitConverter.TryWriteBytes(scopeIdSpan, _addressOrScopeId);
616617
Debug.Assert(scopeWritten);
@@ -626,7 +627,7 @@ public override int GetHashCode()
626627

627628
// For IPv4 addresses, we use Marvin on the integer representation of the Address.
628629
hashCode = Marvin.ComputeHash32(
629-
addressOrScopeIdSpan.AsBytes(),
630+
MemoryMarshal.AsBytes(addressOrScopeIdSpan),
630631
Marvin.DefaultSeed);
631632
}
632633

src/System.Private.Xml/src/System/Xml/NameTable.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Runtime.InteropServices;
67

78
namespace System.Xml
89
{
@@ -234,13 +235,13 @@ private static bool TextEquals(string str1, char[] str2, int str2Start, int str2
234235

235236
private static int ComputeHash32(string key)
236237
{
237-
ReadOnlySpan<byte> bytes = key.AsSpan().AsBytes();
238+
ReadOnlySpan<byte> bytes = MemoryMarshal.AsBytes(key.AsSpan());
238239
return Marvin.ComputeHash32(bytes, Marvin.DefaultSeed);
239240
}
240241

241242
private static int ComputeHash32(char[] key, int start, int len)
242243
{
243-
ReadOnlySpan<byte> bytes = key.AsSpan(start, len).AsBytes();
244+
ReadOnlySpan<byte> bytes = MemoryMarshal.AsBytes(key.AsSpan(start, len));
244245
return Marvin.ComputeHash32(bytes, Marvin.DefaultSeed);
245246
}
246247
}

0 commit comments

Comments
 (0)