From 14285f37e1ab1cd5cf1caa703d5c9af6f7e34e6c Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sat, 10 Dec 2016 09:54:15 -0500 Subject: [PATCH] Use JitHelpers.UnsafeCast in ConditionalWeakTable We know the types and can use UnsafeCast when reading out the objects as TKey and TValue. This improves reading speed by ~30%. Commit migrated from https://github.com/dotnet/coreclr/commit/381199e5e1e834e7130c31ff6a51adb4f2aafbc6 --- .../System/Runtime/CompilerServices/ConditionalWeakTable.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/coreclr/src/mscorlib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs b/src/coreclr/src/mscorlib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs index 9d9b61c..7455967 100644 --- a/src/coreclr/src/mscorlib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs +++ b/src/coreclr/src/mscorlib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs @@ -473,7 +473,7 @@ namespace System.Runtime.CompilerServices { object secondary; int entryIndex = FindEntry(key, out secondary); - value = (TValue)secondary; + value = JitHelpers.UnsafeCast(secondary); return entryIndex != -1; } @@ -649,7 +649,7 @@ namespace System.Runtime.CompilerServices { for (int entriesIndex = _buckets[bucket]; entriesIndex != -1; entriesIndex = _entries[entriesIndex].Next) { - TKey thisKey = (TKey)_entries[entriesIndex].depHnd.GetPrimary(); + TKey thisKey = JitHelpers.UnsafeCast(_entries[entriesIndex].depHnd.GetPrimary()); if (thisKey != null) { list.Add(thisKey); @@ -680,7 +680,7 @@ namespace System.Runtime.CompilerServices // expired key as a live key with a null value.) if (primary != null) { - list.Add((TValue)secondary); + list.Add(JitHelpers.UnsafeCast(secondary)); } } } @@ -706,8 +706,8 @@ namespace System.Runtime.CompilerServices if (Equals(thisKey, key)) { GC.KeepAlive(this); // ensure we don't get finalized while accessing DependentHandles. - value = (TValue)thisValue; - return (TKey)thisKey; + value = JitHelpers.UnsafeCast(thisValue); + return JitHelpers.UnsafeCast(thisKey); } } } -- 2.7.4