<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2448.0">
<TITLE>RE: [LCP]wrapper around strcmp</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>&gt; Definitely, I dont want my program to halt. Actually I myself </FONT>
<BR><FONT SIZE=2>&gt; am not clear </FONT>
<BR><FONT SIZE=2>&gt; about the usage of 'assert'. 'assert.h' has this macro,</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; #define assert(EX) (void)((EX)||(__assert(#EX, __FILE__, __LINE__),0))</FONT>
<BR><FONT SIZE=2>&gt; </FONT>
<BR><FONT SIZE=2>&gt; I didnt understand this. Can you please say more on this?</FONT>
</P>

<P><FONT SIZE=2>Most probably, this bit was only defined within</FONT>
<BR><FONT SIZE=2>#ifdef DEBUG</FONT>
<BR><FONT SIZE=2>#endif</FONT>
<BR><FONT SIZE=2>right ?</FONT>
<BR><FONT SIZE=2>In non DEBUG builds, you will get something along the lines of:</FONT>
<BR><FONT SIZE=2>#define assert(x) ((void)0)</FONT>
<BR><FONT SIZE=2>or</FONT>
<BR><FONT SIZE=2>#define assert(x) do {}while(0)</FONT>
</P>

<P><FONT SIZE=2>The point is that assert is only compiled in in DEBUG builds to</FONT>
<BR><FONT SIZE=2>generate code that traps errors in the program. For instance,</FONT>
<BR><FONT SIZE=2>checking that a char* is non NULL when it should not. These tests</FONT>
<BR><FONT SIZE=2>are never included in non DEBUG builds, so don't slow the program</FONT>
<BR><FONT SIZE=2>down.</FONT>
</P>

<P><FONT SIZE=2>About the definition of it, the particular one you have relies on</FONT>
<BR><FONT SIZE=2>the short circuit evaluation of &amp;&amp; and || in C. Since the || of</FONT>
<BR><FONT SIZE=2>two expressions is true if either of its components is true, the</FONT>
<BR><FONT SIZE=2>program will generate code that doesn't bother evaluating the</FONT>
<BR><FONT SIZE=2>second one if the first one yields true. Therefore, code will</FONT>
<BR><FONT SIZE=2>evaluate EX, and only call __assert (which probably abort()s or</FONT>
<BR><FONT SIZE=2>sends a signal or something similar) if the first expression</FONT>
<BR><FONT SIZE=2>actually yields false (as the truth value of the full expression</FONT>
<BR><FONT SIZE=2>is then not known).</FONT>
<BR><FONT SIZE=2>#EX is a construst which makes the compiler emit the string</FONT>
<BR><FONT SIZE=2>representation of the expression you have, __FILE__ and __LINE__</FONT>
<BR><FONT SIZE=2>are the textual file name and line number where the macro was</FONT>
<BR><FONT SIZE=2>called (in the un-cpp'd original source file). __assert will</FONT>
<BR><FONT SIZE=2>probably print those three strings to help you pinpoint what</FONT>
<BR><FONT SIZE=2>exactly happened.</FONT>
</P>

<P><FONT SIZE=2>To get back to your first question, if you want speed, you might</FONT>
<BR><FONT SIZE=2>want to create your own implementation of strings, which builds</FONT>
<BR><FONT SIZE=2>on knolwedge you have of your strings, that the libc can't assume.</FONT>
<BR><FONT SIZE=2>For instance, if you have only constant strings that you don't</FONT>
<BR><FONT SIZE=2>add to, remove, or modify, you can make an array of these, and</FONT>
<BR><FONT SIZE=2>pass around only the indices in the array of the strings. Comparing</FONT>
<BR><FONT SIZE=2>two strings then boils down to integer comparisons. You can also</FONT>
<BR><FONT SIZE=2>create a structure for each string which holds the string itself,</FONT>
<BR><FONT SIZE=2>and a hash of it, so comparing the strings means comparing the</FONT>
<BR><FONT SIZE=2>hash values. Of course, if you modify strings a lot and compare</FONT>
<BR><FONT SIZE=2>not that much, it will be slower than plain strcmp.</FONT>
<BR><FONT SIZE=2>Another solution is to make sure you only have dynamic strings</FONT>
<BR><FONT SIZE=2>(eg malloc allocated, no string constants), and allocate by chunks</FONT>
<BR><FONT SIZE=2>of 4 bytes (or whatever the word size is on your typical machine),</FONT>
<BR><FONT SIZE=2>make sure you zero out the last ones (not only the ANSI zero</FONT>
<BR><FONT SIZE=2>terminator), then do compares (a la strcmp) but using word compares</FONT>
<BR><FONT SIZE=2>instead of char compares. OF course, you have to make sure you have</FONT>
<BR><FONT SIZE=2>a full zeroed word, aligned, to be sure not to miss the ending null</FONT>
<BR><FONT SIZE=2>for some string lengths. Bar cache misses, this should get roughly</FONT>
<BR><FONT SIZE=2>4 times faster. It all depends on what you do, with what strings,</FONT>
<BR><FONT SIZE=2>etc. Oh, I'm thinking of another possibility: store the length of</FONT>
<BR><FONT SIZE=2>the string, and early out if the lengths are different.</FONT>
<BR><FONT SIZE=2>There are plenty of solutions, really, when you know your data.</FONT>
</P>

<P><FONT SIZE=2>-- </FONT>
<BR><FONT SIZE=2>Vincent Penquerc'h </FONT>
</P>

</BODY>
</HTML>