I had not, but here is some code which I compiled with the same gcc version, 
the only difference being that in one, I had the optimization enabled:


#include <x86intrin.h>
#include <iostream>
#include <chrono>

struct A {
  virtual ~A() {}
};

struct B final : A {
  virtual ~B() {}
};

B* __attribute__ ((noinline)) dc(A* a) {
  return dynamic_cast<B*>(a);
}

int main() {
  auto start = std::chrono::high_resolution_clock::now();
  B* objb = new B();
  long long a = 0;
  for(size_t i = 0; i < 100000; i++)
    a = (long long)dc(objb);
  auto end = std::chrono::high_resolution_clock::now();
  std::chrono::duration<double, std::milli> duration = end - start;
  std::cout << "time: " << duration.count() << std::endl;
  return a&0xFF;
}


These tests resulted in the unoptimized binary having ~3x longer runtime. Very 
scrappy and inconclusive tests of course, though the optimized assembly is just 
so much simpler and shorter, I would be very surprised if it was slower (clang 
has it enabled by default as well). Let me know if you'd like the code snippet 
implementing the optimization to experiment with yourself.


________________________________
From: Jonathan Wakely <jwakely....@gmail.com>
Sent: 14 July 2025 14:48:34
To: Thomas de Bock
Cc: gcc@gcc.gnu.org
Subject: [ext] Re: Addition of compiler flag for dynamic_cast optimization

On Mon, 14 Jul 2025 at 14:57, Thomas de Bock via Gcc <gcc@gcc.gnu.org> wrote:
>
> Hello all, I've been looking into a compiler optimization implemented by 
> clang but not gcc that substitutes a __dynamic_cast call for a simple vtable 
> ptr comparison in the case that the class being cast to is final.
>
> However this brings issues with it in some cases (specified in thread). I 
> believe having the optimization disabled by default, but adding a compiler 
> flag to enable it would be ideal. Code for the optimization is mostly done so 
> I'm looking to have this optimization available in gcc somehow now, 
> interested in hearing any opinions on this :)
>
> thread link: 
> https://urldefense.com/v3/__https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63164*add_comment__;Iw!!EvhwMw!TqfOor9Os_rUZhzAVS2Yy0v1wFmG4XMNlMaw3rzG68jx4woxpnRBaoDMK9_wUOwqHyLnWyj85s-LrQR2toU$

I agree that we don't want to perform the optimization by default, but
it would be useful to have an option to enable it when you know it's
safe to do so.

Have you done any measurements to show that the optimization is worth it?

This e-mail and any attachments may contain information that is confidential 
and proprietary and otherwise protected from disclosure. If you are not the 
intended recipient of this e-mail, do not read, duplicate or redistribute it by 
any means. Please immediately delete it and any attachments and notify the 
sender that you have received it by mistake. Unintended recipients are 
prohibited from taking action on the basis of information in this e-mail or any 
attachments. The DRW Companies make no representations that this e-mail or any 
attachments are free of computer viruses or other defects.

Reply via email to