Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// See the LICENSE file in the project root for more information.

using System.Runtime.CompilerServices;
#if NET8_0_OR_GREATER
using System.Runtime.InteropServices;
#endif

#if WINUI2
using Windows.UI.Composition;
Expand All @@ -24,7 +27,13 @@ internal sealed class CompositionObjectCache<TKey, TValue>
/// <summary>
/// The cache of weak references of type <typeparamref name="TValue"/> to <typeparamref name="TKey"/> instances, to avoid memory leaks
/// </summary>
private readonly ConditionalWeakTable<Compositor, Dictionary<TKey, WeakReference<TValue>>> cache = new ConditionalWeakTable<Compositor, Dictionary<TKey, WeakReference<TValue>>>();
/// <remarks>
/// The values are always <c>Dictionary&lt;TKey, WeakReference&lt;TValue&gt;&gt;</c> instances. They are typed as <see cref="object"/> because the
/// <c>[DynamicallyAccessedMembers(PublicParameterlessConstructor)]</c> annotation on the value type of <see cref="ConditionalWeakTable{TKey, TValue}"/>
/// 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 <see cref="Unsafe.As{T}(object)"/> is safe.
/// </remarks>
private readonly ConditionalWeakTable<Compositor, object> cache = new ConditionalWeakTable<Compositor, object>();

/// <summary>
/// Tries to retrieve a valid instance from the cache, and uses the provided factory if an existing item is not found
Expand All @@ -38,7 +47,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<Dictionary<TKey, WeakReference<TValue>>>(map).TryGetValue(key, out var reference) &&
reference.TryGetTarget(out result))
{
return true;
Expand All @@ -61,9 +70,15 @@ public void AddOrUpdate(Compositor compositor, TKey key, TValue value)
{
if (this.cache.TryGetValue(compositor, out var map))
{
_ = map.Remove(key);
Dictionary<TKey, WeakReference<TValue>> dictionary = Unsafe.As<Dictionary<TKey, WeakReference<TValue>>>(map);

#if NET8_0_OR_GREATER
CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out _) = new WeakReference<TValue>(value);
#else
_ = dictionary.Remove(key);

map.Add(key, new WeakReference<TValue>(value));
dictionary.Add(key, new WeakReference<TValue>(value));
#endif
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ internal sealed class CompositionObjectCache<T>
/// <summary>
/// The cache of weak references of type <typeparamref name="T"/>, to avoid memory leaks
/// </summary>
private readonly ConditionalWeakTable<Compositor, WeakReference<T>> cache = new ConditionalWeakTable<Compositor, WeakReference<T>>();
/// <remarks>
/// The values are always <see cref="WeakReference{T}"/> instances. They are typed as <see cref="object"/> for consistency with
/// <see cref="CompositionObjectCache{TKey, TValue}"/>, to avoid generic <see cref="ConditionalWeakTable{TKey, TValue}"/> value types
/// (see the remarks there). This field is private and only accessed here, so <see cref="Unsafe.As{T}(object)"/> is safe.
/// </remarks>
private readonly ConditionalWeakTable<Compositor, object> cache = new ConditionalWeakTable<Compositor, object>();

/// <summary>
/// Tries to retrieve a valid <typeparamref name="T"/> instance from the cache, and uses the provided factory if an existing item is not found
Expand All @@ -35,7 +40,7 @@ public T GetValue(Compositor compositor, Func<Compositor, T> producer)
lock (cache)
{
if (this.cache.TryGetValue(compositor, out var reference) &&
reference.TryGetTarget(out var instance))
Unsafe.As<WeakReference<T>>(reference).TryGetTarget(out var instance))
{
return instance;
}
Expand Down
Loading