diff options
author | Jason Merrill <jason@redhat.com> | 2022-09-06 10:35:21 -0400 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2022-09-07 10:11:54 -0400 |
commit | 873d395c2976a8321cec03f21d77e11f746da7c0 (patch) | |
tree | b4dd2843a060eeb0fdc2aea4c3cf5d24f9b27b32 | |
parent | 0a91bdaf177409a2a5e7895bce4f0e7091b4b3ca (diff) | |
download | gcc-873d395c2976a8321cec03f21d77e11f746da7c0.zip gcc-873d395c2976a8321cec03f21d77e11f746da7c0.tar.gz gcc-873d395c2976a8321cec03f21d77e11f746da7c0.tar.bz2 |
libstdc++: small dynamic_cast optimization
This change speeds up the simple benchmark below by about 40%.
struct A { virtual ~A() {} };
struct B: A { } b;
A* ap = &b;
void *sink;
int main()
{
for (long i = 0; i < 4000000000L; ++i)
sink = dynamic_cast<B*>(ap);
}
libstdc++-v3/ChangeLog:
* libsupc++/dyncast.cc (__dynamic_cast): Avoid virtual function
call in simple success case.
-rw-r--r-- | libstdc++-v3/libsupc++/dyncast.cc | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/libstdc++-v3/libsupc++/dyncast.cc b/libstdc++-v3/libsupc++/dyncast.cc index 853c911..616e4c0 100644 --- a/libstdc++-v3/libsupc++/dyncast.cc +++ b/libstdc++-v3/libsupc++/dyncast.cc @@ -71,6 +71,12 @@ __dynamic_cast (const void *src_ptr, // object started from if (whole_prefix->whole_type != whole_type) return NULL; + // Avoid virtual function call in the simple success case. + if (src2dst >= 0 + && src2dst == -prefix->whole_object + && *whole_type == *dst_type) + return const_cast <void *> (whole_ptr); + whole_type->__do_dyncast (src2dst, __class_type_info::__contained_public, dst_type, whole_ptr, src_type, src_ptr, result); if (!result.dst_ptr) |