Skip to content

Conversation

@Sergio0694
Copy link
Member

@Sergio0694 Sergio0694 commented Jan 3, 2026

Title. Also added some optimizations for string keys.

Examples of codegen:

image

Note

Draft because there's some commits to move and this branch also needs to be rebased.

Deleted the IMapViewVftbl struct from InteropServices Vtables as part of code cleanup or refactoring. This may indicate a change in how IMapView interfaces are handled or a removal of unused interop code.
Introduces IReadOnlyDictionarySplitAdapter<TKey, TValue>, an adapter for IReadOnlyDictionary that maintains keys in sorted order and supports efficient splitting. This class is intended for internal use and is marked obsolete, providing binary search-based lookups and split functionality for Windows Runtime interop scenarios.
Introduces a static adapter class to expose IReadOnlyDictionary<TKey, TValue> as Windows.Foundation.Collections.IMapView<K, V>. Provides Lookup, Size, and Split methods for compatibility with WinRT map view interfaces.
Introduces IReadOnlyDictionaryAdapterExtensions with a Lookup extension method for IReadOnlyDictionary<string, TValue> that accepts ReadOnlySpan<char> keys. This method optimizes lookups by avoiding string allocations when possible, using alternate lookup mechanisms for Dictionary, ConcurrentDictionary, and FrozenDictionary types.
Introduces a new Lookup method implementation for IReadOnlyDictionary2 interop types, including a dedicated factory and supporting references. Updates the builder to use this method in vtable construction and refines parameter rewrite tracking in IReadOnlyList1Impl.
Introduced an Import extension method for IMethodDescriptor to allow importing method descriptors into a module using the default importer. This enhances consistency and usability alongside existing import extensions.
Refactored the Lookup method generation to accept a lookupMethod parameter, enabling optimized handling for string key types and supporting both IReadOnlyDictionary and IDictionary interfaces. Updated related builder and reference code to support this change and added remarks for clarity.
Introduces the get_Size method implementation for IReadOnlyDictionary2 interop types. Updates the builder to include get_Size in the vtable, adds a factory method to generate the method definition, and provides a reference accessor for the Size property in InteropReferences.
Introduces a HasKey method for IReadOnlyDictionary2 interop types, including method generation in the builder and factory, and a fast-path extension for string keys using ReadOnlySpan<char>. This improves performance and feature parity for COM interop scenarios involving IReadOnlyDictionary.
Introduces the Split method to the IReadOnlyDictionary2 interop implementation, including its definition, method body, and reference resolution. This enables support for splitting IReadOnlyDictionary2 instances in the generated interop code.
Adds handling for IReadOnlyDictionarySplitAdapter<TKey, TValue> in InteropTypeDiscovery to support types returned by IMapView<K, V>.Split. Also introduces a TypeReference for IReadOnlyDictionarySplitAdapter2 in InteropReferences.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements full "Impl" type code generation for IReadOnlyDictionary<TKey, TValue> in the cswinrtgen tool, enabling proper WinRT interop for read-only dictionary types. The implementation includes optimizations for string keys to avoid allocations.

Key changes:

  • Added adapter and split adapter classes for IReadOnlyDictionary<TKey, TValue> to handle WinRT's IMapView<K, V> interface requirements
  • Implemented method factories for Lookup, Size, HasKey, and Split operations with string key optimizations
  • Removed obsolete IMapViewVftbl.cs as it's replaced by the new implementation

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
IMapViewVftbl.cs Removed deprecated vtable binding type
IReadOnlyDictionarySplitAdapter{TKey, TValue}.cs Added split adapter supporting sorted dictionary halving for WinRT Split operation
IReadOnlyDictionaryAdapter{TKey, TValue}.cs Added stateless adapter providing core dictionary operations for WinRT projection
IReadOnlyDictionaryAdapterExtensions.cs Added string-key optimizations using alternate lookups to avoid allocations
InteropReferences.cs Added type references for new adapter types and their methods
InteropMethodDefinitionFactory.IReadOnlyList1Impl.cs Updated comments to clarify IndexOf method reusability
InteropMethodDefinitionFactory.IReadOnlyDictionary2Impl.cs Added method factories for all IReadOnlyDictionary impl operations
ImportExtensions.cs Added Import overload for IMethodDescriptor
InteropTypeDiscovery.Generics.cs Added tracking for IReadOnlyDictionarySplitAdapter type instances
InteropTypeDefinitionBuilder.IReadOnlyDictionary2.cs Implemented full Impl type builder with all vtable methods

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Replaces InvalidOperationException with KeyNotFoundException when a key is not found in IReadOnlyDictionary adapters. This change improves exception accuracy and aligns with .NET conventions.
Renamed the variable 'getAtMethod' to 'lookupMethod' to better reflect its purpose and improve code readability in the IReadOnlyDictionary2 interop builder.
Applied the MethodImplOptions.AggressiveInlining attribute to the Size method in IReadOnlyDictionaryAdapter to improve performance by suggesting inlining to the compiler.
Corrects the generic parameter signatures used in the IReadOnlyDictionary2 reference for the Lookup method, ensuring the correct type parameters are passed instead of previously used variables.
Adds special handling for ArraySegment<T>.Enumerator returned by IReadOnlyDictionarySplitAdapter<TKey, TValue>.GetEnumerator() in cswinrtgen. Updates InteropReferences to expose ArraySegment1Enumerator and documents the need to keep implementation in sync with code generator logic.
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.

3 participants