libcxxabi __dynamic_cast and random strings

While doing some performance profiling I ran into dynamic_cast being one of the hogs. I do know that dynamic_cast does typeid and string comparison on windows and it sucks. However on gcc it seems quite fast (haven’t checked the implementation there), became curious how is it implemented in libcxx in Clang.

Will do a separate post covering my findings soon, this post is about some random strings disassembler showed in XCode, as you can see there are some references to “Adobe Illustrator” and “Adobe Photoshop CS5″, wonder if this is just garbage.. Anyway..

/wp-content/uploads/2014/07/dynamic-cast-xcode-lq-1024x682.jpg

I think this code snippet from file, makes is quite clear why dynamic_cast can be so slow on Windows (things can become more fun when you have templates – they can generate very long symbol names for types):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// On Windows, typeids are different between DLLs and EXEs, so comparing
// type_info* will work for typeids from the same compiled file but fail
// for typeids from a DLL and an executable. Among other things, exceptions
// are not caught by handlers since can_catch() returns false.
//
// Defining _LIBCXX_DYNAMIC_FALLBACK does not help since can_catch() calls
// is_equal() with use_strcmp=false so the string names are not compared.

#ifdef _WIN32
#include <string.h>
#endif

namespace __cxxabiv1
{

#pragma GCC visibility push(hidden)

#if _LIBCXX_DYNAMIC_FALLBACK

inline
bool
is_equal(const std::type_info* x, const std::type_info* y, bool use_strcmp)
{
    if (!use_strcmp)
        return x == y;
    return strcmp(x->name(), y->name()) == 0;
}

#else  // !_LIBCXX_DYNAMIC_FALLBACK

inline
bool
is_equal(const std::type_info* x, const std::type_info* y, bool)
{
#ifndef _WIN32
    return x == y;
#else
    return (x == y) || (strcmp(x->name(), y->name()) == 0);
#endif
}