This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
optimization question: mpl
- From: "Hite, Christopher" <Christopher dot Hite at partner dot commerzbank dot com>
- To: <gcc at gcc dot gnu dot org>
- Date: Wed, 28 Jul 2010 16:37:35 +0200
- Subject: optimization question: mpl
I'm writing a decoder using a meta programming techniques alla
boost::mpl and I'd like to know if I'm asking too much of the compiler.
Basically I've got lots of packet types with different ids. The
classical way to write these would be a switch case
int id;
switch(id){
case Id1: decode1() break;
case Id2: decode2() break;
...
}
I'm tring to use the template compiler to generate equivalent code. I
have a mpl::list of packet descriptions like this:
struct Packet1{
static const int id=1;
void decode();
};
I then use boost::mpl::fold to generate a decode function which might
look sort of like this:
void decode1(int id){
if(id==1)
Packet1::decode();
else
decode2(id);
}
What I'm hoping is that the compiler is smart enough
* to inline all decode*() calls into one function
* to notice I compare id to N constants and use switch case optimization
(binary search or lookup table)
Am I asking too much?
Do I have to watch for any pitfalls that willl break the optimization,
like making id a member of a class or leaving out the 'else' ?
I could write more complicated meta-code which sorts by id and does a
runtime binary search, that would probably prevent the optimizer from
doing anything smarter.
Chris Hite