This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix MemoryManager ctor, add unit and perf tests, and improve performa…
…nce (#28880) * Fix MemoryManager ctor, add unit and perf tests, and improve performance. * Remove Dangerous Span Ctor * Fix sort order in csproj and rename Perf.MemorySlice.cs to Perf.Memory.Slice * Fix MemoryManager ctor and use internal span ctor to improve performance (#17452) * Fix MemoryManager ctor, add unit and perf tests, and use internal span ctor. * Address PR feedback (remove use of Unsafe.As and Dangerous Span Ctor) Signed-off-by: dotnet-bot-corefx-mirror <[email protected]>
- Loading branch information
Showing
9 changed files
with
264 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,6 +185,7 @@ internal enum ExceptionArgument | |
startIndex, | ||
endIndex, | ||
array, | ||
culture | ||
culture, | ||
manager | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
166 changes: 166 additions & 0 deletions
166
src/System.Memory/tests/Performance/Perf.Memory.Span.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Buffers; | ||
using System.MemoryTests; | ||
using Microsoft.Xunit.Performance; | ||
using Xunit; | ||
|
||
namespace System.Memory.Tests | ||
{ | ||
public class Perf_Memory_Span | ||
{ | ||
private const int InnerCount = 1_000_000; | ||
|
||
[Benchmark(InnerIterationCount = InnerCount)] | ||
public static void SpanFromDefaultIntegerMemory() | ||
{ | ||
Memory<int> memory = default; | ||
|
||
foreach (BenchmarkIteration iteration in Benchmark.Iterations) | ||
{ | ||
using (iteration.StartMeasurement()) | ||
{ | ||
for (int i = 0; i < Benchmark.InnerIterationCount; i++) | ||
{ | ||
Span<int> span = memory.Span; | ||
} | ||
} | ||
} | ||
} | ||
|
||
[Benchmark(InnerIterationCount = InnerCount)] | ||
public static void SpanFromIntegerArrayBackedMemory() | ||
{ | ||
int[] a = { 91, 92, -93, 94 }; | ||
var memory = new Memory<int>(a); | ||
|
||
foreach (BenchmarkIteration iteration in Benchmark.Iterations) | ||
{ | ||
using (iteration.StartMeasurement()) | ||
{ | ||
for (int i = 0; i < Benchmark.InnerIterationCount; i++) | ||
{ | ||
Span<int> span = memory.Span; | ||
} | ||
} | ||
} | ||
} | ||
|
||
[Benchmark(InnerIterationCount = InnerCount)] | ||
public static void SpanFromCharArrayBackedMemory() | ||
{ | ||
char[] a = "9192-9394".ToCharArray(); | ||
var memory = new Memory<char>(a); | ||
|
||
foreach (BenchmarkIteration iteration in Benchmark.Iterations) | ||
{ | ||
using (iteration.StartMeasurement()) | ||
{ | ||
for (int i = 0; i < Benchmark.InnerIterationCount; i++) | ||
{ | ||
Span<char> span = memory.Span; | ||
} | ||
} | ||
} | ||
} | ||
|
||
[Benchmark(InnerIterationCount = InnerCount)] | ||
public static void SpanFromObjectArrayBackedMemory() | ||
{ | ||
object o1 = new object(); | ||
object o2 = new object(); | ||
object[] a = { o1, o2 }; | ||
var memory = new Memory<object>(a); | ||
|
||
foreach (BenchmarkIteration iteration in Benchmark.Iterations) | ||
{ | ||
using (iteration.StartMeasurement()) | ||
{ | ||
for (int i = 0; i < Benchmark.InnerIterationCount; i++) | ||
{ | ||
Span<object> span = memory.Span; | ||
} | ||
} | ||
} | ||
} | ||
|
||
[Benchmark(InnerIterationCount = InnerCount)] | ||
public static void SpanFromStringBackedMemory() | ||
{ | ||
string a = "9192-9394"; | ||
ReadOnlyMemory<char> memory = a.AsMemory(); | ||
|
||
foreach (BenchmarkIteration iteration in Benchmark.Iterations) | ||
{ | ||
using (iteration.StartMeasurement()) | ||
{ | ||
for (int i = 0; i < Benchmark.InnerIterationCount; i++) | ||
{ | ||
ReadOnlySpan<char> span = memory.Span; | ||
} | ||
} | ||
} | ||
} | ||
|
||
[Benchmark(InnerIterationCount = InnerCount)] | ||
public static void SpanFromIntegerMemoryManager() | ||
{ | ||
int[] a = { 91, 92, -93, 94 }; | ||
var memory = new Memory<int>(a); | ||
MemoryManager<int> manager = new CustomMemoryForTest<int>(a); | ||
|
||
foreach (BenchmarkIteration iteration in Benchmark.Iterations) | ||
{ | ||
using (iteration.StartMeasurement()) | ||
{ | ||
for (int i = 0; i < Benchmark.InnerIterationCount; i++) | ||
{ | ||
Span<int> span = manager.Memory.Span; | ||
} | ||
} | ||
} | ||
} | ||
|
||
[Benchmark(InnerIterationCount = InnerCount)] | ||
public static void SpanFromCharMemoryManager() | ||
{ | ||
char[] a = "9192-9394".ToCharArray(); | ||
var memory = new Memory<char>(a); | ||
MemoryManager<char> manager = new CustomMemoryForTest<char>(a); | ||
|
||
foreach (BenchmarkIteration iteration in Benchmark.Iterations) | ||
{ | ||
using (iteration.StartMeasurement()) | ||
{ | ||
for (int i = 0; i < Benchmark.InnerIterationCount; i++) | ||
{ | ||
Span<char> span = manager.Memory.Span; | ||
} | ||
} | ||
} | ||
} | ||
|
||
[Benchmark(InnerIterationCount = InnerCount)] | ||
public static void SpanFromObjectMemoryManager() | ||
{ | ||
object o1 = new object(); | ||
object o2 = new object(); | ||
object[] a = { o1, o2 }; | ||
var memory = new Memory<object>(a); | ||
MemoryManager<object> manager = new CustomMemoryForTest<object>(a); | ||
|
||
foreach (BenchmarkIteration iteration in Benchmark.Iterations) | ||
{ | ||
using (iteration.StartMeasurement()) | ||
{ | ||
for (int i = 0; i < Benchmark.InnerIterationCount; i++) | ||
{ | ||
Span<object> span = manager.Memory.Span; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters