Skip to content
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

2D System.Memory-like primitives (Span2D<T>, Memory2D<T>) #3353

Merged
Merged
Show file tree
Hide file tree
Changes from 202 commits
Commits
Show all changes
203 commits
Select commit Hold shift + click to select a range
2b80061
Added new constructors for Ref<T> and ReadOnlyRef<T>
Sergio0694 Jun 17, 2020
b89a83c
Added initial version of the Span2D<T> type
Sergio0694 Jun 17, 2020
d6980bc
Minor code tweaks to Span2D<T>
Sergio0694 Jun 17, 2020
eb1bb6e
Added initial version of the Memory2D<T> type
Sergio0694 Jun 17, 2020
f11b77d
Added missing Memory2D<T> header
Sergio0694 Jun 17, 2020
64a293c
Improved Span2D<T> layout (removed 4 bytes)
Sergio0694 Jun 17, 2020
4de84df
Minor code refactoring
Sergio0694 Jun 17, 2020
ad31cfe
Fixed a bug in T[,].AsSpan() on .NET Standard 2.1
Sergio0694 Jun 17, 2020
26e33db
Added some T[,,] extensions
Sergio0694 Jun 17, 2020
033055a
Added more Memory2D<T> constructors
Sergio0694 Jun 17, 2020
f0de8bb
Disabled Memory2D<T> before .NET Standard 2.1
Sergio0694 Jun 17, 2020
fdd75e8
Enabled Span2D<T> on all targets
Sergio0694 Jun 17, 2020
a572171
Added IsCovariant<T> extensions for arrays
Sergio0694 Jun 17, 2020
d6d50ea
Minor code refactoring
Sergio0694 Jun 17, 2020
20d3b11
Enabled Memory2D<T> on all targets
Sergio0694 Jun 17, 2020
1a7a27e
Reordered some APIs
Sergio0694 Jun 17, 2020
2de43c2
Draft for Memory2D<T>.Slice
Sergio0694 Jun 18, 2020
b3e757e
Span2D<T> refactoring, added Height/Width properties
Sergio0694 Jun 18, 2020
357f49e
Added ThrowHelper class for memory APIs
Sergio0694 Jun 18, 2020
98affab
Code refactoring using ThrowHelper
Sergio0694 Jun 18, 2020
d5991b0
Added Memory<T> constructor to Memory2D<T>
Sergio0694 Jun 18, 2020
a6ef300
More fixes to Memory2D<T>
Sergio0694 Jun 18, 2020
9942e4e
Added Memory2D<T> constructors with pitch
Sergio0694 Jun 18, 2020
8a246c8
Enabled Span2D<T>.TryGetSpan on .NET Standard < 2.1
Sergio0694 Jun 18, 2020
b52f573
Added Memory2D<T>.TryGetMemory
Sergio0694 Jun 18, 2020
09c0fc4
Minor code refactoring
Sergio0694 Jun 18, 2020
91f2f29
Implemented Span2D<T>.CopyTo(Span2D<T>)
Sergio0694 Jun 18, 2020
4d187c3
Implemented Span2D<T>.Slice API
Sergio0694 Jun 18, 2020
422cab9
Added [ReadOnly]RefEnumerable<T> types
Sergio0694 Jun 18, 2020
61be3b6
Added [ReadOnly]RefEnumerable<T>.ToArray()
Sergio0694 Jun 18, 2020
0413d80
Fixed a build error
Sergio0694 Jun 18, 2020
9f9dcfe
Refactored 2D array extensions with new enumerables
Sergio0694 Jun 18, 2020
5403daa
Added T[,].GetRowSpan extension
Sergio0694 Jun 18, 2020
9798477
Added Span2D<T>.Get[Row|Column] APIs
Sergio0694 Jun 18, 2020
6d194e4
Fixed new APIs on older runtimes
Sergio0694 Jun 18, 2020
925fefb
Minor code refactoring
Sergio0694 Jun 18, 2020
a422dbf
Tweaked some XML docs
Sergio0694 Jun 18, 2020
57094c2
Added T[,] array Memory2D<T> extensions
Sergio0694 Jun 18, 2020
025d9c8
Added T[,,].GetLayer extension
Sergio0694 Jun 18, 2020
aef42db
Code refactoring to Span2D<T>
Sergio0694 Jun 18, 2020
1aabc38
Added missing Span2D<T> constructors
Sergio0694 Jun 18, 2020
0cb7ec4
Added new Span[2D]<T> APIs for 2D/3D arrays
Sergio0694 Jun 18, 2020
5735f91
Added ReadOnly[Memory|Span]2D<T> types
Sergio0694 Jun 18, 2020
cdfee3e
Fixed a bug in Span2D<T>.Empty
Sergio0694 Jun 18, 2020
07cc99e
Fixed file headers
Sergio0694 Jun 18, 2020
71a0954
Bug fixes in Span2D<T> constructors
Sergio0694 Jun 18, 2020
d6df6a2
Added unit tests for Span2D<T> constructors
Sergio0694 Jun 18, 2020
5f0162d
Added new Span2D<T> T[,,] constructor and test
Sergio0694 Jun 18, 2020
0d61001
Added more Span2D<T> tests
Sergio0694 Jun 18, 2020
5f3368f
Added Span2D<T>.Slice tests
Sergio0694 Jun 18, 2020
744d8e9
Fixed RefEnumerable<T>.ToArray
Sergio0694 Jun 18, 2020
64d51c0
Finished Span2D<T> tests
Sergio0694 Jun 18, 2020
4cf78e4
Fixed bugs in ReadOnlyRefEnumerable<T>
Sergio0694 Jun 18, 2020
088bda6
Updated ReadOnlySpan2D<T> with changes from Span2D<T>
Sergio0694 Jun 18, 2020
c10665a
Fixed some leftover changes
Sergio0694 Jun 18, 2020
7755850
Added ReadOnlySpan2D<T> tests
Sergio0694 Jun 18, 2020
1dc46f1
Inverted height/width parameters for 2D memory types
Sergio0694 Jun 18, 2020
28360b9
Updated Memory2D<T> constructors
Sergio0694 Jun 18, 2020
568c993
Reordered params in Slice APIs
Sergio0694 Jun 18, 2020
26441a2
Added missing Memory2D<T> constructor
Sergio0694 Jun 18, 2020
9f406d3
Bug fixes, added Memory2D<T> tests
Sergio0694 Jun 19, 2020
be73d4e
Updated ReadOnlyMemory2D<T>
Sergio0694 Jun 19, 2020
1955933
Added ReadOnlySpan2D<T> string constructors
Sergio0694 Jun 19, 2020
1aebcb2
Reverted new APIs, added ReadOnlyMemory2D<T> tests
Sergio0694 Jun 19, 2020
c23dff9
Fixed tests/types on UWP
Sergio0694 Jun 19, 2020
d69d43a
Added [ReadOnly]Memory2D<T>.DangerousCreate
Sergio0694 Jun 19, 2020
261efd0
Removed redundant NullReferenceExceptions
Sergio0694 Jun 19, 2020
73d97ec
Removed unnecessary attributes
Sergio0694 Jun 19, 2020
d7dc9eb
Added [ReadOnly]Span2D<T>.DangerousGetReferenceAt
Sergio0694 Jun 19, 2020
6e35468
Added ParallelHelper APIs for new 2D types
Sergio0694 Jun 19, 2020
d7a1679
Fixed a bug in T[,] array memory/span constructors
Sergio0694 Jun 19, 2020
365bbc7
Added ParallelHelper.ForEach2D readonly tests
Sergio0694 Jun 19, 2020
a2007c2
Added ParallelHelper.ForEach2D ref tests
Sergio0694 Jun 19, 2020
3e68dd8
Added debug views for 2D memory types
Sergio0694 Jun 19, 2020
e260291
Simplified debug type proxies for memory types
Sergio0694 Jun 19, 2020
c46aba8
Removed more unnecessary debug proxy types
Sergio0694 Jun 19, 2020
1a6ff3e
Fixed a typo
Sergio0694 Jun 19, 2020
f8545ac
Tweaked [ReadOnly]Memory2D<T>.GetHashCode()
Sergio0694 Jun 19, 2020
73d7503
Renamed variables in unit tests
Sergio0694 Jun 19, 2020
4000d92
Bug fixes in the GetRow/GetColumn enumerators
Sergio0694 Jun 19, 2020
bb78804
Bug fixes in the ReadOnlyRefEnumerable<T>, more tests
Sergio0694 Jun 19, 2020
023aa20
Minor code tweaks
Sergio0694 Jun 19, 2020
544eedc
Added T[,,].GetRow/GetColumn APIs
Sergio0694 Jun 19, 2020
fb08781
Added new 3D array tests, minor refactoring
Sergio0694 Jun 19, 2020
998ec84
Improved codegen when throwing exceptions
Sergio0694 Jun 19, 2020
b64827e
Removed aaaaaaaaaaaa-s
Sergio0694 Jun 19, 2020
2431323
Added more comments for IsCovariant<T>() extension
Sergio0694 Jun 19, 2020
98c511a
Added XML note for [ReadOnly]RefEnumerable<T>.step
Sergio0694 Jun 19, 2020
e79c6a4
Fixed two typos
Sergio0694 Jun 20, 2020
5d6c69d
Readonly all the things!
Sergio0694 Jun 23, 2020
5dfd5d3
Minor optimization when wrapping [ReadOnly]Memory<T>
Sergio0694 Jun 23, 2020
87b8d23
Improved Memory<T> support in ReadOnlyMemory2D<T>
Sergio0694 Jun 23, 2020
5ee7662
Added string constructors in ReadOnlyMemory2D<T>
Sergio0694 Jun 23, 2020
6c682a9
Removed and fixed Memory<T> support in 2D memory
Sergio0694 Jun 23, 2020
e7823c6
Added support for string in Memory2D<T>
Sergio0694 Jun 23, 2020
77ec56a
Fixed initial offset with array segment from memory
Sergio0694 Jun 23, 2020
6696eb7
Improved codegen in enumerables
Sergio0694 Jul 13, 2020
9d9ec18
Improved codegen in refactored array extensions
Sergio0694 Jul 13, 2020
593fa47
Improved codegen in new ParallelHelper APIs
Sergio0694 Jul 13, 2020
22c4321
Improved codegen in [ReadOnly]Span2D<T> types
Sergio0694 Jul 13, 2020
1bd8e94
Speed improvements to Span2D<T> CopyTo/Fill/Clear methods
Sergio0694 Aug 15, 2020
4f145d6
Ported speed improvements to ReadOnlySpan2D<T>
Sergio0694 Aug 16, 2020
c818eb2
Improved handling of Memory<char>(string) inputs for Memory2D<T>
Sergio0694 Aug 16, 2020
f9fa7a3
Fixed two typos
Sergio0694 Aug 16, 2020
5be4959
Minor code refactoring
Sergio0694 Aug 17, 2020
b5f728f
Fixed build errors in the unit tests (refactoring)
Sergio0694 Aug 18, 2020
5ed38ff
Added missing nullability annotation
Sergio0694 Aug 19, 2020
6109476
Extended [ReadOnly]Span2D<T>(void*) support
Sergio0694 Aug 24, 2020
e0b9f8e
Added more unit tests for Span2D<T>(void*)
Sergio0694 Aug 24, 2020
52297bb
Added downlevel RuntimeHelpers support to HashCode<T>
Sergio0694 Aug 24, 2020
7790b99
Improved [ReadOnly]Memory2D<T>.Equals(object)
Sergio0694 Aug 24, 2020
f4ae789
Removed unnecessary generic type constraint
Sergio0694 Aug 27, 2020
70d5624
Minor code refactoring
Sergio0694 Sep 10, 2020
9a68e93
Minor code refactoring
Sergio0694 Sep 10, 2020
ef942b8
Switched Memory2D<T> constructor to MemoryManager<T>
Sergio0694 Sep 10, 2020
ec3c0ae
Fixed a bug in [ReadOnly]Memory2D<T>.TryGetMemory
Sergio0694 Sep 10, 2020
5d2c5aa
Added Memory<T>.AsMemory2D extensions
Sergio0694 Sep 10, 2020
a39669e
Added missing compiler directive
Sergio0694 Sep 10, 2020
6d25a63
Added MemoryManager<T> support to ReadOnlyMemory2D<T>
Sergio0694 Sep 10, 2020
fb1f189
Fixed build errors in unit tests
Sergio0694 Sep 10, 2020
401043d
Fixed ND array offsetting in portable version
Sergio0694 Oct 9, 2020
241a923
Added T[,]/T[,,].AsMemory() extensions
Sergio0694 Oct 9, 2020
aaf5fb3
Improved Memory2D<T>.TryGetMemory for ND arrays
Sergio0694 Oct 9, 2020
3562f73
Fixed unit tests
Sergio0694 Oct 9, 2020
6ddb4ee
Fixed bug in unit tests on UWP
Sergio0694 Oct 9, 2020
c4c92f6
Improved array indexing extensions in portable version
Sergio0694 Oct 10, 2020
ee4f26b
Minor code tweak
Sergio0694 Oct 10, 2020
29ca850
Fixed some catastrophic typos
Sergio0694 Oct 10, 2020
b265679
Added missing array extensions
Sergio0694 Oct 11, 2020
3b25d76
Bug fixes and speed improvement to T[].IsCovariant
Sergio0694 Oct 11, 2020
c2fe6d7
Improved validation in array extensions
Sergio0694 Oct 12, 2020
d998f26
Minor code refactoring
Sergio0694 Oct 12, 2020
b0e7df6
Improved unit test coverage
Sergio0694 Oct 12, 2020
000ed71
Tweaked nullability annotations to follow the BCL
Sergio0694 Oct 14, 2020
21928da
Removed T[,].Fill extension (replaced by Span2D<T>.Fill)
Sergio0694 Oct 14, 2020
f9a935c
Added missing Range/Index indexers
Sergio0694 Oct 14, 2020
3d75e3b
Changed names of Span2D<T> indexer parameters
Sergio0694 Oct 14, 2020
88ef25c
Added [ReadOnly]RefEnumerable<T> helper methods
Sergio0694 Oct 15, 2020
e7738f3
Added missing readonly modifiers
Sergio0694 Oct 15, 2020
957cb36
Codegen improvements in enumerator types
Sergio0694 Oct 15, 2020
8addab0
Bug fixes in [ReadOnly]RefEnumerable<T>.CopyTo
Sergio0694 Oct 16, 2020
212717d
Added [ReadOnly]RefEnumerable<T> tests
Sergio0694 Oct 16, 2020
d7a4ece
Removed unnecessary APIs
Sergio0694 Oct 17, 2020
a21c397
Renamed some APIs
Sergio0694 Oct 17, 2020
05e084f
Minor code tweaks
Sergio0694 Oct 17, 2020
6dc0a05
Added missing internal constructors
Sergio0694 Oct 17, 2020
a100345
Added [ReadOnly]Span<T>.AsSpan2D extensions
Sergio0694 Oct 17, 2020
ff61256
Added comment for GCHandle.Alloc exception
Sergio0694 Oct 19, 2020
0e65ef9
Fixed incorrect file name
Sergio0694 Oct 19, 2020
d505dca
Fixed [Memory|Span]2D<T>.IsEmpty property
Sergio0694 Oct 21, 2020
b40ac2a
Added OverflowHelper, enabled C# 9
Sergio0694 Oct 21, 2020
5c9344b
Added 32 bit overflow checks in constructors
Sergio0694 Oct 21, 2020
ea621fc
Switched (IntPtr)(void*) casts with (nint)
Sergio0694 Oct 21, 2020
a491369
Added Span<T>.DangerousGetReferenceAt nint overload
Sergio0694 Oct 21, 2020
41d0a4c
Fixed indexing computation in Span2D<T> types
Sergio0694 Oct 21, 2020
3636b3e
Fixed missing overflow check in constructor
Sergio0694 Oct 22, 2020
1b624e5
Fixed overflows in [ReadOnly]RefEnumerable<T>
Sergio0694 Oct 22, 2020
94bd6d1
Renamed Size property to Length
Sergio0694 Oct 22, 2020
c8563d7
Switched [Memory|Span]2D<T>.Length property to nint
Sergio0694 Oct 22, 2020
752a533
Improved support for large 2D/3D arrays
Sergio0694 Oct 22, 2020
c92ef0f
Merge pull request #29 from Sergio0694/feature/nint-offsetting
Sergio0694 Oct 22, 2020
54d0d64
Merge branch 'master' into feature/array3d-apis
Sergio0694 Oct 22, 2020
1136753
Added missing header
Sergio0694 Oct 22, 2020
cf85cc0
Fixed a bug in some RefEnumerable<T> methods
Sergio0694 Oct 22, 2020
9ae258d
Switched Count<T> implementation to nint
Sergio0694 Oct 23, 2020
00e2645
Code refactoring with IntPtr to nint
Sergio0694 Oct 23, 2020
3e8aae8
Minor codegen improvements
Sergio0694 Oct 23, 2020
fb6ae89
More codegen improvements
Sergio0694 Oct 23, 2020
4911a86
Increased count batch size on x64 for long/ulong values
Sergio0694 Oct 23, 2020
7d111d7
Updated C# version in .csproj
Sergio0694 Oct 23, 2020
9a10a5a
Minor codegen improvement
Sergio0694 Oct 24, 2020
e054aba
Minor code tweaks
Sergio0694 Oct 25, 2020
e9e8e7b
Updated unit tests
Sergio0694 Oct 25, 2020
b5f3ef5
Added missing overflow check, improved docs
Sergio0694 Oct 26, 2020
67ec240
15% speedup in Span2D<T>.DangerousGetReferenceAt
Sergio0694 Oct 26, 2020
b75f238
Added more comments, minor code refactoring
Sergio0694 Oct 28, 2020
0e8a2db
Added comments to DangerousGetReference tests
Sergio0694 Oct 29, 2020
a2ecb5f
Bug fixes, added RefEnumerable<T>.[Try]CopyFrom
Sergio0694 Oct 30, 2020
60e17b4
Added [ReadOnly]Span.[Try]CopyTo RefEnumerable APIs
Sergio0694 Oct 30, 2020
5497eb8
Added internal RefEnumerableHelper type
Sergio0694 Oct 30, 2020
e54a545
Code refactoring using the new helper
Sergio0694 Oct 30, 2020
7ab524d
Added RefEnumerableHelper.CopyTo 2-step overload
Sergio0694 Oct 30, 2020
ffc95e6
Minor code tweaks
Sergio0694 Oct 30, 2020
fabeba6
Added implicit ReadOnlyRefEnumerable operator
Sergio0694 Oct 30, 2020
bce507b
Removed unnecessary EditorBrowsable attributes
Sergio0694 Oct 30, 2020
1a15ee2
Added RefEnumerable<T>.CopyTo(RefEnumerable) APIs
Sergio0694 Oct 30, 2020
a90866d
Fixed a build error in the unit tests
Sergio0694 Oct 31, 2020
296e916
Tweaked some using directives
Sergio0694 Oct 31, 2020
14f40e2
Improved unit test coverage for RefEnumerable<T>
Sergio0694 Oct 31, 2020
2626c7d
Removed unnecessary covariance array checks
Sergio0694 Oct 31, 2020
8a70ace
Added comments to unit tests
Sergio0694 Oct 31, 2020
151f38a
Fixed bugs in unit tests
Sergio0694 Oct 31, 2020
20bd65f
Fixed bug in ReadOnlyRefEnumerable<T> implicit cast
Sergio0694 Oct 31, 2020
f3e7e00
Minor code refactoring
Sergio0694 Nov 1, 2020
bc6f70b
Minor code tweaks for clarity
Sergio0694 Nov 1, 2020
2745962
Merge branch 'master' into feature/array3d-apis
michael-hawker Nov 3, 2020
7a65a29
Added tests for off-by-one Get[Row|Column] for 2D arrays
Sergio0694 Nov 3, 2020
6bb9e5a
Improved some unit tests
Sergio0694 Nov 3, 2020
7b04c7d
Added more tests for invalid Memory2D<T> constructors
Sergio0694 Nov 3, 2020
7738d34
Fixed some typos
Sergio0694 Nov 3, 2020
cd21086
Fixed XML docs for internal helpers
Sergio0694 Nov 4, 2020
9c90f66
Updated package description with new types
Sergio0694 Nov 4, 2020
7e67a9c
Fixed StyleCop warnings in HighPerformance tests
Sergio0694 Nov 5, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Microsoft.Toolkit.HighPerformance.Buffers
/// the arrays in use are rented from the shared <see cref="ArrayPool{T}"/> instance,
/// and that <see cref="ArrayPoolBufferWriter{T}"/> is also available on .NET Standard 2.0.
/// </remarks>
[DebuggerTypeProxy(typeof(ArrayPoolBufferWriterDebugView<>))]
[DebuggerTypeProxy(typeof(MemoryDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class ArrayPoolBufferWriter<T> : IBuffer<T>, IMemoryOwner<T>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// 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.

#if SPAN_RUNTIME_SUPPORT

using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.Toolkit.HighPerformance.Extensions;

namespace Microsoft.Toolkit.HighPerformance.Buffers.Internals
{
/// <summary>
/// A custom <see cref="MemoryManager{T}"/> that can wrap arbitrary <see cref="object"/> instances.
/// </summary>
/// <typeparam name="T">The type of elements in the target memory area.</typeparam>
internal sealed class RawObjectMemoryManager<T> : MemoryManager<T>
{
/// <summary>
/// The target <see cref="object"/> instance.
/// </summary>
private readonly object instance;

/// <summary>
/// The initial offset within <see cref="instance"/>.
/// </summary>
private readonly IntPtr offset;

/// <summary>
/// The length of the target memory area.
/// </summary>
private readonly int length;

/// <summary>
/// Initializes a new instance of the <see cref="RawObjectMemoryManager{T}"/> class.
/// </summary>
/// <param name="instance">The target <see cref="object"/> instance.</param>
/// <param name="offset">The starting offset within <paramref name="instance"/>.</param>
/// <param name="length">The usable length within <paramref name="instance"/>.</param>
public RawObjectMemoryManager(object instance, IntPtr offset, int length)
{
this.instance = instance;
this.offset = offset;
this.length = length;
}

/// <inheritdoc/>
public override Span<T> GetSpan()
{
ref T r0 = ref this.instance.DangerousGetObjectDataReferenceAt<T>(this.offset);

return MemoryMarshal.CreateSpan(ref r0, this.length);
}

/// <inheritdoc/>
public override unsafe MemoryHandle Pin(int elementIndex = 0)
{
if ((uint)elementIndex >= (uint)this.length)
{
ThrowArgumentOutOfRangeExceptionForInvalidElementIndex();
}

// Allocating a pinned handle for the array with fail and throw an exception
// if the array contains non blittable data. This is the expected behavior and
// the same happens when trying to pin a Memory<T> instance obtained through
// traditional means (eg. via the implicit T[] array conversion), if T is a
// reference type or a type containing some references.
GCHandle handle = GCHandle.Alloc(this.instance, GCHandleType.Pinned);
ref T r0 = ref this.instance.DangerousGetObjectDataReferenceAt<T>(this.offset);
ref T r1 = ref Unsafe.Add(ref r0, (nint)(uint)elementIndex);
void* p = Unsafe.AsPointer(ref r1);

return new MemoryHandle(p, handle);
}

/// <inheritdoc/>
public override void Unpin()
{
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
}

/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the input index for <see cref="Pin"/> is not valid.
/// </summary>
private static void ThrowArgumentOutOfRangeExceptionForInvalidElementIndex()
{
throw new ArgumentOutOfRangeException("elementIndex", "The input element index was not in the valid range");
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.HighPerformance.Buffers.Views;

namespace Microsoft.Toolkit.HighPerformance.Buffers
{
Expand All @@ -20,7 +21,7 @@ namespace Microsoft.Toolkit.HighPerformance.Buffers
/// instances (or objects that can be converted to a <see cref="Memory{T}"/>), to ensure the data is written directly
/// to the intended buffer, with no possibility of doing additional allocations or expanding the available capacity.
/// </remarks>
[DebuggerTypeProxy(typeof(MemoryBufferWriter<>))]
[DebuggerTypeProxy(typeof(MemoryDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class MemoryBufferWriter<T> : IBuffer<T>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.Toolkit.HighPerformance.Buffers
/// An <see cref="IMemoryOwner{T}"/> implementation with an embedded length and a fast <see cref="Span{T}"/> accessor.
/// </summary>
/// <typeparam name="T">The type of items to store in the current instance.</typeparam>
[DebuggerTypeProxy(typeof(MemoryOwnerDebugView<>))]
[DebuggerTypeProxy(typeof(MemoryDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public sealed class MemoryOwner<T> : IMemoryOwner<T>
{
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Toolkit.HighPerformance/Buffers/SpanOwner{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Microsoft.Toolkit.HighPerformance.Buffers
/// Not doing so will cause the underlying buffer not to be returned to the shared pool.
/// </summary>
/// <typeparam name="T">The type of items to store in the current instance.</typeparam>
[DebuggerTypeProxy(typeof(SpanOwnerDebugView<>))]
[DebuggerTypeProxy(typeof(MemoryDebugView<>))]
[DebuggerDisplay("{ToString(),raw}")]
public readonly ref struct SpanOwner<T>
{
Expand Down
36 changes: 18 additions & 18 deletions Microsoft.Toolkit.HighPerformance/Buffers/StringPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ private unsafe ref string TryGet(ReadOnlySpan<char> span, int hashcode)
(uint)i < (uint)length;
i = entry.NextIndex)
{
entry = ref Unsafe.Add(ref mapEntriesRef, (IntPtr)(void*)(uint)i);
entry = ref Unsafe.Add(ref mapEntriesRef, (nint)(uint)i);

if (entry.HashCode == hashcode &&
entry.Value!.AsSpan().SequenceEqual(span))
Expand All @@ -556,7 +556,7 @@ private unsafe ref string TryGet(ReadOnlySpan<char> span, int hashcode)
/// <param name="value">The new <see cref="string"/> instance to store.</param>
/// <param name="hashcode">The precomputed hashcode for <paramref name="value"/>.</param>
[MethodImpl(MethodImplOptions.NoInlining)]
private unsafe void Insert(string value, int hashcode)
private void Insert(string value, int hashcode)
{
ref int bucketsRef = ref this.buckets.DangerousGetReference();
ref MapEntry mapEntriesRef = ref this.mapEntries.DangerousGetReference();
Expand All @@ -571,7 +571,7 @@ private unsafe void Insert(string value, int hashcode)
entryIndex = heapEntriesRef.MapIndex;
heapIndex = 0;

ref MapEntry removedEntry = ref Unsafe.Add(ref mapEntriesRef, (IntPtr)(void*)(uint)entryIndex);
ref MapEntry removedEntry = ref Unsafe.Add(ref mapEntriesRef, (nint)(uint)entryIndex);

// The removal logic can be extremely optimized in this case, as we
// can retrieve the precomputed hashcode for the target entry by doing
Expand All @@ -588,9 +588,9 @@ private unsafe void Insert(string value, int hashcode)
}

int bucketIndex = hashcode & (this.buckets.Length - 1);
ref int targetBucket = ref Unsafe.Add(ref bucketsRef, (IntPtr)(void*)(uint)bucketIndex);
ref MapEntry targetMapEntry = ref Unsafe.Add(ref mapEntriesRef, (IntPtr)(void*)(uint)entryIndex);
ref HeapEntry targetHeapEntry = ref Unsafe.Add(ref heapEntriesRef, (IntPtr)(void*)(uint)heapIndex);
ref int targetBucket = ref Unsafe.Add(ref bucketsRef, (nint)(uint)bucketIndex);
ref MapEntry targetMapEntry = ref Unsafe.Add(ref mapEntriesRef, (nint)(uint)entryIndex);
ref HeapEntry targetHeapEntry = ref Unsafe.Add(ref heapEntriesRef, (nint)(uint)heapIndex);

// Assign the values in the new map entry
targetMapEntry.HashCode = hashcode;
Expand All @@ -616,7 +616,7 @@ private unsafe void Insert(string value, int hashcode)
/// <param name="mapIndex">The index of the target map node to remove.</param>
/// <remarks>The input <see cref="string"/> instance needs to already exist in the map.</remarks>
[MethodImpl(MethodImplOptions.NoInlining)]
private unsafe void Remove(int hashcode, int mapIndex)
private void Remove(int hashcode, int mapIndex)
{
ref MapEntry mapEntriesRef = ref this.mapEntries.DangerousGetReference();
int
Expand All @@ -628,15 +628,15 @@ private unsafe void Remove(int hashcode, int mapIndex)
// value we're looking for is guaranteed to be present
while (true)
{
ref MapEntry candidate = ref Unsafe.Add(ref mapEntriesRef, (IntPtr)(void*)(uint)entryIndex);
ref MapEntry candidate = ref Unsafe.Add(ref mapEntriesRef, (nint)(uint)entryIndex);

// Check the current value for a match
if (entryIndex == mapIndex)
{
// If this was not the first list node, update the parent as well
if (lastIndex != EndOfList)
{
ref MapEntry lastEntry = ref Unsafe.Add(ref mapEntriesRef, (IntPtr)(void*)(uint)lastIndex);
ref MapEntry lastEntry = ref Unsafe.Add(ref mapEntriesRef, (nint)(uint)lastIndex);

lastEntry.NextIndex = candidate.NextIndex;
}
Expand All @@ -662,14 +662,14 @@ private unsafe void Remove(int hashcode, int mapIndex)
/// </summary>
/// <param name="heapIndex">The index of the target heap node to update.</param>
[MethodImpl(MethodImplOptions.NoInlining)]
private unsafe void UpdateTimestamp(ref int heapIndex)
private void UpdateTimestamp(ref int heapIndex)
{
int
currentIndex = heapIndex,
count = this.count;
ref MapEntry mapEntriesRef = ref this.mapEntries.DangerousGetReference();
ref HeapEntry heapEntriesRef = ref this.heapEntries.DangerousGetReference();
ref HeapEntry root = ref Unsafe.Add(ref heapEntriesRef, (IntPtr)(void*)(uint)currentIndex);
ref HeapEntry root = ref Unsafe.Add(ref heapEntriesRef, (nint)(uint)currentIndex);
uint timestamp = this.timestamp;

// Check if incrementing the current timestamp for the heap node to update
Expand Down Expand Up @@ -721,7 +721,7 @@ private unsafe void UpdateTimestamp(ref int heapIndex)
// Check and update the left child, if necessary
if (left < count)
{
ref HeapEntry child = ref Unsafe.Add(ref heapEntriesRef, (IntPtr)(void*)(uint)left);
ref HeapEntry child = ref Unsafe.Add(ref heapEntriesRef, (nint)(uint)left);

if (child.Timestamp < minimum.Timestamp)
{
Expand All @@ -733,7 +733,7 @@ private unsafe void UpdateTimestamp(ref int heapIndex)
// Same check as above for the right child
if (right < count)
{
ref HeapEntry child = ref Unsafe.Add(ref heapEntriesRef, (IntPtr)(void*)(uint)right);
ref HeapEntry child = ref Unsafe.Add(ref heapEntriesRef, (nint)(uint)right);

if (child.Timestamp < minimum.Timestamp)
{
Expand All @@ -752,8 +752,8 @@ private unsafe void UpdateTimestamp(ref int heapIndex)
}

// Update the indices in the respective map entries (accounting for the swap)
Unsafe.Add(ref mapEntriesRef, (IntPtr)(void*)(uint)root.MapIndex).HeapIndex = targetIndex;
Unsafe.Add(ref mapEntriesRef, (IntPtr)(void*)(uint)minimum.MapIndex).HeapIndex = currentIndex;
Unsafe.Add(ref mapEntriesRef, (nint)(uint)root.MapIndex).HeapIndex = targetIndex;
Unsafe.Add(ref mapEntriesRef, (nint)(uint)minimum.MapIndex).HeapIndex = currentIndex;

currentIndex = targetIndex;

Expand All @@ -764,7 +764,7 @@ private unsafe void UpdateTimestamp(ref int heapIndex)
minimum = temp;

// Update the reference to the root node
root = ref Unsafe.Add(ref heapEntriesRef, (IntPtr)(void*)(uint)currentIndex);
root = ref Unsafe.Add(ref heapEntriesRef, (nint)(uint)currentIndex);
}

Fallback:
Expand All @@ -787,14 +787,14 @@ private unsafe void UpdateTimestamp(ref int heapIndex)
/// a given number of nodes, those are all contiguous from the start of the array.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
private unsafe void UpdateAllTimestamps()
private void UpdateAllTimestamps()
{
int count = this.count;
ref HeapEntry heapEntriesRef = ref this.heapEntries.DangerousGetReference();

for (int i = 0; i < count; i++)
{
Unsafe.Add(ref heapEntriesRef, (IntPtr)(void*)(uint)i).Timestamp = (uint)i;
Unsafe.Add(ref heapEntriesRef, (nint)(uint)i).Timestamp = (uint)i;
}
}
}
Expand Down

This file was deleted.

This file was deleted.

Loading