1999Q1/
<!-- MHonArc v2.4.4 -->
<!--X-Subject: [MUD&#45;Dev] Re: Question on c++ switch optimization, and parsers in general. -->
<!--X-From-R13: Quevf Uenl <ptNnzv&#45;pt.UenlEntr.Sqzbagba.OP.QO> -->
<!--X-Date: Mon, 8 Feb 1999 19:31:57 &#45;0800 -->
<!--X-Message-Id: 199902090329.UAA00854@ami&#45;cg.GraySage.Edmonton.AB.CA -->
<!--X-Content-Type: text/plain -->
<!--X-Head-End-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>MUD-Dev message, [MUD-Dev] Re: Question on c++ switch optimization, and parsers</title>
<!-- meta name="robots" content="noindex,nofollow" -->
<link rev="made" href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">
</head>
<body background="/backgrounds/paperback.gif" bgcolor="#ffffff"
      text="#000000" link="#0000FF" alink="#FF0000" vlink="#006000">

  <font size="+4" color="#804040">
    <strong><em>MUD-Dev<br>mailing list archive</em></strong>
  </font>
      
<br>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
<br clear=all><hr>
<!--X-Body-Begin-->
<!--X-User-Header-->
<!--X-User-Header-End-->
<!--X-TopPNI-->

Date:&nbsp;
[&nbsp;<a href="msg00365.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00367.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Thread:&nbsp;
[&nbsp;<a href="msg00374.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00369.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Index:&nbsp;
[&nbsp;<A HREF="author.html#00366">Author</A>
&nbsp;|&nbsp;<A HREF="#00366">Date</A>
&nbsp;|&nbsp;<A HREF="thread.html#00366">Thread</A>
&nbsp;]

<!--X-TopPNI-End-->
<!--X-MsgBody-->
<!--X-Subject-Header-Begin-->
<H1>[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</H1>
<HR>
<!--X-Subject-Header-End-->
<!--X-Head-of-Message-->
<UL>
<LI><em>To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
<LI><em>Subject</em>: [MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</LI>
<LI><em>From</em>: Chris Gray &lt;<A HREF="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</A>&gt;</LI>
<LI><em>Date</em>: Mon, 8 Feb 1999 20:29:46 -0700</LI>
<LI><em>Reply-To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
</UL>
<!--X-Head-of-Message-End-->
<!--X-Head-Body-Sep-Begin-->
<HR>
<!--X-Head-Body-Sep-End-->
<!--X-Body-of-Message-->
<PRE>
[Richard Woolcock (KaVir):]

 &gt;I believe that switch statements are compiled down to the equivilent
 &gt;of if statements, ie:
 &gt;
 &gt;   switch ( x )
 &gt;   {
 &gt;      case 1: ...
 &gt;      case 2: ...
 &gt;      case 3: ...
 &gt;      default: ...
 &gt;   }
 &gt;
 &gt;Is no more efficient than:
 &gt;
 &gt;   if      ( x == 1 ) ...
 &gt;   else if ( x == 2 ) ...
 &gt;   else if ( x == 3 ) ...
 &gt;   else ...

 &gt;Some more modern compilers may treat switch statements better than
 &gt;the ones I'm used to - if so, I'd appreciate it if someone told me :)

Any compiler that still does that should be taken out and shot. Even
toy compilers do better. I very much doubt any commercial compiler will
do it. There are generally a small number of schemes that a compiler
will use to handle switch statements, depending on the density and
number of the index values. Some methods:

    - emit an inline table containing offsets to the code to be executed.
	Table is directly indexed by a multiple of the switch value,
	usually with a range-test to handle the default. A variant is
	to have branch instructions in the table, and to branch to
	the appropriate one which then branches to the final code.

    - emit an inline table containing index values and code offsets.
	Either call a run-time routine, or emit an in-line binary
	search to search the table. Similar alternate.

    - emit a sequence of instructions that implement an inline binary
	search of the index space directly, using inline constants.

    - emit a sequence of instructions to do a simple linear search, much
	as KaVir suggests. If the compiler does this for more than about
	4 or 5 tests, however, it should be shot. This would be done when
	the index values are such that, on average, this simple linear
	search is the fastest method.

I'm sure I'm missing some - I found lots when I was writing disassemblers
about 10 years ago. I used the first and second in my Draco compiler,
and my AmigaMUD bytecode has those as well.

--
Don't design inefficiency in - it'll happen in the implementation. - me

Chris Gray     cg#ami-cg,GraySage.Edmonton.AB.CA
               <A  HREF="http://www.GraySage.Edmonton.AB.CA/cg/">http://www.GraySage.Edmonton.AB.CA/cg/</A>


</PRE>

<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<HR>
<ul compact><li><strong>Follow-Ups</strong>:
<ul>
<li><strong><A NAME="00369" HREF="msg00369.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>
<ul compact><li><em>From:</em> "Adam J. Thornton" &lt;adam#phoenix,Princeton.EDU&gt;</li></ul>
</UL></LI></UL>
<!--X-Follow-Ups-End-->
<!--X-References-->
<!--X-References-End-->
<!--X-BotPNI-->
<UL>
<LI>Prev by Date:
<STRONG><A HREF="msg00365.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in  general.</A></STRONG>
</LI>
<LI>Next by Date:
<STRONG><A HREF="msg00367.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers i</A></STRONG>
</LI>
<LI>Prev by thread:
<STRONG><A HREF="msg00374.html">[MUD-Dev] Re: optimizing code</A></STRONG>
</LI>
<LI>Next by thread:
<STRONG><A HREF="msg00369.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></STRONG>
</LI>
<LI>Index(es):
<UL>
<LI><A HREF="index.html#00366"><STRONG>Date</STRONG></A></LI>
<LI><A HREF="thread.html#00366"><STRONG>Thread</STRONG></A></LI>
</UL>
</LI>
</UL>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
<ul><li>Thread context:
<BLOCKQUOTE><UL>
<LI><strong><A NAME="00375" HREF="msg00375.html">[MUD-Dev] Re: optimizing code</A></strong>, 
Chris Gray <a href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</a>, Wed 10 Feb 1999, 02:41 GMT
<LI><strong><A NAME="00371" HREF="msg00371.html">[MUD-Dev] code profiling</A></strong>, 
Chris Gray <a href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</a>, Tue 09 Feb 1999, 06:00 GMT
<UL>
<LI><strong><A NAME="00373" HREF="msg00373.html">[MUD-Dev] optimizing code</A></strong>, 
diablo <a href="mailto:diablo#best,com">diablo#best,com</a>, Tue 09 Feb 1999, 06:31 GMT
<UL>
<LI><strong><A NAME="00374" HREF="msg00374.html">[MUD-Dev] Re: optimizing code</A></strong>, 
Hans-Henrik Staerfeldt <a href="mailto:hhs#cbs,dtu.dk">hhs#cbs,dtu.dk</a>, Tue 09 Feb 1999, 14:58 GMT
</LI>
</UL>
</LI>
</UL>
</LI>
<LI><strong><A NAME="00366" HREF="msg00366.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>, 
Chris Gray <a href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</a>, Tue 09 Feb 1999, 03:31 GMT
<UL>
<LI><strong><A NAME="00369" HREF="msg00369.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>, 
Adam J. Thornton <a href="mailto:adam#phoenix,Princeton.EDU">adam#phoenix,Princeton.EDU</a>, Tue 09 Feb 1999, 04:34 GMT
<UL>
<LI><strong><A NAME="00372" HREF="msg00372.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>, 
Ben Greear <a href="mailto:greear#cyberhighway,net">greear#cyberhighway,net</a>, Tue 09 Feb 1999, 06:01 GMT
</LI>
</UL>
</LI>
</UL>
<UL>
<li>&lt;Possible follow-up(s)&gt;<br>
<LI><strong><A NAME="00368" HREF="msg00368.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>, 
Chris Gray <a href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</a>, Tue 09 Feb 1999, 03:54 GMT
<UL>
<LI><strong><A NAME="00370" HREF="msg00370.html">[MUD-Dev] Re: Question on c++ switch optimization, and parsers in general.</A></strong>, 
T. Alexander Popiel <a href="mailto:popiel#snugharbor,com">popiel#snugharbor,com</a>, Tue 09 Feb 1999, 04:41 GMT
</LI>
</UL>
</LI>
</UL>
</LI>
</UL></BLOCKQUOTE>

</ul>
<hr>
<center>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
</center>
<hr>
</body>
</html>