octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #62146] dynamic_cast is performance kill


From: anonymous
Subject: [Octave-bug-tracker] [bug #62146] dynamic_cast is performance kill
Date: Mon, 7 Mar 2022 03:22:43 -0500 (EST)

URL:
  <https://savannah.gnu.org/bugs/?62146>

                 Summary: dynamic_cast is performance kill
                 Project: GNU Octave
            Submitted by: None
            Submitted on: 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: 
             Open/Closed: Open
                 Release: dev
         Discussion Lock: Any
        Operating System: Any

    _______________________________________________________

Details:

"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




    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?62146>

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]