Skip to content

Commit 1ba2321

Browse files
author
msftbot[bot]
authored
Simplified namespaces in HighPerformance package (#3687)
<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 --> <!-- 📝 It is preferred if you keep the "☑️ Allow edits by maintainers" checked in the Pull Request Template as it increases collaboration with the Toolkit maintainers by permitting commits to your PR branch (only) created from your fork. This can let us quickly make fixes for minor typos or forgotten StyleCop issues during review without needing to wait on you doing extra work. Let us help you help us! 🎉 --> ## Related to #3422 <!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. --> <!-- Add a brief overview here of the feature/bug & fix. --> ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> - Refactoring - Improvements <!-- - Bugfix --> <!-- - Feature --> <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## Overview Following the suggestion from @mrlacey for non-breaking changes, this PR moves the new `Span2D<T>` and `Memory2D<T>` types introduced in version 7.0 to the root `Microsoft.Toolkit.HighPerformance` namespace (instead of `.Memory`). This is simpler to use for consumers, it makes sense given that the root namespace already contains some other "primitive" types, and it's also more consistent to how the `Span<T>` and `Memory<T>` types are included in the `System.Memory` package, but are still located in the root `System` namespace. This PR also includes some minor codegen improvements to some other extensions, while I was at it 😄 ## PR Checklist Please check if your PR fulfills the following requirements: - [X] Tested code with current [supported SDKs](../readme.md#supported) - [ ] ~~Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link -->~~ - [ ] ~~Sample in sample app has been added / updated (for bug fixes / features)~~ - [ ] ~~Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets)~~ - [X] New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/windows-toolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc... - [X] Tests for the changes have been added (for bug fixes / features) (if applicable) - [X] Header has been added to all new source files (run *build/UpdateHeaders.bat*) - [X] Contains **NO** breaking changes
2 parents 75c8719 + 39be4e1 commit 1ba2321

23 files changed

+42
-49
lines changed

Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#endif
1212
using Microsoft.Toolkit.HighPerformance.Enumerables;
1313
using Microsoft.Toolkit.HighPerformance.Helpers.Internals;
14-
using Microsoft.Toolkit.HighPerformance.Memory;
1514
using RuntimeHelpers = Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
1615

1716
namespace Microsoft.Toolkit.HighPerformance.Extensions

Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.Toolkit.HighPerformance.Buffers.Internals;
1111
#endif
1212
using Microsoft.Toolkit.HighPerformance.Helpers.Internals;
13-
using Microsoft.Toolkit.HighPerformance.Memory;
1413
using RuntimeHelpers = Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
1514

1615
namespace Microsoft.Toolkit.HighPerformance.Extensions

Microsoft.Toolkit.HighPerformance/Extensions/BoolExtensions.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ public static class BoolExtensions
2323
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2424
public static unsafe byte ToByte(this bool flag)
2525
{
26-
return *(byte*)&flag;
26+
// Whenever we need to take the address of an argument, we make a local copy first.
27+
// This will be removed by the JIT anyway, but it can help produce better codegen and
28+
// remove unwanted stack spills if the caller is using constant arguments. This is
29+
// because taking the address of an argument can interfere with some of the flow
30+
// analysis executed by the JIT, which can in some cases block constant propagation.
31+
bool copy = flag;
32+
33+
return *(byte*)&copy;
2734
}
2835

2936
/// <summary>
@@ -58,7 +65,8 @@ public static unsafe int ToInt(this bool flag)
5865
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5966
public static unsafe int ToBitwiseMask32(this bool flag)
6067
{
61-
byte rangeFlag = *(byte*)&flag;
68+
bool copy = flag;
69+
byte rangeFlag = *(byte*)&copy;
6270
int
6371
negativeFlag = rangeFlag - 1,
6472
mask = ~negativeFlag;
@@ -77,7 +85,8 @@ public static unsafe int ToBitwiseMask32(this bool flag)
7785
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7886
public static unsafe long ToBitwiseMask64(this bool flag)
7987
{
80-
byte rangeFlag = *(byte*)&flag;
88+
bool copy = flag;
89+
byte rangeFlag = *(byte*)&copy;
8190
long
8291
negativeFlag = (long)rangeFlag - 1,
8392
mask = ~negativeFlag;

Microsoft.Toolkit.HighPerformance/Extensions/MemoryExtensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
using System.IO;
88
using System.Runtime.CompilerServices;
99
using System.Runtime.InteropServices;
10-
#if SPAN_RUNTIME_SUPPORT
11-
using Microsoft.Toolkit.HighPerformance.Memory;
12-
#endif
1310
using MemoryStream = Microsoft.Toolkit.HighPerformance.Streams.MemoryStream;
1411

1512
namespace Microsoft.Toolkit.HighPerformance.Extensions

Microsoft.Toolkit.HighPerformance/Extensions/ReadOnlyMemoryExtensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
using System.Runtime.InteropServices;
1111
using Microsoft.Toolkit.HighPerformance.Buffers.Internals;
1212
using Microsoft.Toolkit.HighPerformance.Buffers.Internals.Interfaces;
13-
#if SPAN_RUNTIME_SUPPORT
14-
using Microsoft.Toolkit.HighPerformance.Memory;
15-
#endif
1613
using MemoryStream = Microsoft.Toolkit.HighPerformance.Streams.MemoryStream;
1714

1815
namespace Microsoft.Toolkit.HighPerformance.Extensions

Microsoft.Toolkit.HighPerformance/Extensions/ReadOnlySpanExtensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
using System.Runtime.InteropServices;
99
using Microsoft.Toolkit.HighPerformance.Enumerables;
1010
using Microsoft.Toolkit.HighPerformance.Helpers.Internals;
11-
#if SPAN_RUNTIME_SUPPORT
12-
using Microsoft.Toolkit.HighPerformance.Memory;
13-
#endif
1411

1512
namespace Microsoft.Toolkit.HighPerformance.Extensions
1613
{

Microsoft.Toolkit.HighPerformance/Extensions/SpanExtensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
using System.Runtime.InteropServices;
99
using Microsoft.Toolkit.HighPerformance.Enumerables;
1010
using Microsoft.Toolkit.HighPerformance.Helpers.Internals;
11-
#if SPAN_RUNTIME_SUPPORT
12-
using Microsoft.Toolkit.HighPerformance.Memory;
13-
#endif
1411

1512
namespace Microsoft.Toolkit.HighPerformance.Extensions
1613
{

Microsoft.Toolkit.HighPerformance/Helpers/BitHelper.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ public static unsafe uint SetFlag(uint value, int n, bool flag)
209209
// and perform an OR with the resulting value of the previous
210210
// operation. This will always guaranteed to work, thanks to the
211211
// initial code clearing that bit before setting it again.
212+
bool copy = flag;
212213
uint
213-
flag32 = *(byte*)&flag,
214+
flag32 = *(byte*)&copy,
214215
shift = flag32 << n,
215216
or = and | shift;
216217

@@ -378,8 +379,9 @@ public static unsafe ulong SetFlag(ulong value, int n, bool flag)
378379
ulong
379380
bit = 1ul << n,
380381
not = ~bit,
381-
and = value & not,
382-
flag64 = *(byte*)&flag,
382+
and = value & not;
383+
bool copy = flag;
384+
ulong flag64 = *(byte*)&copy,
383385
shift = flag64 << n,
384386
or = and | shift;
385387

Microsoft.Toolkit.HighPerformance/Helpers/ParallelHelper.ForEach.IInAction2D.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System;
66
using System.Runtime.CompilerServices;
77
using System.Threading.Tasks;
8-
using Microsoft.Toolkit.HighPerformance.Memory;
98

109
namespace Microsoft.Toolkit.HighPerformance.Helpers
1110
{

Microsoft.Toolkit.HighPerformance/Helpers/ParallelHelper.ForEach.IRefAction2D.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System;
66
using System.Runtime.CompilerServices;
77
using System.Threading.Tasks;
8-
using Microsoft.Toolkit.HighPerformance.Memory;
98

109
namespace Microsoft.Toolkit.HighPerformance.Helpers
1110
{

0 commit comments

Comments
 (0)