On 13.04.2016 18:19, Simon Richter wrote:
> Hi,
> 
> On 13.04.2016 18:13, Chris Pavlina wrote:
> 
>> What is the purpose of dyn_cast<> in include/core/typeinfo.h? Why don't we 
>> just
>> use dynamic_cast<>? And can we either replace the former with the latter, or
>> add a comment to the former explaining its purpose?
> 
> It uses the parallel type system in EDA_ITEM rather than RTTI, so it
> works if RTTI is broken, e.g. when compiling with gcc 2.95.
> 

I wrote it inspired with Clang/LLVM design which uses a very similar
pattern. Sorry Simon, I didn't consider compatibility with gcc 2.95
would be of an advantage. My reasons were:

1) Much faster (code in the attachment):
- dynamic_cast<> : 9090437 usecs
- dyn_cast<> : 1832433 usecs (5x improvement)

2) Lightweight & compatible with existing Kicad type system.

Cheers,
Tom
#include <cstdio>

#include "profile.h"
#include "typeinfo.h"

const int n_iter = 1000000000;

const int id_base = 0;
const int id_derived = 1;


class Base {
public:
    Base()
    {
	m_type = id_base;
    }

    virtual ~Base() {};

    static inline bool ClassOf( const Base* aItem )
    {
        return aItem && id_base == aItem->Type();
    }

    int Type() const
    {
	return m_type;
    }


protected:
    int m_type;
    int m_count;
};

class Derived : public Base {
public:
    static inline bool ClassOf( const Base* aItem )
    {
        return aItem && id_derived == aItem->Type();
    }

    Derived()
    {
	m_type = id_derived;
    }

    void DoSomething()
    {
	m_count++;
    }

};

main()
{
    Base *b = new Derived();

    prof_counter cnt;

    prof_start(&cnt);

    for(int i = 0; i <n_iter;i++)
    {
	Derived *d = dynamic_cast<Derived*> (b);
	d->DoSomething();
    }
    prof_end(&cnt);

    printf("dynamic_cast<> : %d usecs\n", cnt.usecs());

    prof_start(&cnt);


    for(int i = 0; i <n_iter;i++)
    {
	Derived *d = dyn_cast<Derived*>(b);
	d->DoSomething();
    }

    prof_end(&cnt);
    printf("dyn_cast<> : %d usecs\n", cnt.usecs());

}
_______________________________________________
Mailing list: https://launchpad.net/~kicad-developers
Post to     : kicad-developers@lists.launchpad.net
Unsubscribe : https://launchpad.net/~kicad-developers
More help   : https://help.launchpad.net/ListHelp

Reply via email to