← Back to Pull requests

C++/WinRT ABI interop improvements #1608

Closed
unknown closed this pull request without merging on Aug 24, 2026 21:51 UTC
View on GitHub
unknown unknown commented on Jul 17, 2026 17:28 UTC
Author

Why

Analysis of the Rust (windows-rs) projection plus code analysis of Windows components that use C++/WinRT showed improvements available to C++/WinRT on two common interop paths — windows-rs boxes scalars and arrays in-process and builds HSTRING literals at compile time.

What this PR changes

| Path | Change | Opt-in? |
| :-- | :-- | :-- |
| String | _hs compile-time HSTRING literal | opt-in |
| Reference | In-process scalar boxing (u8–u64, float, double, bool, char16, hstring, guid, DateTime, TimeSpan, Point) and array boxing (IReferenceArray<T>), marshaling by value through combase PropertyValue | transparent |

Reference boxing is the largest piece. box_value on a scalar or array previously hopped into combase to allocate a general IPropertyValue carrying the full discriminated-union machinery. This branch points the scalar and array reference_traits at the in-proc impl::reference<T> / impl::reference_array<T> that already back IReference<T> / IReferenceArray<T>, and makes those a correct IPropertyValue (right PropertyType, fixed IsNumericScalar, typed getters). A shared reference_producer CRTP base carries the whole IPropertyValue surface and the IMarshal tearoff once; each leaf supplies only its storage and how a getter reads it.

To preserve cross-process behavior, stock element types are marked non_agile and supply IMarshal lazily from query_interface_tearoff by building the equivalent combase PropertyValue — so the value still marshals by value, and the combase hop is paid only on marshal, never on box/unbox. Elements whose projected layout differs from the ABI (hstring, DateTime, TimeSpan arrays) and the non-stock element types (IInspectable, Size, Rect) continue to route through combase. This mirrors windows-rs' StockReference.

Two deltas vs combase, both matching windows-rs: GetRuntimeClassName now returns the IReference<T> / IReferenceArray<T> runtime class name rather than Windows.Foundation.PropertyValue, and cross-process the value marshals by value rather than as a proxy.

Results

Warmed, order-controlled benchmark of Rust (windows-rs), stock cppwinrt, and cppwinrt-new (this branch). Rust is Rust→Rust; cppwinrt columns are C++→C++. Average of two reversed-order samples, 4M measured + 2M warmup iterations. Relative results are stable across multiple attempts.

| Path | Rust | cppwinrt (stock) | cppwinrt-new | new vs stock | new vs Rust |
| :-- | --: | --: | --: | :-- | :-- |
| Int32 | 14 | 18 | 10 | 1.8× faster | tie |
| String | 14 | 20 | 8 | 2.5× faster | tie |
| Reference | 435 | 2,065 | 541 | 3.8× faster | Rust ~1.2× |

Reference retains a ~1.2× Rust lead that is structural, not the box shape: cppwinrt's per-object module-lock accounting (a lock-prefixed RMW on a process-global at each box's birth/death) plus the CRT malloc/free layer vs windows-rs' direct HeapAlloc.

Testing

New tests: test/test/reference_boxing.cpp (scalar and array PropertyType, numeric conversion, round-trip, array-getter throw behavior, marshal-by-value via GetUnmarshalClass parity with combase, and cross-STA agile identity through the Global Interface Table) and test/test_cpp20/hstring_literal.cpp. Built Release/x64; reference_boxing passes all 61 assertions.

References

  • windows-rs fork with the benchmark harness and parity changes: jonwis/windows-rs jonwis/perf-bench

  • windows-rs compare: master…jonwis:windows-rs:jonwis/perf-bench
  • (via Copilot)

    Diff: 477 lines (+357 / -120) across 5 files Reviewer: YexuanXiao Review duration: 54982m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.46) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 477 lines diff across 5 files
    0.46
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.369
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1608"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.8% (MEDIUM) · Higher = larger blast radius
    45.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.1% · Safe to merge
    18.1%