public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-2521] libstdc++: small dynamic_cast optimization Date: Wed, 7 Sep 2022 14:12:58 +0000 (GMT) [thread overview] Message-ID: <20220907141258.5B707385841A@sourceware.org> (raw) https://gcc.gnu.org/g:873d395c2976a8321cec03f21d77e11f746da7c0 commit r13-2521-g873d395c2976a8321cec03f21d77e11f746da7c0 Author: Jason Merrill <jason@redhat.com> Date: Tue Sep 6 10:35:21 2022 -0400 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. Diff: --- libstdc++-v3/libsupc++/dyncast.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libstdc++-v3/libsupc++/dyncast.cc b/libstdc++-v3/libsupc++/dyncast.cc index 853c911a4cf..616e4c05766 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)
reply other threads:[~2022-09-07 14:12 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220907141258.5B707385841A@sourceware.org \ --to=jason@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).