Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Oct 15, 2019
1 parent cb70550 commit 996524e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ internal static ref T NullRef<T>()
{
#if CORECLR
throw new PlatformNotSupportedException();
// ldnull
// ldc.i4.0
// conv.u
// ret
#else
return ref Unsafe.AsRef<T>(null);
Expand All @@ -424,27 +425,6 @@ internal static bool IsNullRef<T>(ref T source)
// ret
#else
return Unsafe.AsPointer(ref source) == null;
#endif
}

/// <summary>
/// Returns if a given by-ref of type <typeparamref name="T"/> is not a null reference.
/// Note this is the address of the by-ref not what the ref points to.
/// </summary>
[Intrinsic]
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsNotNullRef<T>(ref T source)
{
#if CORECLR
throw new PlatformNotSupportedException();
// ldarg.0
// ldc.i4.0
// conv.u
// cgt.un
// ret
#else
return Unsafe.AsPointer(ref source) != null;
#endif
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public TValue this[TKey key]
get
{
ref TValue value = ref FindValue(key);
if (Unsafe.IsNotNullRef(ref value))
if (!Unsafe.IsNullRef(ref value))
{
return value;
}
Expand All @@ -197,7 +197,7 @@ void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> keyV
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> keyValuePair)
{
ref TValue value = ref FindValue(keyValuePair.Key);
if (Unsafe.IsNotNullRef(ref value) && EqualityComparer<TValue>.Default.Equals(value, keyValuePair.Value))
if (!Unsafe.IsNullRef(ref value) && EqualityComparer<TValue>.Default.Equals(value, keyValuePair.Value))
{
return true;
}
Expand All @@ -207,7 +207,7 @@ bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue>
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> keyValuePair)
{
ref TValue value = ref FindValue(keyValuePair.Key);
if (Unsafe.IsNotNullRef(ref value) && EqualityComparer<TValue>.Default.Equals(value, keyValuePair.Value))
if (!Unsafe.IsNullRef(ref value) && EqualityComparer<TValue>.Default.Equals(value, keyValuePair.Value))
{
Remove(keyValuePair.Key);
return true;
Expand All @@ -232,9 +232,8 @@ public void Clear()
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe bool ContainsKey(TKey key)
=> Unsafe.IsNotNullRef(ref FindValue(key));
=> !Unsafe.IsNullRef(ref FindValue(key));

public bool ContainsValue(TValue value)
{
Expand Down Expand Up @@ -324,7 +323,7 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte
}
}

private unsafe ref TValue FindValue(TKey key)
private ref TValue FindValue(TKey key)
{
if (key == null)
{
Expand Down Expand Up @@ -896,7 +895,7 @@ public bool Remove(TKey key, [MaybeNullWhen(false)] out TValue value)
public unsafe bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
{
ref TValue valRef = ref FindValue(key);
if (Unsafe.IsNotNullRef(ref valRef))
if (!Unsafe.IsNullRef(ref valRef))
{
value = valRef;
return true;
Expand Down Expand Up @@ -1064,7 +1063,7 @@ public void TrimExcess(int capacity)
if (IsCompatibleKey(key))
{
ref TValue value = ref FindValue((TKey)key);
if (Unsafe.IsNotNullRef(ref value))
if (!Unsafe.IsNullRef(ref value))
{
return value;
}
Expand Down
15 changes: 1 addition & 14 deletions src/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7253,27 +7253,14 @@ bool getILIntrinsicImplementationForUnsafe(MethodDesc * ftn,
}
else if (tk == MscorlibBinder::GetMethod(METHOD__UNSAFE__BYREF_NULLREF)->GetMemberDef())
{
static const BYTE ilcode[] = { CEE_LDNULL, CEE_RET };
static const BYTE ilcode[] = { CEE_LDC_I4_0, CEE_CONV_U, CEE_RET };
methInfo->ILCode = const_cast<BYTE*>(ilcode);
methInfo->ILCodeSize = sizeof(ilcode);
methInfo->maxStack = 1;
methInfo->EHcount = 0;
methInfo->options = (CorInfoOptions)0;
return true;
}
else if (tk == MscorlibBinder::GetMethod(METHOD__UNSAFE__BYREF_IS_NOTNULL)->GetMemberDef())
{
// 'ldnull' opcode would produce type o, and we can't compare & against o (ECMA-335, Table III.4).
// However, we can compare & against native int, so we'll use that instead.

static const BYTE ilcode[] = { CEE_LDARG_0, CEE_LDC_I4_0, CEE_CONV_U, CEE_PREFIX1, (CEE_CGT_UN & 0xFF), CEE_RET };
methInfo->ILCode = const_cast<BYTE*>(ilcode);
methInfo->ILCodeSize = sizeof(ilcode);
methInfo->maxStack = 2;
methInfo->EHcount = 0;
methInfo->options = (CorInfoOptions)0;
return true;
}
else if (tk == MscorlibBinder::GetMethod(METHOD__UNSAFE__BYREF_IS_NULL)->GetMemberDef())
{
// 'ldnull' opcode would produce type o, and we can't compare & against o (ECMA-335, Table III.4).
Expand Down
1 change: 0 additions & 1 deletion src/vm/mscorlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,6 @@ DEFINE_METHOD(JIT_HELPERS, ENUM_COMPARE_TO, EnumCompareTo, NoSig
DEFINE_CLASS(UNSAFE, InternalCompilerServices, Unsafe)
DEFINE_METHOD(UNSAFE, AS_POINTER, AsPointer, NoSig)
DEFINE_METHOD(UNSAFE, BYREF_IS_NULL, IsNullRef, NoSig)
DEFINE_METHOD(UNSAFE, BYREF_IS_NOTNULL, IsNotNullRef, NoSig)
DEFINE_METHOD(UNSAFE, BYREF_NULLREF, NullRef, NoSig)
DEFINE_METHOD(UNSAFE, AS_REF_IN, AsRef, GM_RefT_RetRefT)
DEFINE_METHOD(UNSAFE, AS_REF_POINTER, AsRef, GM_VoidPtr_RetRefT)
Expand Down

0 comments on commit 996524e

Please sign in to comment.