From: Stephen Toub Date: Sat, 10 Dec 2016 14:54:15 +0000 (-0500) Subject: Use JitHelpers.UnsafeCast in ConditionalWeakTable X-Git-Tag: submit/tizen/20210909.063632~11030^2~8641^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=14285f37e1ab1cd5cf1caa703d5c9af6f7e34e6c;p=platform%2Fupstream%2Fdotnet%2Fruntime.git 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 --- 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); } } }