Skip to content

Use object as ConditionalWeakTable value type in CompositionObjectCache - #891

Open
Sergio0694 wants to merge 5 commits into
mainfrom
user/sergiopedri/cwt-object-value-type
Open

Sergio0694 wants to merge 5 commits into
mainfrom
user/sergiopedri/cwt-object-value-type

Conversation

@Sergio0694

Copy link
Copy Markdown
Member

Fixes

No linked issue. Small internal binary size optimization for trimmed / NativeAOT apps.

PR Type

  • Refactoring (no functional changes, no api changes)

What is the current behavior?

CompositionObjectCache<TKey, TValue> stores its values in a ConditionalWeakTable<Compositor, Dictionary<TKey, WeakReference<TValue>>>. ConditionalWeakTable<TKey, TValue> annotates TValue with [DynamicallyAccessedMembers(PublicParameterlessConstructor)], so with a generic collection as TValue, trimming and NativeAOT (ILC) keep and make reflectable the parameterless constructor of every Dictionary<,> instantiation in the app. In a large NativeAOT app this was measured at roughly 460-500 KB (the previous analysis on the Microsoft Store app, by patching the shipped DLL: reflectable Dictionary ctor nodes 212 -> 0, -497,664 B, -0.59%).

What is the new behavior?

  • CompositionObjectCache{TKey,TValue}: the table is now ConditionalWeakTable<Compositor, object>, and values are read back with Unsafe.As<Dictionary<TKey, WeakReference<TValue>>>.
  • CompositionObjectCache{T}: same change with WeakReference<T>. WeakReference<T> has no public parameterless constructor, so this one likely saves nothing by itself; it is changed for consistency.
  • AddOrUpdate uses CollectionsMarshal.GetValueRefOrAddDefault on NET8_0_OR_GREATER to avoid the duplicate dictionary lookup (Remove + Add); other targets (uap) keep the previous code.

Why Unsafe.As is safe: the cache fields are private readonly, both classes are internal sealed, InternalsVisibleTo only goes to test assemblies, and the only writer in each class inserts exactly that one concrete type. Readers only cast after TryGetValue returns true. All access is under the same lock. No public API or behavior change.

I scanned the rest of the repo for equivalent cases (other ConditionalWeakTable fields, Lazy<T>, Activator.CreateInstance<T>, new() constraints, [DynamicallyAccessedMembers] type parameters). The other CWTs all have non-generic value types (Action, AttachedShadowElementContext, CancellationTokenSource, SurfaceLoader), so nothing else needed changing.

PR Checklist

  • Created a feature/dev branch in your fork (vs. submitting directly from a commit on main)
  • Based off latest main branch of toolkit
  • Tested code with current supported SDKs
  • New component
  • Tests for the changes have been added (if applicable): not applicable, Media has no test content
  • Header has been added to all new source files: no new files
  • Contains NO breaking changes

Other information

Validation: components\Media\src\CommunityToolkit.WinUI.Media.csproj, Release, AnyCPU, built with dotnet build for net8.0-windows10.0.26100.0 and net9.0-windows10.0.26100.0, and with VS MSBuild for uap10.0.17763 (-p:EnabledMultiTargets=uwp): 0 errors, no warnings from the touched files. The final GetValueRefOrAddDefault edits were rebuilt on net8/net9 only (the uap path is unchanged since its last build).

Not verified: no NativeAOT / ILC size re-measurement of this exact source change, and no tests were run (none exist for Media). On uap, Unsafe comes transitively via System.Threading.Tasks.Extensions 4.6.0 (referenced in components/Media/src/Dependencies.props).

Sergio0694 and others added 5 commits November 15, 2026 23:09
ConditionalWeakTable<TKey, TValue> annotates TValue with
[DynamicallyAccessedMembers(PublicParameterlessConstructor)]. Using generic
instantiations such as Dictionary<TKey, WeakReference<TValue>> 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<T> 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>
Use CollectionsMarshal.GetValueRefOrAddDefault on NET6+ instead of Remove + Add.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@Sergio0694
Sergio0694 marked this pull request as ready for review November 20, 2026 22:08
@Sergio0694
Sergio0694 enabled auto-merge (squash) November 20, 2026 22:08

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants