public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* optimization question: mpl
@ 2010-07-28 14:38 Hite, Christopher
  2010-07-28 15:26 ` Richard Guenther
  2010-07-28 15:35 ` Larry Evans
  0 siblings, 2 replies; 9+ messages in thread
From: Hite, Christopher @ 2010-07-28 14:38 UTC (permalink / raw)
  To: gcc

 


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

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2011-10-17 15:58 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-28 14:38 optimization question: mpl 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
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

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).