[Dune] dynamic_cast cost
Carsten Gräser
graeser at mi.fu-berlin.de
Mon Nov 21 12:30:37 CET 2016
Hi,
Am 21.11.2016 um 11:55 schrieb Marco Cisternino:
> Hi Andreas,
> I'll try to prepare a simple example even if a simple example would not put this cost in evidence, I think.
> I experienced that cost by profiling my code, a finite volume discretization for Navier-Stokes equations. That dynamic_cast in updateFaceInfo costs 13% of the whole run (~5000 time iterations over ~2000 cells).
> UpdateFaceInfo is called by
> - Dune::LeafIntersectionIteratorWrapper<>::LeafIntersectionIteratorWrapper(LeafIntersectionIteratorWrapper<> const &)
> - Dune::ALU3dGridIntersectionIterator<>::increment()
>
> About your PS: can I replace dynamic_cast with reinterpret_cast without problems and see an improvement in performance?
yes and you can get the same with static_cast on a pointer
unless the result is checked for zero before being used.
Both just change the type of the pointer at compile type.
As a consequence both will allow the compiler to inline code
and have zero runtime cost. If the pointed to object does not
have the proper type this will invoke undefined behaviour.
In contrast a dynamic_cast will check at runtime if the pointed
to object has the proper type and return zero otherwise.
The runtime check and its consequence that code cannot be inlined
often impose some runtime cost. However, modern compiler do
quite a bit of work in trying to examine or guess the result
of a dynamic cast at compile time in order to avoid or minimize
this runtime cost (cf (speculative) devirtualization)
Best,
Carsten
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: OpenPGP digital signature
URL: <https://lists.dune-project.org/pipermail/dune/attachments/20161121/d84d5283/attachment.sig>
More information about the Dune
mailing list