C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Get base class from std::any

From: Phil Endecott <std_proposals_list_at_[hidden]>
Date: Mon, 27 Mar 2023 18:30:22 +0100
Edward Catmur wrote:
> On Sun, 26 Mar 2023, 15:11 Phil Endecott, <std_proposals_list_at_[hidden]>
> wrote:
>>
>> Would an implementation that didn't throw-catch but used the
>> compiler-generated tables more directly be significantly more efficient?
>>
>
> Almost certainly; you'd still be paying a virtual function call and a data
> structure traversal in the case of multiple inheritance, but you'd save on
> exception allocation and (most pertinently) unwinding. Why not measure the
> difference? The Itanium abi function is std::type_info::__do_catch, iirc.

Some very quick benchmarks for any_cast:

std::any: 9 ns

My implementation of the std::any functionality, comparing typeids: 5 -
8 ns.

My implementation, as posted previously, using throw-catch to cast from
derived to base class: 1100 ns !!!

My implementation using std::type_info::__do_catch: 9 - 18 ns.

That's with g++ 10, on 64-bit Arm.

Clearly the throw-catch implementation has a huge overhead. Making it
possible to do this portably, i.e. by standardising the functionality
provided by std::type_info::__do_catch, would be useful IMO.

Thoughts anyone?


Regards, Phil.

Received on 2023-03-27 17:30:23