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:  Fixed Assigned to:  None
Originator Name:  Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * dev
Operating System:  * Any Fixed Release:  9.1.0
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Thu 02 Mar 2023 04:42:20 PM UTC, comment #3: 

I'm closing as fixed because the casts in the operator functions have been fixed.  There, we can avoid dynamic_cast because the types are already supposed to be correct due to the design of operator dispatching.  I'm sure there are other places where we can make the change to static_cast, but the effects are probably less significant and we can look at them individually and I think some of Petter's remaining patches take care of a few more cases.

John W. Eaton <jwe>
Group administrator
Thu 02 Mar 2023 03:33:10 PM UTC, comment #2: 

Patch #10139 was committed. Can this report be closed? Or are there still places that might be worth changing?

Markus Mützel <mmuetzel>
Group administrator
Mon 07 Mar 2022 12:22:40 PM UTC, comment #1: 

For your point 1, see also patch #10139.

Markus Mützel <mmuetzel>
Group administrator
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

Anonymous

 

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

Attach Files:
   
   
Comment:
   

No files currently attached

 

Digest:
   patch dependencies.

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by jwe (Posted a comment)
  • -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 group members can vote.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-03-02 jwe StatusNone Fixed
        Open/ClosedOpen Closed
        Fixed ReleaseNone 9.1.0
    2022-03-07 mmuetzel Dependencies- Depends on patch #10139

    Back to the top

    Powered by Savane 3.13-3230.
    Corresponding source code