Add feature switches for Type marshalling and IDIC support#2223
Draft
Sergio0694 wants to merge 10 commits intouser/kythant/TypeHandlingfrom
Draft
Add feature switches for Type marshalling and IDIC support#2223Sergio0694 wants to merge 10 commits intouser/kythant/TypeHandlingfrom
Type marshalling and IDIC support#2223Sergio0694 wants to merge 10 commits intouser/kythant/TypeHandlingfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds two feature switches to support trimming optimization in CsWinRT: one for Type marshalling and one for IDynamicInterfaceCastable (IDIC) support. Both features default to enabled but can be disabled to reduce application size when not needed.
Changes:
- Added
EnableXamlTypeMarshallingandEnableIDynamicInterfaceCastableSupportfeature switches with corresponding MSBuild properties - Refactored
Typemarshalling with improved caching usingConcurrentDictionaryand alternate lookups for better performance - Changed internal type map tables from
ConditionalWeakTabletoConcurrentDictionaryfor trimming compatibility
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/WinRT.Runtime2/Properties/WindowsRuntimeFeatureSwitches.cs | Added two new feature switch properties with configuration names |
| src/WinRT.Runtime2/WindowsRuntimeObject.cs | Added feature switch checks in IDIC methods, new exception helper class, and fixed comment reference |
| src/WinRT.Runtime2/InteropServices/WindowsRuntimeComWrappers.cs | Added feature switch guard for Type marshalling |
| src/WinRT.Runtime2/ABI/System/Type.cs | Major refactoring: new caching infrastructure, helper types for zero-allocation lookups, and feature switch integration |
| src/WinRT.Runtime2/InteropServices/TypeMapInfo/WindowsRuntimeMetadataInfo.cs | Changed from ConditionalWeakTable to ConcurrentDictionary |
| src/WinRT.Runtime2/InteropServices/TypeMapInfo/WindowsRuntimeMarshallingInfo.cs | Changed from ConditionalWeakTable to ConcurrentDictionary |
| src/WinRT.Runtime2/InteropServices/TypeMapInfo/DynamicInterfaceCastableImplementationInfo.cs | Changed from ConditionalWeakTable to ConcurrentDictionary, added System.Collections.Concurrent import |
| nuget/Microsoft.Windows.CsWinRT.targets | Added MSBuild properties and runtime configuration for new feature switches, removed obsolete switches |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Replace the per-Type backing tables from ConditionalWeakTable<Type, T?> to ConcurrentDictionary<Type, T?> in DynamicInterfaceCastableImplementationInfo, WindowsRuntimeMarshallingInfo, and WindowsRuntimeMetadataInfo. Also add a using System.Collections.Concurrent directive where needed. Note: this changes lifetime semantics (ConcurrentDictionary holds strong references), which may affect object garbage-collection behavior compared to ConditionalWeakTable.
Introduce zero-allocation marshalling helpers and caches for System.Type handling. Adds ManagedTypeReference and TransientTypeReference types, a ManagedTypeReferenceEqualityComparer, and a TypeNameCache (two ConcurrentDictionaries optimized for UI-thread access) to enable alternate-key lookups and avoid string allocations. Split out UncachedTypeMarshaller with FromTransientTypeReference/ToManagedTypeReference to centralize non-cached logic, and update TypeMarshaller to use the cache paths and alternate lookups. Adjust Convert/Box/Unbox/Dispose flows accordingly. Remove the NoMetadataTypeInfo cache and make its constructor public so trimmed/metadata-missing types are created on demand.
Introduce a new feature switch in WindowsRuntimeFeatureSwitches to control marshalling of Type instances for XAML projections. Adds the configuration constant CSWINRT_ENABLE_XAML_TYPE_MARSHALLING and the EnableXamlTypeMarshalling property (FeatureSwitchDefinition) with a default of true, along with XML documentation.
Make System.Type marshalling conditional on WindowsRuntimeFeatureSwitches.EnableXamlTypeMarshalling. Added runtime checks in TypeMarshaller ConvertFrom/ConvertTo to throw a NotSupportedException when XAML type marshalling is disabled, and added TypeExceptions.ThrowNotSupportedExceptionForMarshallingDisabled to provide a clear error message. Updated WindowsRuntimeComWrappers to only return Type marshalling info when the feature switch is enabled to avoid rooting extra metadata/type-mapping code and reduce binary size.
Delete default property values and their RuntimeHostConfigurationOption entries for several CsWinRT feature switches in nuget/Microsoft.Windows.CsWinRT.targets. Removed properties: CsWinRTEnableDynamicObjectsSupport, CsWinRTUseExceptionResourceKeys, CsWinRTEnableDefaultCustomTypeMappings, CsWinRTEnableICustomPropertyProviderSupport, and CsWinRTEnableIReferenceSupport. This cleans up obsolete/unused runtime feature switches while leaving other configuration options (e.g. IDynamicInterfaceCastable, ManifestFreeActivation) intact.
Introduce an MSBuild property CsWinRTEnableXamlTypeMarshalling (default true) and map it to the runtime host configuration option CSWINRT_ENABLE_XAML_TYPE_MARSHALLING in nuget/Microsoft.Windows.CsWinRT.targets. This exposes a build-time switch to enable/disable XAML type marshalling behavior alongside the other CsWinRT runtime switches.
Introduce a configurable feature switch to control IDynamicInterfaceCastable support so the runtime can be trimmed when this capability is not needed. - nuget: rename the RuntimeHostConfigurationOption to CSWINRT_ENABLE_IDYNAMICINTERFACECASTABLE_SUPPORT and wire it to the CsWinRTEnableIDynamicInterfaceCastableSupport property. - src/WinRT.Runtime2: add EnableIDynamicInterfaceCastableSupport feature switch and default it to true. - src/WinRT.Runtime2/WindowsRuntimeObject: gate IDynamicInterfaceCastable APIs with the feature switch (throw NotSupportedException or return false when disabled) to allow trimming and to ensure unreachable paths can be removed. Also include a small doc comment polish for an IEnumerable reference.
Replace the inline ThrowNotSupportedException local with a file-scoped WindowsRuntimeObjectExceptions.ThrowNotSupportedException and update IDynamicInterfaceCastable methods to perform an early feature-switch check. This centralizes the NotSupportedException stub (DoesNotReturn/StackTraceHidden) to improve trimming and reuse, and ensures IsInterfaceImplemented throws the same descriptive NotSupportedException when throwIfNotImplemented is true.
f3d9477 to
d1fe0c7
Compare
Reorder the Type marshalling check to first verify WindowsRuntimeFeatureSwitches.EnableXamlTypeMarshalling so the 'Type' cast can be trimmed and avoid rooting the metadata type map (reducing binary size). Also simplify the GetInfo call to use the local method (GetInfo(typeof(Type))). Comments updated to reflect the new ordering.
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.
Title. It also enables caching marshalling results for
Type.