@@ -609,9 +609,9 @@ internal sealed class ObjectReferenceWithContext<
609
609
private readonly IntPtr _contextCallbackPtr ;
610
610
private readonly IntPtr _contextToken ;
611
611
612
- private volatile ConcurrentDictionary < IntPtr , ObjectReference < T > > __cachedContext ;
613
- private ConcurrentDictionary < IntPtr , ObjectReference < T > > CachedContext => __cachedContext ?? Make_CachedContext ( ) ;
614
- private ConcurrentDictionary < IntPtr , ObjectReference < T > > Make_CachedContext ( )
612
+ private volatile ConcurrentDictionary < IntPtr , IObjectReference > __cachedContext ;
613
+ private ConcurrentDictionary < IntPtr , IObjectReference > CachedContext => __cachedContext ?? Make_CachedContext ( ) ;
614
+ private ConcurrentDictionary < IntPtr , IObjectReference > Make_CachedContext ( )
615
615
{
616
616
global ::System . Threading . Interlocked . CompareExchange ( ref __cachedContext , new ( ) , null ) ;
617
617
return __cachedContext ;
@@ -702,9 +702,15 @@ private ObjectReference<T> GetCurrentContext()
702
702
return null ;
703
703
}
704
704
705
- return CachedContext . GetOrAdd ( currentContext , CreateForCurrentContext ) ;
705
+ // We use a non-generic map of just <IntPtr, IObjectReference> values, to avoid all generic instantiations
706
+ // of ConcurrentDictionary<,> and transitively dependent types for every vtable type T, since it's not
707
+ // something we actually need. Because the cache is private and we're the only ones using it, we can
708
+ // just store the per-context agile references as IObjectReference values, and then cast them on return.
709
+ IObjectReference objectReference = CachedContext . GetOrAdd ( currentContext , CreateForCurrentContext ) ;
706
710
707
- ObjectReference < T > CreateForCurrentContext ( IntPtr _ )
711
+ return Unsafe . As < ObjectReference < T > > ( objectReference ) ;
712
+
713
+ IObjectReference CreateForCurrentContext ( IntPtr _ )
708
714
{
709
715
var agileReference = AgileReference ;
710
716
// We may fail to switch context and thereby not get an agile reference.
0 commit comments