From 5ad850a31d05280a1a767ab22a8220e8c64fbc84 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 15 Nov 2026 23:09:46 -0800 Subject: [PATCH 1/5] Use object as ConditionalWeakTable value type in CompositionObjectCache ConditionalWeakTable annotates TValue with [DynamicallyAccessedMembers(PublicParameterlessConstructor)]. Using generic instantiations such as Dictionary> as TValue makes trimming and NativeAOT keep the parameterless constructors of all instantiations of those generic types, which increases binary size. Store the values as object and use Unsafe.As when reading them back. This is safe because the tables are private and only ever store a single concrete type, written and read inside the same class. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../CompositionObjectCache{TKey,TValue}.cs | 20 ++++++++++++------- .../Cache/CompositionObjectCache{T}.cs | 9 +++++++-- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs b/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs index 7846b220..e9309f33 100644 --- a/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs +++ b/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs @@ -24,7 +24,13 @@ internal sealed class CompositionObjectCache /// /// The cache of weak references of type to instances, to avoid memory leaks /// - private readonly ConditionalWeakTable>> cache = new ConditionalWeakTable>>(); + /// + /// The values are always Dictionary<TKey, WeakReference<TValue>> instances. They are typed as because the + /// [DynamicallyAccessedMembers(PublicParameterlessConstructor)] annotation on the value type of + /// would otherwise make trimming and NativeAOT keep the parameterless constructors of all generic instantiations (binary size). + /// This field is private and only accessed here, so is safe. + /// + private readonly ConditionalWeakTable cache = new ConditionalWeakTable(); /// /// Tries to retrieve a valid instance from the cache, and uses the provided factory if an existing item is not found @@ -38,7 +44,7 @@ public bool TryGetValue(Compositor compositor, TKey key, out TValue? result) lock (this.cache) { if (this.cache.TryGetValue(compositor, out var map) && - map.TryGetValue(key, out var reference) && + Unsafe.As>>(map).TryGetValue(key, out var reference) && reference.TryGetTarget(out result)) { return true; @@ -61,15 +67,15 @@ public void AddOrUpdate(Compositor compositor, TKey key, TValue value) { if (this.cache.TryGetValue(compositor, out var map)) { - _ = map.Remove(key); + Dictionary> dictionary = Unsafe.As>>(map); - map.Add(key, new WeakReference(value)); + _ = dictionary.Remove(key); + + dictionary.Add(key, new WeakReference(value)); } else { - map = new Dictionary> { [key] = new WeakReference(value) }; - - this.cache.Add(compositor, map); + this.cache.Add(compositor, new Dictionary> { [key] = new WeakReference(value) }); } } } diff --git a/components/Media/src/Helpers/Cache/CompositionObjectCache{T}.cs b/components/Media/src/Helpers/Cache/CompositionObjectCache{T}.cs index 6465b941..7014c7a3 100644 --- a/components/Media/src/Helpers/Cache/CompositionObjectCache{T}.cs +++ b/components/Media/src/Helpers/Cache/CompositionObjectCache{T}.cs @@ -22,7 +22,12 @@ internal sealed class CompositionObjectCache /// /// The cache of weak references of type , to avoid memory leaks /// - private readonly ConditionalWeakTable> cache = new ConditionalWeakTable>(); + /// + /// The values are always instances. They are typed as for consistency with + /// , to avoid generic value types + /// (see the remarks there). This field is private and only accessed here, so is safe. + /// + private readonly ConditionalWeakTable cache = new ConditionalWeakTable(); /// /// Tries to retrieve a valid instance from the cache, and uses the provided factory if an existing item is not found @@ -35,7 +40,7 @@ public T GetValue(Compositor compositor, Func producer) lock (cache) { if (this.cache.TryGetValue(compositor, out var reference) && - reference.TryGetTarget(out var instance)) + Unsafe.As>(reference).TryGetTarget(out var instance)) { return instance; } From 42c32dac661592d36c7a5d1b9ab1dc6d920176e2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Nov 2026 16:07:41 -0800 Subject: [PATCH 2/5] Avoid double lookup in CompositionObjectCache.AddOrUpdate on modern .NET Use CollectionsMarshal.GetValueRefOrAddDefault on NET6+ instead of Remove + Add. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs b/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs index e9309f33..f12baac7 100644 --- a/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs +++ b/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs @@ -3,6 +3,9 @@ // See the LICENSE file in the project root for more information. using System.Runtime.CompilerServices; +#if NET6_0_OR_GREATER +using System.Runtime.InteropServices; +#endif #if WINUI2 using Windows.UI.Composition; @@ -69,9 +72,15 @@ public void AddOrUpdate(Compositor compositor, TKey key, TValue value) { Dictionary> dictionary = Unsafe.As>>(map); +#if NET6_0_OR_GREATER + WeakReference reference = new(value); + + CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out _) = reference; +#else _ = dictionary.Remove(key); dictionary.Add(key, new WeakReference(value)); +#endif } else { From 3130806dd475e3ea7de114e495772315b53e71ee Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Nov 2026 16:26:08 -0800 Subject: [PATCH 3/5] Inline WeakReference allocation in CompositionObjectCache.AddOrUpdate Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs b/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs index f12baac7..ece1570d 100644 --- a/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs +++ b/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs @@ -73,9 +73,7 @@ public void AddOrUpdate(Compositor compositor, TKey key, TValue value) Dictionary> dictionary = Unsafe.As>>(map); #if NET6_0_OR_GREATER - WeakReference reference = new(value); - - CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out _) = reference; + CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out _) = new WeakReference(value); #else _ = dictionary.Remove(key); From 8b712bba3a9e491022707854973dc8719959b68f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Nov 2026 16:27:04 -0800 Subject: [PATCH 4/5] Use NET8_0_OR_GREATER for GetValueRefOrAddDefault path Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs b/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs index ece1570d..b8427728 100644 --- a/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs +++ b/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs @@ -3,7 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Runtime.CompilerServices; -#if NET6_0_OR_GREATER +#if NET8_0_OR_GREATER using System.Runtime.InteropServices; #endif @@ -72,7 +72,7 @@ public void AddOrUpdate(Compositor compositor, TKey key, TValue value) { Dictionary> dictionary = Unsafe.As>>(map); -#if NET6_0_OR_GREATER +#if NET8_0_OR_GREATER CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out _) = new WeakReference(value); #else _ = dictionary.Remove(key); From ccd97b56d372f36c16de4093a091a9131615ca94 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 20 Nov 2026 13:49:58 -0800 Subject: [PATCH 5/5] Restore original else branch in CompositionObjectCache.AddOrUpdate Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs b/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs index b8427728..1341a5e5 100644 --- a/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs +++ b/components/Media/src/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs @@ -82,7 +82,9 @@ public void AddOrUpdate(Compositor compositor, TKey key, TValue value) } else { - this.cache.Add(compositor, new Dictionary> { [key] = new WeakReference(value) }); + map = new Dictionary> { [key] = new WeakReference(value) }; + + this.cache.Add(compositor, map); } } }