Skip to content

Share ConditionalWeakTable instantiation for IEnumeratorAdapter<T> - #2661

Open
Sergio Pedri (Sergio0694) wants to merge 5 commits into
staging/3.0from
user/sergiopedri/cwt-object-tvalue-audit
Open

Sergio Pedri (Sergio0694) wants to merge 5 commits into
staging/3.0from
user/sergiopedri/cwt-object-tvalue-audit

Conversation

@Sergio0694

@Sergio0694 Sergio Pedri (Sergio0694) commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Summary

Store the IEnumeratorAdapter<T> mapping tables as ConditionalWeakTable<object, object> for every element type, instead of a distinct ConditionalWeakTable<IEnumerator<T>, IEnumeratorAdapter<T>> instantiation per T. This removes a large number of generic instantiations from Native AOT apps without changing behavior.

Motivation

Every distinct constructed ConditionalWeakTable<TKey, TValue> pulls its own set of generic types into Native AOT binaries (the table, its entries and containers, boxed key/value pairs, interface instantiations, generic dictionaries and type loader templates), about 2.3 to 3 KB each. Inspecting the MSTAT and dependency graph of the Microsoft Store app showed 406 constructed ConditionalWeakTable instantiations, 325 of which were IEnumeratorAdapterTable<T> tables, one for each element type used with an IEnumerator<T> that is marshalled to native code. Using object for both type arguments collapses all of them into a single instantiation. (ConditionalWeakTable<TKey, TValue> also annotates TValue with [DynamicallyAccessedMembers(PublicParameterlessConstructor)], but with object that only keeps object..ctor; the saving here comes from the instantiation count, not from the annotation.)

There is still one table instance for each T, rather than a single shared table. An enumerator object can implement IEnumerator<T> for multiple T values (including covariant views), and IEnumeratorAdapter<T> is stateful (_firstItem, _hasCurrent), so each element type needs its own adapter for the same enumerator. A prototype sharing a single table across all element types lost iterator state in that scenario, and was dropped.

Changes

  • src/WinRT.Runtime/InteropServices/Collections/IEnumeratorAdapter{T}.cs: IEnumeratorAdapterTable<T>.Table is now a ConditionalWeakTable<object, object>. GetInstance calls a new private GetOrCreateInstance, which uses the existing cached-factory GetOrAdd pattern and casts the stored value back with Unsafe.As (the table is file-local and only written here, with a matching T). IEnumeratorAdapterFactory<T> keeps its role, but is now a static class whose cached Func<object, object> callback is a plain static method that casts the key back to IEnumerator<T>.
  • src/Tests/UnitTest/EnumeratorAdapterTests.cs: new tests checking that GetInstance returns a stable adapter for the same enumerator (and returns an adapter unchanged), and that an enumerator implementing both IEnumerator<int> and IEnumerator<object> gets one distinct, stable adapter for each element type, each tracking its own first-item state. The second test fails with an InvalidCastException against a single shared table.

Binary size impact

Measured on the Microsoft Store app (WinStore.App.dll, Native AOT, OptimizationPreference=Size, real msbuild publish consuming a local Microsoft.Windows.CsWinRT 3.0.0-preview.261104.1 package where only WinRT.Runtime.dll (lib and ref) was replaced with a build containing this change, no other changes to the Store):

Build Binary size Binary size delta MSIX size (zlib level 6) MSIX size delta
Baseline 84,961,792 B 27,617,706 B
This PR 83,990,528 B -971,264 B (-1.14%) 27,348,762 B -268,944 B (-0.97%)

The number of constructed ConditionalWeakTable instantiations in the compiled app goes from 406 to 75, and there are no new IL or ILC warnings. The same change layered with the Toolkit ConditionalWeakTable fix already used as a baseline in earlier measurements gave the same relative saving (-1.13%).

Other variants that were measured and not kept:

  • Also changing the EventRegistrationTokenTable<T> event tables (touching the projection writer and the interop generator) saved only -13,824 B (-0.02%) on its own.
  • Eagerly allocating the adapter (TryGetValue then GetOrAdd(key, value), no factory) gave 84,016,128 B and 27,343,345 B MSIX: 25,600 B larger and 5,417 B smaller in MSIX than this PR.
  • Inlining the GetOrAdd call directly in GetInstance instead of a separate helper cost about 76 KB raw and 10 KB MSIX over this PR.
  • Using the GetOrAdd(key, factory, argument) overload with the enumerator as the argument cost about 55 KB raw and 11 KB MSIX over this PR, and is redundant since the argument is the key.

Not verified

  • No runtime smoke test of the Store app was run.
  • The new tests were compiled and run in an isolated MSTest project against a Release build of WinRT.Runtime (2/2 passing), not through the full UnitTest project.
  • Measurements are from the gate-only ILC toolchain (dynamic generics switch off), on the Store NativeAOT build only.

…pping tables

Store the adapters in a ConditionalWeakTable<object, object> for every element type, instead of a distinct ConditionalWeakTable<IEnumerator<T>, IEnumeratorAdapter<T>> instantiation per T. This avoids a separate set of generic instantiations (the table, its entries and related types) for each element type, which shrinks Native AOT binaries. There is still one table for each T, as an enumerator can implement IEnumerator<T> for multiple T-s and the adapters are stateful.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…table entries

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
Sergio Pedri (Sergio0694) marked this pull request as ready for review November 20, 2026 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AOT CsWinRT 3.0 performance Related to performance work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant