From: Piotr Rak <piotr.rak@gmail.com>
To: "Hite, Christopher" <Christopher.Hite@partner.commerzbank.com>
Cc: Richard Guenther <richard.guenther@gmail.com>, gcc@gcc.gnu.org
Subject: Re: optimization question: mpl
Date: Wed, 28 Jul 2010 18:17:00 -0000 [thread overview]
Message-ID: <AANLkTi=5t20XgJ-m4Zbe3F2f7OUBaHOEfkxoHK27pF8R@mail.gmail.com> (raw)
In-Reply-To: <824586E7387D1443B5DD024DAC75B91C139049@SE000319.ztb.icb.commerzbank.com>
Hi,
2010/7/28 Hite, Christopher <Christopher.Hite@partner.commerzbank.com>:
>> Generally without knowing the compiler version you are using
>> it is hard to tell.
> I'll use whatever's best. Right now I'm still on 4.4.3. I'll probably
> upgrade soon to 4.5.
>
>> The same is true without a complete compilable testcase.
> I didn't want to make a test case that depends on boost::mpl. How's
> this:
>
> struct DecodeContext;
>
> struct M1{
> static const int id=1;
> static void decode(DecodeContext& ){}
> };
>
> struct M2{
> static const int id=2;
> static void decode(DecodeContext& ){}
> };
>
> struct M3{
> static const int id=3;
> static void decode(DecodeContext& ){}
> };
>
>
> template <int> struct ListMember;
>
> template <> struct ListMember<1>{ typedef M1 type; };
> template <> struct ListMember<2>{ typedef M2 type; };
> template <> struct ListMember<3>{ typedef M3 type; };
>
> template<int i>
> void foo(int id, DecodeContext& dc) {
> typedef typename ListMember<i>::type M;
> if(M::id==id)
> M::decode(dc);
> else
> foo<i-1>(id,dc);
> }
>
> template<>
> void foo<0>(int id, DecodeContext& dc) {}
>
>
>
> int main(){
> DecodeContext& dc= *(DecodeContext*)0;// junk for now
> int id=2; //sometime runtime dependent
> foo<3>(id,dc);
> return 0;
> }
>
>
>> You can use the flatten attribute to tell the compiler to inline all
>> calls in a given function, like
>>
>> void __attribute__((flatten)) foo(void)
>> {
>> ...
>> decode1();
>> ...
>> }
>
> That would cause decode1() to be inlined, which might not be what you
> want.
>
> Hmm maybe I could rewrite things so the switch case returns a function
> pointer. I'm guessing that would make things slower though.
>
Or you could just initialize static array of pointers, like that
(please note, that I never compiled code below):
typedef void (*pfn) (DeclContext&);
tempate <int I>
struct InitDispatchHelper
{
static void init(std::tr1::array<pfn,N>& a)
{
a[I-1] = ListMemeber<I-1>::foo;
InitPointersHelper<I-1>::init(a);
}
};
// End recursion
template<>
struct InitDispatchHelper
{
static void init(std::tr1::array<pfn,N>& a)
{ /*does nothing */ }
};
// Dispatch table;
template <int N>
struct DispatchFoo : std::tr1::array<pfn, N>
{
DispatchFoo() : std::tr1::array<pfn, N>()
{
InitDispatchHelper<N>::init(*this);
}
};
then your call would look like:
static const int X = /* your maximal I in ListMembers meta-class */
void call_foo(int i, DeclContext& c)
{
static const DispatchFoo<X> foo_dispatch;
foo_dispatch[i] (c);
}
Bonus points for benchmarking both solutions and making it policy
class choosing between those two solutions depending on X.
Good luck,
Piotr
next prev parent reply other threads:[~2010-07-28 18:17 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-07-28 14:38 Hite, Christopher
2010-07-28 15:26 ` Richard Guenther
2010-07-28 15:53 ` Hite, Christopher
2010-07-28 17:26 ` Larry Evans
2010-07-28 18:17 ` Piotr Rak [this message]
2010-07-28 19:00 ` Larry Evans
2011-10-17 16:35 ` Hite, Christopher
2010-07-29 12:16 ` Larry Evans
2010-07-28 15:35 ` Larry Evans
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='AANLkTi=5t20XgJ-m4Zbe3F2f7OUBaHOEfkxoHK27pF8R@mail.gmail.com' \
--to=piotr.rak@gmail.com \
--cc=Christopher.Hite@partner.commerzbank.com \
--cc=gcc@gcc.gnu.org \
--cc=richard.guenther@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).