kicad-developers team mailing list archive
-
kicad-developers team
-
Mailing list archive
-
Message #24180
Re: dyn_cast
-
To:
Simon Richter <Simon.Richter@xxxxxxxxxx>, <kicad-developers@xxxxxxxxxxxxxxxxxxx>
-
From:
Tomasz Wlostowski <tomasz.wlostowski@xxxxxxx>
-
Date:
Wed, 13 Apr 2016 18:34:28 +0200
-
Authentication-results:
spf=pass (sender IP is 188.184.36.50) smtp.mailfrom=cern.ch; lists.launchpad.net; dkim=none (message not signed) header.d=none;lists.launchpad.net; dmarc=bestguesspass action=none header.from=cern.ch;
-
In-reply-to:
<570E7198.4090402@hogyros.de>
-
Spamdiagnosticmetadata:
NSPM
-
Spamdiagnosticoutput:
1:23
-
User-agent:
Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1
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());
}
Follow ups
References