New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize MemoryMarshal.ToEnumerable for arrays and strings #89274
Merged
Conversation
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
Member
stephentoub
commented
Jul 20, 2023
Method | Toolchain | Length | Mean | Ratio | Allocated | Alloc Ratio |
---|---|---|---|---|---|---|
CountArrayLINQ | \main\corerun.exe | 1 | 32.808 ns | 1.00 | 64 B | 1.00 |
CountArrayLINQ | \pr\corerun.exe | 1 | 9.272 ns | 0.28 | - | 0.00 |
CountArray | \main\corerun.exe | 1 | 21.079 ns | 1.00 | 64 B | 1.00 |
CountArray | \pr\corerun.exe | 1 | 13.257 ns | 0.63 | 32 B | 0.50 |
CountArraySlice | \main\corerun.exe | 1 | 17.804 ns | 1.00 | 64 B | 1.00 |
CountArraySlice | \pr\corerun.exe | 1 | 5.121 ns | 0.29 | - | 0.00 |
CountNonArray | \main\corerun.exe | 1 | 23.013 ns | 1.00 | 64 B | 1.00 |
CountNonArray | \pr\corerun.exe | 1 | 24.884 ns | 1.08 | 64 B | 1.00 |
CountArrayLINQ | \main\corerun.exe | 1000 | 3,129.629 ns | 1.000 | 64 B | 1.00 |
CountArrayLINQ | \pr\corerun.exe | 1000 | 9.484 ns | 0.003 | - | 0.00 |
CountArray | \main\corerun.exe | 1000 | 3,627.104 ns | 1.00 | 64 B | 1.00 |
CountArray | \pr\corerun.exe | 1000 | 1,400.046 ns | 0.41 | 32 B | 0.50 |
CountArraySlice | \main\corerun.exe | 1000 | 3,579.278 ns | 1.00 | 64 B | 1.00 |
CountArraySlice | \pr\corerun.exe | 1000 | 1,920.087 ns | 0.54 | 64 B | 1.00 |
CountNonArray | \main\corerun.exe | 1000 | 5,087.494 ns | 1.00 | 64 B | 1.00 |
CountNonArray | \pr\corerun.exe | 1000 | 4,562.180 ns | 0.90 | 64 B | 1.00 |
Tagging subscribers to this area: @dotnet/area-system-memory Issue Details
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
BenchmarkSwitcher.FromAssembly(typeof(Tests).Assembly).Run(args);
[MemoryDiagnoser(false)]
public class Tests
{
private Memory<char> _array;
private Memory<char> _arraySlice;
private Memory<char> _nonArray;
[Params(1, 1000)]
public int Length { get; set; }
[GlobalSetup]
public void Setup()
{
_array = Enumerable.Repeat('a', Length).ToArray();
_arraySlice = _array.Slice(1);
_nonArray = new WrapperMemoryManager<char>(_array).Memory;
}
[Benchmark]
public int CountArrayLINQ() => Enumerable.Count(MemoryMarshal.ToEnumerable<char>(_array));
[Benchmark]
public int CountArray() => Count(MemoryMarshal.ToEnumerable<char>(_array));
[Benchmark]
public int CountArraySlice() => Count(MemoryMarshal.ToEnumerable<char>(_arraySlice));
[Benchmark]
public int CountNonArray() => Count(MemoryMarshal.ToEnumerable<char>(_nonArray));
private static int Count<T>(IEnumerable<T> source)
{
int count = 0;
foreach (T item in source) count++;
return count;
}
private sealed class WrapperMemoryManager<T>(Memory<T> memory) : MemoryManager<T>
{
public override Span<T> GetSpan() => memory.Span;
public override MemoryHandle Pin(int elementIndex = 0) => throw new NotSupportedException();
public override void Unpin() => throw new NotSupportedException();
protected override void Dispose(bool disposing) { }
}
}
|
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs
Show resolved
Hide resolved
stephentoub
force-pushed
the
toenumerable
branch
from
July 21, 2023 01:16
2026239
to
ae32686
Compare
hamarb123
reviewed
Jul 21, 2023
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs
Show resolved
Hide resolved
eiriktsarpalis
approved these changes
Jul 21, 2023
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.