4

By this(How expensive is RTTI?), it seems clear that dynamic casting is much expensive than static type comparison, but I wonder if it would be worth to turn off RTTI option in compiler option(VS2010, /GR-)

I have no dynamic cast in my code(I replaced them with static cast). But does (/GR-) option do any other than emitting errors when using dynamic cast? Is there any memory or code optimization in there?

Thanks in advance.

Community
  • 1
  • 1
SeniorLee
  • 805
  • 1
  • 12
  • 25

2 Answers2

5

Straight from MSDN (emphasis mine):

When /GR is on, the compiler defines the _CPPRTTI preprocessor macro. By default, /GR is on. /GR- disables run-time type information.

Use /GR if the compiler cannot statically resolve an object type in your code. You usually need the /GR option when your code uses dynamic_cast Operator or typeid. However, /GR increases the size of the .rdata sections of your image. If your code does not use dynamic_cast or typeid, /GR- may produce a smaller image.

Looks like turning RTTI off is worth it in your case.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222
  • 1
    I am surprised there is no mention of *exceptions*. I would expect a `catch` clause to use RTTI beneath the scenes to check whether an exception matches the clause (at least, it does so in the Itanium ABI) so I wonder whether MSVC generates code that a) uses another mechanism for exception or b) uses a lightweight RTTI even if `/GR` is specified to support exceptions. – Matthieu M. Jan 28 '14 at 08:53
  • They seemed to have a separate option for that `/EH` (and `/GX`) which seems to point towards _uses another mechanism for exception_. – legends2k Jan 28 '14 at 08:56
1

There may be some memory decrease (since the data required for RTTI are not needed), but in most practical cases you will find it completely negligible, so in most practical cases it is not worth it.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51