What can make C++ RTTI undesirable to use?

后端 未结 4 1580
梦毁少年i
梦毁少年i 2020-12-02 07:25

Looking at the LLVM documentation, they mention that they use \"a custom form of RTTI\", and this is the reason they have isa<>, cast<>

4条回答
  •  粉色の甜心
    2020-12-02 08:18

    The predominant reason is that they struggle to keep memory usage as low as possible.

    RTTI is only available for classes which feature at least one virtual method, which means that instances of the class will contain a pointer to the virtual table.

    On a 64-bits architecture (which is common today), a single pointer is 8 bytes. Since the compiler instantiate lots of small objects, this adds up pretty quickly.

    Therefore there is an ongoing effort to remove virtual functions as much as possible (and practical), and implement what would have been virtual functions with the switch instruction, which has similar execution speed but a significantly lower memory impact.

    Their constant worry for memory consumption has paid off, in that Clang consumes significantly less memory than gcc, for example, which is important when you offer the library to clients.

    On the other hand, it also means that adding a new kind of node usually results in editing code in a good number of files because each switch need to be adapted (thankfully compilers issue warning if you miss an enum member in a switch). So they accepted to make maintenance a tad more difficult in the name of memory efficiency.

提交回复
热议问题