Skip to content

Bug: Calling PropertyValue.Create to handle boxing is significantly slower than allocating an IReference directly #1416

Closed as not planned

Description

Version

2.0.240405.15

Summary

This takes 30ms to execute:

winrt::Windows::Foundation::IInspectable ref = nullptr;
for (int i = 0; i < 200000; i++)
    ref = winrt::box_value(i + 1);

This takes 8ms to execute:

winrt::Windows::Foundation::IInspectable ref = nullptr;
for (int i = 0; i < 200000; i++)
    ref = winrt::make<winrt::impl::reference<int>>(i + 1);

It seems weird for box_value to be using PropertyValue.CreateInt32 when just directly allocating a wrapper is significantly faster.

Reproducible example

int main()
{
    winrt::init_apartment();
    const auto before = std::chrono::high_resolution_clock::now();

    winrt::Windows::Foundation::IInspectable ref = nullptr;
    for (int i = 0; i < 200000; i++)
        ref = winrt::box_value(i + 1);

    const auto after = std::chrono::high_resolution_clock::now();
    const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(after - before);
    std::cout << duration.count();
}

Expected behavior

I expected boxing to be fast.

Actual behavior

It's kinda slow.

Additional comments

Spotted this while looking into microsoft/microsoft-ui-xaml#9154

WPF users are expecting WinUI 3 to match WPF performance - and setting 200k DPs takes 21ms with WPF, but 130ms with WinUI 3. We ought to try and match that speed where possible, and this should help by cutting middlemans, at least in C++.

Activity

  1. jgkumaran commented on May 15, 2024

    @jgkumaran
    Contributor

    Reälly reminds me of those StructHelper types, which offered often basic functionality, doäble by yourself, with much greater overhead.

    I approve replacing PropertyValue.Create* methods! That said, can we finally get useful interfaces like IStringable implemented on boxed objects?

  2. kennykerr commented on May 16, 2024

    @kennykerr
    Collaborator

    As I recall, the PropertyValue implementations offer marshal-by-value semantics which the C++/WinRT fallback does not. This is/was required for some APIs depending on boxing. We can't change this now without the risk of breaking such callers. In practice, it's mostly only interesting to Xaml but there are other APIs that depend on this.

  3. jgkumaran commented on May 16, 2024

    @jgkumaran
    Contributor

    marshal-by-value semantics

    Kenny Kerr (@kennykerr) Can't we implement this ourselves?

  4. sylveon commented on May 16, 2024

    @sylveon
    ContributorAuthor

    From a cursory search, CsWinRT seems to allocate its own IReference (which works fine with XAML).

  5. github-actions commented on May 27, 2024

    @github-actions

    This issue is stale because it has been open 10 days with no activity. Remove stale label or comment or this will be closed in 5 days.

  6. oldnewthing commented on Nov 30, 2025

    @oldnewthing
    Member

    I'm concerned that there may be projections that rely on boxed values supporting IPropertyValue.

  7. sylveon commented on Nov 30, 2025

    @sylveon
    ContributorAuthor

    Could we have winrt::impl::reference implement that? I wonder if CsWinRT implements it for theirs.

  8. kennykerr commented on Dec 1, 2025

    @kennykerr
    Collaborator

    I'm concerned that there may be projections that rely on boxed values supporting IPropertyValue.

    This must already be implemented since it is required by IReference<T>. Of course, the implementation may be different... Another question is whether the marshaling behavior will change and cause issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions