bugGNU Octave - Bugs: bug #62146, dynamic_cast is performance kill


bug #62146: dynamic_cast is performance kill

Submitter:  None
Submitted:  Mon 07 Mar 2022 08:22:41 AM UTC
Category:  Performance Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Performance
Status:  None Assigned to:  None
Originator Name:  Originator Email:  -email is unavailable-
Open/Closed:  * Open Release:  * dev
Operating System:  * Any
* Mandatory Fields

Add a New Comment (Rich Markup)

Comment Type & Canned Response:


Mon 07 Mar 2022 12:22:40 PM UTC, comment #1: 

For your point 1, see also patch #10139.

Markus M├╝tzel <mmuetzel>
Project Member
Mon 07 Mar 2022 08:22:41 AM UTC, original submission:  

"dynamic_cast" is present in many places in Octave source. It has been proven that dynamic_cast degrades the performance [1]. So replacing it with alternative solutions potentially improves the overall performance of Octave.
Generally dynamic cast has two applications:

1. Casting something to other thing (both are related by inheritance):

ClassB& b = dynamic_cast<ClassA&> (a);

An exception may be thrown if the cast (from reference type) isn't successful. This pattern is used dominantly in libinterp/operators/* and some other places.
It simply can be replaced by static_cast that has no cost.

2. Checking if an object is of some type (or related to some other type by inheritance):

bool flag = dynamic_cast<ClassA*> (ptr);
if (flag)
  do_something ();

Here no exception is throw in the case of failure.
This pattern can be replaced by virtual functions. The cost of virtual function is much less than dynamic_cast. "octave_base_value" has many of these virtual functions like: "is_defined" , "is_function" ,... . If there is no virtual function for some cases the virtual function "type_id()" can be used:

bool flag = ptr->type_id () == classA::static_type_id ();
if (flag)
  do_something ();

Or possibly more efficient:

static const int classA_id = classA::static_type_id ();
bool flag = ptr->type_id () == classA_id;
if (flag)
  do_something ();

dynamic_cast can also be used for sidecast that I think there is no or very limited such cases in Octave.

[1] https://stackoverflow.com/questions/4050901/performance-of-dynamic-cast



(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


   patch dependencies.

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by mmuetzel (Posted a comment)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only project members can vote.





    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2022-03-07 mmuetzel Dependencies- Depends on patch #10139

    Back to the top

    Powered by Savane 3.9