Why is dynamic_cast so dam slow?

Kevin Atkinson kevinatk@home.com
Sun Dec 26 13:15:00 GMT 1999


When I was profiling one of my programs I discovered that a good deal of
the CPU time was spend in dcast or type_info calls.  I have attached the
relevant parts of gprof output.   I have several questions

1) Why is it so slow?

2) Why does there even need to be a function call for dynamic_cast. 
Can't gcc get the relevant information from looking at the vtables in an
inline function.

3) Will this be improved with the new API.

Thanks again.  For now I solved the problem by storing the necessary
information directly in the base object and simply checking with it and
then doing a static_cast instead of calling a dynamic_cast.  My program
ran a good 20% faster by eliminating the time consuming dynamic_casts.

This was compiled with gcc 2.95.1 with "-O2 -g -pg".  If this is due to
a KNOWN bug that got you KNOW was fixed in gcc 2.95.2 let me know and I
will upgrade and try again.

-- 
Kevin Atkinson
kevinatk@home.com
http://metalab.unc.edu/kevina/
 Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
  8.22      0.44     0.44                             __class_type_info::dcast(type_info const &, int, void *, type_info const *, void *) const
  7.85      0.86     0.42    22419     0.02     0.02  autil::edit_distance(char const *, char const *, char const *, char const *, autil::EditDistanceWeights const &)
  5.23      1.14     0.28                             type_info::operator==(type_info const &) const
  4.86      1.40     0.26   667237     0.00     0.00  basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::replace(unsigned int, unsigned int, char const *, unsigned int)
  4.86      1.66     0.26   102137     0.00     0.02  aspell_default_suggest::Working::try_sound(basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> > const &, double)
  4.67      1.91     0.25   204274     0.00     0.00  aspell_default_writable_wl::WritableWS::words_w_soundslike(char const *) const
  4.67      2.16     0.25   144713     0.00     0.00  autil::FastEditDistanceStructure<autil::FastEditDistanceDoNothing, aspell_default_suggest::ToLower>::FastEditDistanceStructure(char const *, char const *, autil::FastEditDistanceDoNothing const &, aspell_default_suggest::ToLower const &)
  4.30      2.39     0.23   614484     0.00     0.00  basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::compare(basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> > const &, unsigned int, unsigned int) const
  3.93      2.60     0.21                             __si_type_info::dcast(type_info const &, int, void *, type_info const *, void *) const
  3.18      2.77     0.17   102137     0.00     0.00  autil::VectorHashTable<aspell_default_readonly_ws::ReadOnlyWS::SoundslikeLookupParms>::find_item(char const *const &) const
  2.99      2.93     0.16  1231135     0.00     0.00  aspell_default_suggest::compare(aspell_default_suggest::ScoreWordSound const &, aspell_default_suggest::ScoreWordSound const &)
  2.99      3.09     0.16   289426     0.00     0.00  aspell_default_suggest::ScoreWordSound::~ScoreWordSound(void)
  2.62      3.23     0.14   147107     0.00     0.00  slist<aspell_default_suggest::ScoreWordSound, allocator<aspell_default_suggest::ScoreWordSound> >::merge(slist<aspell_default_suggest::ScoreWordSound, allocator<aspell_default_suggest::ScoreWordSound> > &)
  2.43      3.36     0.13                             __dynamic_cast
  2.34      3.48     0.12                             __builtin_new
  2.24      3.60     0.12   246736     0.00     0.00  autil::MakeMultiVirEmulation<aspell::ws::InnerParms, aspell::ws::OuterParms>::next(void)
  1.68      3.69     0.09  1924083     0.00     0.00  autil::ClonePtr<autil::VirEmulation<char const *> >::~ClonePtr(void)
  1.68      3.79     0.09                             __user_type_info::dcast(type_info const &, int, void *, type_info const *, void *) const
  1.50      3.87     0.08   817096     0.00     0.00  autil::ClonePtr<autil::VirEmulation<char const *> >::operator=(autil::ClonePtr<autil::VirEmulation<char const *> > const &)
  1.50      3.94     0.08   252068     0.00     0.00  basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::replace(unsigned int, unsigned int, unsigned int, char)

index % time    self  children    called     name
                                                 <spontaneous>
[14]     8.2    0.44    0.00                 __class_type_info::dcast(type_info const &, int, void *, type_info const *, void *) const [14]
-----------------------------------------------
                                                 <spontaneous>
[19]     5.2    0.28    0.00                 type_info::operator==(type_info const &) const [19]
-----------------------------------------------
                                                 <spontaneous>
[24]     4.5    0.13    0.11                 __dynamic_cast [24]
                0.04    0.00  425226/425226      aspell::BasicWordSet type_info function [48]
                0.03    0.00  833777/833779      aspell::WordSet type_info function [55]
                0.01    0.00  209834/209834      aspell_default_readonly_ws::ReadOnlyWS type_info function [70]
                0.01    0.00  204275/204275      aspell_default_writable_repl::WritableReplS type_info function [71]
                0.01    0.00  419668/419668      aspell_default_writable_wl::WritableWS type_info function [79]
                0.01    0.00  408549/408549      aspell::BasicReplacementSet type_info function [80]
                0.00    0.00       2/2           aspell::WritableWordSet type_info function [756]
-----------------------------------------------
                                                 <spontaneous>
[26]     3.9    0.21    0.00                 __si_type_info::dcast(type_info const &, int, void *, type_info const *, void *) const [26]


More information about the Gcc mailing list