Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions src/CommunityToolkit.HighPerformance/Buffers/StringPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public void Reset()
private unsafe ref string TryGet(ReadOnlySpan<char> span, int hashcode)
{
ref MapEntry mapEntriesRef = ref this.mapEntries.DangerousGetReference();
ref MapEntry entry = ref Unsafe.NullRef<MapEntry>();
ref MapEntry entry = ref *(MapEntry*)null;
int length = this.buckets.Length;
int bucketIndex = hashcode & (length - 1);

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

return ref Unsafe.NullRef<string>();
return ref *(string*)null;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public static ref T DangerousGetValueOrDefaultReference<T>(this ref T? value)
/// <returns>A reference to the value of the input <see cref="Nullable{T}"/> instance, or a <see langword="null"/> <typeparamref name="T"/> reference.</returns>
/// <remarks>The returned reference can be tested for <see langword="null"/> using <see cref="Unsafe.IsNullRef{T}(ref T)"/>.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T DangerousGetValueOrNullReference<T>(ref this T? value)
public static unsafe ref T DangerousGetValueOrNullReference<T>(ref this T? value)
where T : struct
{
#if NET7_0_OR_GREATER
ref T resultRef = ref Unsafe.NullRef<T>();
ref T resultRef = ref *(T*)null;

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

return ref Unsafe.NullRef<T>();
return ref *(T*)null;
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,9 @@ public TValue GetValue()
/// </summary>
/// <param name="key">Key to look for.</param>
/// <returns>Reference to the existing value.</returns>
private ref TValue FindValue(TKey key)
private unsafe ref TValue FindValue(TKey key)
{
ref Entry entry = ref Unsafe.NullRef<Entry>();
ref Entry entry = ref *(Entry*)null;
uint hashCode = (uint)key.GetHashCode();
int i = GetBucket(hashCode);
Entry[] entries = this.entries;
Expand Down Expand Up @@ -373,7 +373,7 @@ private ref TValue FindValue(TKey key)
return ref value;

ReturnNotFound:
value = ref Unsafe.NullRef<TValue>();
value = ref *(TValue*)null;

goto Return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#if NET6_0_OR_GREATER

using System;
using System.Runtime.CompilerServices;
using CommunityToolkit.HighPerformance.Enumerables;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -41,9 +40,9 @@ public void Test_ReadOnlyRefEnumerable_DangerousCreate_Ok(int length, int step,
[DataRow(10, -14)]
[DataRow(-32, -1)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Test_ReadOnlyRefEnumerable_DangerousCreate_BelowZero(int length, int step)
public unsafe void Test_ReadOnlyRefEnumerable_DangerousCreate_BelowZero(int length, int step)
{
_ = ReadOnlyRefEnumerable<int>.DangerousCreate(in Unsafe.NullRef<int>(), length, step);
_ = ReadOnlyRefEnumerable<int>.DangerousCreate(in *(int*)null, length, step);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#if NET6_0_OR_GREATER

using System;
using System.Runtime.CompilerServices;
using CommunityToolkit.HighPerformance.Enumerables;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -41,9 +40,9 @@ public void Test_RefEnumerable_DangerousCreate_Ok(int length, int step, int[] va
[DataRow(10, -14)]
[DataRow(-32, -1)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Test_RefEnumerable_DangerousCreate_BelowZero(int length, int step)
public unsafe void Test_RefEnumerable_DangerousCreate_BelowZero(int length, int step)
{
_ = RefEnumerable<int>.DangerousCreate(ref Unsafe.NullRef<int>(), length, step);
_ = RefEnumerable<int>.DangerousCreate(ref *(int*)null, length, step);
}

[TestMethod]
Expand Down