diff mbox series

[RFA] libstdc++: small dynamic_cast optimization

Message ID 20220907014444.1469136-1-jason@redhat.com
State New
Headers show
Series [RFA] libstdc++: small dynamic_cast optimization | expand

Commit Message

Jason Merrill Sept. 7, 2022, 1:44 a.m. UTC
An email discussion of optimizing EH led me to wonder why we weren't doing
this already.  Not that this change affects EH at all.

Tested x86_64-pc-linux-gnu.  Does this seem worthwhile?

libstdc++-v3/ChangeLog:

	* libsupc++/dyncast.cc (__dynamic_cast): Avoid virtual function
	call in simple success case.
---
 libstdc++-v3/libsupc++/dyncast.cc | 6 ++++++
 1 file changed, 6 insertions(+)


base-commit: 0a2fba3697411c07a8330abfe7460ce62bce5e7f

Comments

Jonathan Wakely Sept. 7, 2022, 8:20 a.m. UTC | #1
On Wed, 7 Sept 2022 at 02:45, Jason Merrill via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> An email discussion of optimizing EH led me to wonder why we weren't doing
> this already.  Not that this change affects EH at all.
>
> Tested x86_64-pc-linux-gnu.  Does this seem worthwhile?

Yes, I think so. If there's a fast path, we should take it.

>
> libstdc++-v3/ChangeLog:
>
>         * libsupc++/dyncast.cc (__dynamic_cast): Avoid virtual function
>         call in simple success case.
> ---
>  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)
>
> base-commit: 0a2fba3697411c07a8330abfe7460ce62bce5e7f
> --
> 2.31.1
>
diff mbox series

Patch

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)