From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30506 invoked by alias); 28 Jul 2010 14:38:13 -0000 Received: (qmail 30485 invoked by uid 22791); 28 Jul 2010 14:38:12 -0000 X-SWARE-Spam-Status: No, hits=-5.6 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail.commerzbank.com (HELO mail.commerzbank.com) (212.149.50.150) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 28 Jul 2010 14:37:41 +0000 Received: by mail.commerzbank.com (Commerzbank Mail-System, from userid 1002) id 8B1E030332; Wed, 28 Jul 2010 16:37:38 +0200 (CEST) X-Spam-Virus: No Received: from mail.commerzbank.com (localhost [127.0.0.1]) by mail.commerzbank.com (Commerzbank Mail-System) with ESMTP id 46C1F3031A for ; Wed, 28 Jul 2010 16:37:36 +0200 (CEST) Received: from pfx2.commerzbank.com (intern.postfix.commerzbank.com [172.16.71.213]) by mail.commerzbank.com (Commerzbank Mail-System) with ESMTPS id 3ECEE3027F for ; Wed, 28 Jul 2010 16:37:36 +0200 (CEST) Received: by pfx2.commerzbank.com (Commerzbank Internal Mail-System, from userid 1001) id 0CEA61FDA86; Wed, 28 Jul 2010 16:37:36 +0200 (CEST) Received: from sv057100.ztb.icb.commerzbank.com (sv057100.ztb.icb.commerzbank.com [140.13.159.190]) by pfx2.commerzbank.com (Commerzbank Internal Mail-System) with ESMTP id F370A1FDA96 for ; Wed, 28 Jul 2010 16:37:35 +0200 (CEST) Received: from SE000319.ztb.icb.commerzbank.com ([140.26.56.30]) by sv057100.ztb.icb.commerzbank.com with Microsoft SMTPSVC(6.0.3790.4675); Wed, 28 Jul 2010 16:37:35 +0200 MIME-Version: 1.0 Subject: optimization question: mpl Date: Wed, 28 Jul 2010 14:38:00 -0000 Message-ID: <824586E7387D1443B5DD024DAC75B91C139048@SE000319.ztb.icb.commerzbank.com> From: "Hite, Christopher" To: Content-class: urn:content-classes:message Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org X-SW-Source: 2010-07/txt/msg00400.txt.bz2 =20 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=3D1; 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=3D=3D1) Packet1::decode(); else=20 decode2(id); } What I'm hoping is that the compiler is smart enough=20 * 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