Skip to content

Bug:map_view_base Lookup failure case is several orders slower due to C++ exceptions #1511

Description

Version

v2.0.250303.1

Summary

I created a synthetic benchmark to test the perf of the failed case of Lookup(), that generates a C++ exception (impl below hresult_out_of_bounds()), and it reports the following when compared to the OS implementation of ValueSet. Lookup is called in TryLookup to simplify usage.

CppWinRT Map
TryLookup fails
100000 iterations, 2021ms, 0ms per iteration

TryLookup succeeds
100000 iterations, 5ms, 0ms per iteration

ValueSet
TryLookup fails
100000 iterations, 2ms, 0ms per iteration

TryLookup succeeds
100000 iterations, 3ms, 0ms per iteration

2021ms vs 2ms

Antonio is working on fixing this in this PR.

...
    struct map_view_base : iterable_base<D, Windows::Foundation::Collections::IKeyValuePair<K, V>, Version>
    {
        V Lookup(K const& key) const
        {
...
            if (pair == static_cast<D const&>(*this).get_container().end())
            {
                throw hresult_out_of_bounds();
            }

Reproducible example

Expected behavior

No response

Actual behavior

No response

Additional comments

No response

Activity

  1. kennykerr commented on Sep 17, 2025

    @kennykerr
    Collaborator

    Related workaround in Rust to avoid the cost of C++ exceptions: microsoft/windows-rs#3476

  2. ChrisGuzak commented on Sep 17, 2025

    @ChrisGuzak
    MemberAuthor

    thanks for pointing that out. I commented in that completed PR but repeating that here; making 2 calls is fine for in proc, but OOP APIs, where the cost per call is high, that is a problem. Antonio is making progress on a fix for this.

  3. github-actions commented on Sep 28, 2025

    @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.

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