Skip to content

Optimize free-threaded checks when metadata information is available#2255

Draft
Sergio0694 wants to merge 6 commits intostaging/3.0from
user/sergiopedri/optimize-agile-refs2
Draft

Optimize free-threaded checks when metadata information is available#2255
Sergio0694 wants to merge 6 commits intostaging/3.0from
user/sergiopedri/optimize-agile-refs2

Conversation

@Sergio0694
Copy link
Member

Title. This can skip up to 2 QIs for every single RCW being created.

Introduce an internal enum CreateObjectReferenceMarshalingType to specify the marshaling behavior when creating WindowsRuntimeObjectReference instances. Defines Unknown, Agile, and Standard values and includes XML doc remarks linking to Windows.Foundation.Metadata.MarshalingType for clarity.
Add a marshalingType parameter to WindowsRuntimeObjectReference.InitializeObjectReference and InitializeObjectReferenceUnsafe to enable fast-path decisions (Agile/Standard) and avoid expensive runtime checks when metadata is available. Update free-threaded checks to use a boolean result derived from HRESULTs and adjust error handling and ownership semantics accordingly. Add overloads to WindowsRuntimeComWrappersMarshal that accept MarshalingType (mapped via CreateObjectReferenceMarshalingType.FromMetadata) and annotate platform support; update wrapperFlags assignment and XML docs. Also add required usings and pragmas to silence analyzer warnings.
Add a CreateObjectReferenceMarshalingType parameter to creation helpers and branch on it to avoid expensive runtime queries when possible. Fast-paths for Agile and Standard are used to set free-threaded semantics, while the default path calls ComObjectHelpers.IsFreeThreadedUnsafe and preserves previous failure handling (including releasing the inner unknown and throwing). Refactor uses a bool isFreeThreaded instead of HRESULT comparisons and clean up related comments and XML docs (including a small typo fix). This reduces runtime overhead for known marshaling annotations and centralizes the free-threaded decision logic.
Add a local CreateObjectReferenceMarshalingType enum (public but marked Obsolete and EditorBrowsable(Never)) to represent marshaling kinds and replace usages of Windows.Foundation.Metadata.MarshalingType. Update WindowsRuntimeComWrappersMarshal.CreateObjectReference/CreateObjectReferenceUnsafe to accept the new enum and pass it through to WindowsRuntimeObjectReference initialization. Propagate the marshalingType parameter through WindowsRuntimeObject constructors and activation paths so the value is forwarded when creating object references. Remove the direct dependency on Windows.Foundation.Metadata and adjust XML docs/links accordingly.
Introduce runtime validation for declared CreateObjectReferenceMarshalingType values. Adds ComObjectHelpers.ValidateMarshalingType (with aggressive inlining and a NoInlining helper) to check IsFreeThreadedUnsafe and FailFast on mismatches, and wire calls to this validator in WindowsRuntimeObjectReference initialization paths. Add a new feature switch (CSWINRT_ENABLE_MARSHALING_TYPE_VALIDATION / EnableMarshalingTypeValidation) to toggle the behavior (defaults to false) and add the required using for System.Runtime.CompilerServices.
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 optimizes the creation of Runtime Callable Wrappers (RCWs) for Windows Runtime objects by leveraging static metadata information about marshaling types. When the marshaling type is known from metadata (Agile or Standard), the runtime can skip up to 2 QueryInterface calls that would otherwise be needed to determine if an object is free-threaded. This is achieved by adding a new CreateObjectReferenceMarshalingType parameter throughout the object reference creation pipeline.

Changes:

  • Introduced CreateObjectReferenceMarshalingType enum to represent marshaling type information from metadata (Unknown, Agile, Standard)
  • Added optional validation feature (EnableMarshalingTypeValidation) to verify that declared marshaling types match runtime behavior
  • Updated all WindowsRuntimeObjectReference initialization and WindowsRuntimeObject constructor signatures to accept marshaling type parameter
  • Maintained backward compatibility by providing overloads that default to Unknown marshaling type

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
CreateObjectReferenceMarshalingType.cs New enum defining marshaling types (Unknown, Agile, Standard) aligned with Windows metadata
WindowsRuntimeFeatureSwitches.cs Added feature switch to enable validation of marshaling type declarations (defaults to false)
ComObjectHelpers.cs Added validation method to verify marshaling type matches runtime behavior via fail-fast
WindowsRuntimeObjectReference.Initialization.cs Updated all initialization methods to accept marshaling type and use fast-path when type is known
WindowsRuntimeComWrappersMarshal.cs Added overloads for CreateObjectReference methods accepting marshaling type parameter
WindowsRuntimeObject.cs Updated all activation constructors to accept and forward marshaling type parameter; updated XML doc references

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

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.

1 participant