From 0ace669862aab25a000d7a2210323b8d0e4b894d Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 4 Sep 2026 22:29:08 -0700 Subject: [PATCH] Deduplicate "all managed object wrappers" mapping (#133260) --- .../src/System/Runtime/InteropServices/ComWrappers.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs index 84c316b880390a..4e0749e267551c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs @@ -817,7 +817,12 @@ private static void RegisterManagedObjectWrapperForDiagnostics(object instance, List allWrappersForThisInstance = s_allManagedObjectWrapperTable.GetOrCreateValue(instance); lock (allWrappersForThisInstance) { - allWrappersForThisInstance.Add(wrapper); + // While this can be an O(n) search for an object that is exposed via N ComWrappers instances to native code + // in practice, N = 1 for the vast majority of scenarios. + if (!allWrappersForThisInstance.Contains(wrapper)) + { + allWrappersForThisInstance.Add(wrapper); + } } }