Skip to content

Use a shared helper for cached activation factories to reduce binary size - #2678

Merged
Sergio Pedri (Sergio0694) merged 1 commit into
staging/3.0from
sergio0694-activation-factory-size-reduction
Dec 3, 2026
Merged

Sergio Pedri (Sergio0694) merged 1 commit into
staging/3.0from
sergio0694-activation-factory-size-reduction

Conversation

@Sergio0694

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 4540897d62fb7084f4d373d00adb845564fbc56a

Dimension Assessment Rationale
Change confidence 8/10 ↑ Full product build (Release x64) succeeds, and the Store (Release x64 Native AOT) builds and publishes with the change.
↑ Native WinStore.App.dll measured 62,464 B smaller against an identical baseline, with CsWinRT version in effect confirmed per run.
↑ Independent Opus 5.5 review found behavioral equivalence with the old getter and no blocking issues.
↓ ProjectionWriterTest hung locally and was stopped, so no result; no other test suites were run.
↓ CI results not inspected yet.

Summary

Replaces the generated per-factory getters for cached activation factories with a one-line semi-auto property over a shared runtime helper, WindowsRuntimeObjectReference.GetActivationFactory(ref field, "TypeName", iid). This is a pure binary size reduction: there is no behavior change.

Motivation

Every projected runtime class (and every static/factory/composable interface) used to get a multi-line getter that reads the cached factory, checks IsInCurrentContext (a virtual call whose guarded devirtualization got inlined into each getter), and falls back to resolving a new factory. That code is duplicated thousands of times across the Windows SDK, XAML and 3rd party projections. The new helper keeps only a small inlined fast path for free-threaded factories (the common case), and moves the context-aware check and resolution into a single shared [NoInlining] slow path.

Changes

  • src/WinRT.Runtime/.../WindowsRuntimeObjectReference.Activation.cs: new GetActivationFactory(ref location, runtimeClassName[, in iid]) overloads, with an inlined fast path for free-threaded factories and a [NoInlining] GetOrResolveActivationFactory slow path (context-aware validation, then resolve and cache). The slot is accessed with Volatile.Read/Volatile.Write; if multiple threads resolve concurrently, the last stored wins (same as before). The default IActivationFactory overload passes a null ref Guid that the slow path detects with Unsafe.IsNullRef.
  • src/WinRT.Projection.Writer/Factories/ (ClassFactory.cs, ConstructorFactory.AttributedTypes.cs): all cached activation factory properties (default, static, factory and composable) are emitted as private static WindowsRuntimeObjectReference _objRef_* => WindowsRuntimeObjectReference.GetActivationFactory(ref field, ...), through a single shared emitter.
  • docs/: updated the generated code samples in runtime-class-projections.md and event-infrastructure.md.

Binary size

Store app (Release, x64, Native AOT, public feature level, CsWinRT 3.0 leg net10.0-windows10.0.28000.1), same Store commit and clean outputs for both runs, differing only in the CsWinRT package: current staging/3.0 (ec233e6d8) vs. this change (both built locally with the same build.cmd ... product flow; the CsWinRT version in effect was confirmed in project.assets.json for each run). Only the native WinStore.App.dll size is considered.

Binary staging/3.0 This PR Delta
WinStore.App.dll (native) 101,700,608 B 101,638,144 B -62,464 B (-0.061%)
WinRT.Sdk.Projection.dll 32,439,296 B 32,273,920 B -165,376 B (-0.51%)
WinRT.Sdk.Xaml.Projection.dll 8,719,360 B 8,628,224 B -91,136 B (-1.05%)
WinRT.Projection.dll 12,768,768 B 12,686,848 B -81,920 B (-0.64%)
WinRT.Interop.dll 53,189,120 B 53,189,120 B 0

The native size is deterministic: rebuilding the baseline gave a byte-identical size. WinRT.Runtime.dll grows by 512 B.

Replace the generated per-factory getters (a null check plus an 'IsInCurrentContext' check, with an inlined fallback) with a one-line semi-auto property over 'WindowsRuntimeObjectReference.GetActivationFactory(ref field, ...)'. The fast path (free-threaded factories) is inlined, and the context-aware and resolution logic lives in a shared non-inlined slow path.

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.

@Sergio0694
Sergio Pedri (Sergio0694) merged commit 5b444d4 into staging/3.0 Dec 3, 2026
20 checks passed
@Sergio0694
Sergio Pedri (Sergio0694) deleted the sergio0694-activation-factory-size-reduction branch December 3, 2026 08:04
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.

2 participants