Bug 1988

Summary: Optimization for POA_T::_downcast
Product: TAO Reporter: Carlos O'Ryan <coryan>
Component: IDL CompilerAssignee: Ossama Othman <ossama.othman>
Status: RESOLVED FIXED    
Severity: enhancement    
Priority: P3    
Version: 1.4.2   
Hardware: All   
OS: All   
Bug Depends on: 1369    
Bug Blocks:    

Description Carlos O'Ryan 2004-11-22 09:02:46 CST
The servant classes in TAO implement _downcast (a form of dynamic_cast) using
strings.   The first question to ask is why not use dynamic_cast?  Shouldn't
that be at least as efficient as using a bunch of string comparisons?  The
footprint should go down too.

If dynamic_cast is not adequate then I suggest using function pointers as type
identifiers, as follows:

// IDL
interface A {};
interface B : A {};

// C++
typedef char const * (servant_type_id)();

class POA_A {
...
  static inline char const * _id() { return "IDL:A:1.0"; }

  void * _downcast(servant_type_id id) {
    if (id == &POA_A::_id) {
      return static_cast<POA_A*>(this);
    }
    ....
  }
};


class POA_B : public POA_A {
...
  static inline char const * _id() { return "IDL:B:1.0"; }

  void * _downcast(servant_type_id id) {
    if (id == &POA_B::_id) {
      return static_cast<POA_B*>(this);
    }
    if (id == &POA_A::_id) {
      return static_cast<POA_A*>(this);
    }
    ....
  }
};
Comment 1 Ossama Othman 2004-11-23 22:34:37 CST
Jeff asked me to take care of this one since I'm currently working on skeleton
refactoring detailed in bug 1369.
Comment 2 Ossama Othman 2004-11-23 22:35:07 CST
Mine.
Comment 3 Johnny Willemsen 2004-12-01 08:19:55 CST
When the dynamic cast could be used, the I think it makes use to reimplement 
also in tao/SystemException.cpp

CORBA::SystemException::_tao_get_omg_exception_description

Here a huge list with _is_a is used. The _downcast would then be much more 
efficient to be used.
Comment 4 Ossama Othman 2005-02-11 01:34:00 CST
Johnny, using dynamic_cast<> in the _tao_get_omg_exception_description() method
would be more efficient but doing so also increases footprint slightly by a few
bytes.  I confirmed the footprint increase with g++ 3.4.3.

That said, I did remove the _is_a() calls in the exception _downcast() methods.
 Since we're doing a dynamic_cast<> on a pointer, the cast won't throw and will
provide the exact behavior required by _downcast(), meaning that the _is_a()
calls are redundant.  Implementing this change for each of the exception
_downcast() methods and inlining them results in a 6K drop for
SystemException.o.  Not much, but every little bit helps.
Comment 5 Ossama Othman 2005-02-25 02:05:24 CST
Fixed.  All all downcast operations now use dynamic_cast<>.
Comment 6 Ossama Othman 2005-02-25 02:06:39 CST
Really fixed.  :-)