Skip to content

Commit d105721

Browse files
committed
Replace Unsafe.NullRef<T>() with ref *(T*)null
1 parent 2b3e604 commit d105721

File tree

5 files changed

+12
-14
lines changed

5 files changed

+12
-14
lines changed

src/CommunityToolkit.HighPerformance/Buffers/StringPool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ public void Reset()
493493
private unsafe ref string TryGet(ReadOnlySpan<char> span, int hashcode)
494494
{
495495
ref MapEntry mapEntriesRef = ref this.mapEntries.DangerousGetReference();
496-
ref MapEntry entry = ref Unsafe.NullRef<MapEntry>();
496+
ref MapEntry entry = ref *(MapEntry*)null;
497497
int length = this.buckets.Length;
498498
int bucketIndex = hashcode & (length - 1);
499499

@@ -512,7 +512,7 @@ private unsafe ref string TryGet(ReadOnlySpan<char> span, int hashcode)
512512
}
513513
}
514514

515-
return ref Unsafe.NullRef<string>();
515+
return ref *(string*)null;
516516
}
517517

518518
/// <summary>

src/CommunityToolkit.HighPerformance/Extensions/NullableExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public static ref T DangerousGetValueOrDefaultReference<T>(this ref T? value)
5050
/// <returns>A reference to the value of the input <see cref="Nullable{T}"/> instance, or a <see langword="null"/> <typeparamref name="T"/> reference.</returns>
5151
/// <remarks>The returned reference can be tested for <see langword="null"/> using <see cref="Unsafe.IsNullRef{T}(ref T)"/>.</remarks>
5252
[MethodImpl(MethodImplOptions.AggressiveInlining)]
53-
public static ref T DangerousGetValueOrNullReference<T>(ref this T? value)
53+
public static unsafe ref T DangerousGetValueOrNullReference<T>(ref this T? value)
5454
where T : struct
5555
{
5656
#if NET7_0_OR_GREATER
57-
ref T resultRef = ref Unsafe.NullRef<T>();
57+
ref T resultRef = ref *(T*)null;
5858

5959
// This pattern ensures that the resulting code ends up having a single return, and a single
6060
// forward branch (the one where the value is null) that is predicted non taken. That is,
@@ -80,7 +80,7 @@ public static ref T DangerousGetValueOrNullReference<T>(ref this T? value)
8080
return ref Unsafe.As<T?, RawNullableData<T>>(ref value).Value;
8181
}
8282

83-
return ref Unsafe.NullRef<T>();
83+
return ref *(T*)null;
8484
#endif
8585
}
8686

src/CommunityToolkit.Mvvm/Messaging/Internals/System/Collections.Generic/Dictionary2.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,9 @@ public TValue GetValue()
340340
/// </summary>
341341
/// <param name="key">Key to look for.</param>
342342
/// <returns>Reference to the existing value.</returns>
343-
private ref TValue FindValue(TKey key)
343+
private unsafe ref TValue FindValue(TKey key)
344344
{
345-
ref Entry entry = ref Unsafe.NullRef<Entry>();
345+
ref Entry entry = ref *(Entry*)null;
346346
uint hashCode = (uint)key.GetHashCode();
347347
int i = GetBucket(hashCode);
348348
Entry[] entries = this.entries;
@@ -373,7 +373,7 @@ private ref TValue FindValue(TKey key)
373373
return ref value;
374374

375375
ReturnNotFound:
376-
value = ref Unsafe.NullRef<TValue>();
376+
value = ref *(TValue*)null;
377377

378378
goto Return;
379379
}

tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_ReadOnlyRefEnumerable{T}.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#if NET6_0_OR_GREATER
66

77
using System;
8-
using System.Runtime.CompilerServices;
98
using CommunityToolkit.HighPerformance.Enumerables;
109
using Microsoft.VisualStudio.TestTools.UnitTesting;
1110

@@ -41,9 +40,9 @@ public void Test_ReadOnlyRefEnumerable_DangerousCreate_Ok(int length, int step,
4140
[DataRow(10, -14)]
4241
[DataRow(-32, -1)]
4342
[ExpectedException(typeof(ArgumentOutOfRangeException))]
44-
public void Test_ReadOnlyRefEnumerable_DangerousCreate_BelowZero(int length, int step)
43+
public unsafe void Test_ReadOnlyRefEnumerable_DangerousCreate_BelowZero(int length, int step)
4544
{
46-
_ = ReadOnlyRefEnumerable<int>.DangerousCreate(in Unsafe.NullRef<int>(), length, step);
45+
_ = ReadOnlyRefEnumerable<int>.DangerousCreate(in *(int*)null, length, step);
4746
}
4847

4948
[TestMethod]

tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_RefEnumerable{T}.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#if NET6_0_OR_GREATER
66

77
using System;
8-
using System.Runtime.CompilerServices;
98
using CommunityToolkit.HighPerformance.Enumerables;
109
using Microsoft.VisualStudio.TestTools.UnitTesting;
1110

@@ -41,9 +40,9 @@ public void Test_RefEnumerable_DangerousCreate_Ok(int length, int step, int[] va
4140
[DataRow(10, -14)]
4241
[DataRow(-32, -1)]
4342
[ExpectedException(typeof(ArgumentOutOfRangeException))]
44-
public void Test_RefEnumerable_DangerousCreate_BelowZero(int length, int step)
43+
public unsafe void Test_RefEnumerable_DangerousCreate_BelowZero(int length, int step)
4544
{
46-
_ = RefEnumerable<int>.DangerousCreate(ref Unsafe.NullRef<int>(), length, step);
45+
_ = RefEnumerable<int>.DangerousCreate(ref *(int*)null, length, step);
4746
}
4847

4948
[TestMethod]

0 commit comments

Comments
 (0)