Repository navigation
Share ConditionalWeakTable instantiation for IEnumeratorAdapter<T> - #2661
Open
Sergio Pedri (Sergio0694) wants to merge 5 commits into
Open
Sergio Pedri (Sergio0694) wants to merge 5 commits into
Sergio Pedri (Sergio0694) wants to merge 5 commits into
Conversation
…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: There may be pipelines that require an authorized user to comment /azp run to run. |
Sergio Pedri (Sergio0694)
requested a review
from Manodasan Wignarajah (manodasanW)
November 20, 2026 19:42
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>
Sergio Pedri (Sergio0694)
marked this pull request as ready for review
November 20, 2026 21:45
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Store the
IEnumeratorAdapter<T>mapping tables asConditionalWeakTable<object, object>for every element type, instead of a distinctConditionalWeakTable<IEnumerator<T>, IEnumeratorAdapter<T>>instantiation perT. 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 constructedConditionalWeakTableinstantiations, 325 of which wereIEnumeratorAdapterTable<T>tables, one for each element type used with anIEnumerator<T>that is marshalled to native code. Usingobjectfor both type arguments collapses all of them into a single instantiation. (ConditionalWeakTable<TKey, TValue>also annotatesTValuewith[DynamicallyAccessedMembers(PublicParameterlessConstructor)], but withobjectthat only keepsobject..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 implementIEnumerator<T>for multipleTvalues (including covariant views), andIEnumeratorAdapter<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>.Tableis now aConditionalWeakTable<object, object>.GetInstancecalls a new privateGetOrCreateInstance, which uses the existing cached-factoryGetOrAddpattern and casts the stored value back withUnsafe.As(the table is file-local and only written here, with a matchingT).IEnumeratorAdapterFactory<T>keeps its role, but is now a static class whose cachedFunc<object, object>callback is a plain static method that casts the key back toIEnumerator<T>.src/Tests/UnitTest/EnumeratorAdapterTests.cs: new tests checking thatGetInstancereturns a stable adapter for the same enumerator (and returns an adapter unchanged), and that an enumerator implementing bothIEnumerator<int>andIEnumerator<object>gets one distinct, stable adapter for each element type, each tracking its own first-item state. The second test fails with anInvalidCastExceptionagainst a single shared table.Binary size impact
Measured on the Microsoft Store app (
WinStore.App.dll, Native AOT,OptimizationPreference=Size, realmsbuildpublish consuming a localMicrosoft.Windows.CsWinRT3.0.0-preview.261104.1 package where onlyWinRT.Runtime.dll(lib and ref) was replaced with a build containing this change, no other changes to the Store):The number of constructed
ConditionalWeakTableinstantiations in the compiled app goes from 406 to 75, and there are no new IL or ILC warnings. The same change layered with the ToolkitConditionalWeakTablefix already used as a baseline in earlier measurements gave the same relative saving (-1.13%).Other variants that were measured and not kept:
EventRegistrationTokenTable<T>event tables (touching the projection writer and the interop generator) saved only -13,824 B (-0.02%) on its own.TryGetValuethenGetOrAdd(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.GetOrAddcall directly inGetInstanceinstead of a separate helper cost about 76 KB raw and 10 KB MSIX over this PR.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
WinRT.Runtime(2/2 passing), not through the fullUnitTestproject.