<div dir="ltr">Sorry for belaboring on a possibly minor point here, but is my understanding correct that even assuming that the case function is always inlined so we don't have extra function call overhead, we have the redundant if (!Result) checks when we use StringSwitch as opposed to a bunch of if- elses.<div><br></div><div>Thanks.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 8, 2016 at 12:00 PM, Anupama Chandrasekhar <span dir="ltr"><<a href="mailto:anupama.lists@gmail.com" target="_blank">anupama.lists@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>The point I was wondering about is, say in the example, say the input string is "de"</div><div><br></div><div>int i = StringSwitch<int>("de")</div><div><span style="white-space:pre-wrap"> </span>.case("de", 1)</div><div><span style="white-space:pre-wrap"> </span>.case("fghi", 2)</div><div><span style="white-space:pre-wrap"> </span>.case("jkl", 3)</div><div><span style="white-space:pre-wrap"> </span>.default(-1);</div><div><br></div><div>will cause the 3 function calls to "Case()" and 1 to "Default()", Even if the functions were inlined I would perform an if(!Result) check though Result has been found, however if I were to code the same as:</div><span class=""><div><br></div><div>int len = strlen(str);</div><div>if(len == 2 && std::memcmp(str, "de", 2) {</div><div> i = 1;</div><div>} else if(len == 4 && std::memcmp(str, "fghi", 4) {</div><div> i = 2;</div><div>} else if(len == 3 && std::memcmp(str, "jkl", 3) {</div><div> i = 3;</div><div>} else {</div><div> i = -1</div><div>}</div><div><br></div></span><div>I would be performing only one if check.</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 8, 2016 at 10:05 AM, Chris Lattner <span dir="ltr"><<a href="mailto:clattner@apple.com" target="_blank">clattner@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span><br>
> On Feb 5, 2016, at 4:42 PM, Mehdi Amini via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
><br>
><br>
>> On Feb 5, 2016, at 2:43 PM, Anupama Chandrasekhar via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>><br>
>> Hi:<br>
>><br>
>> I have a question about the llvm StringSwitch class. Why is this more efficient than comparing the hashes of the strings or just using a bunch of if statements.<br>
><br>
> I don't know about hashes performance (comparing a small string is probably more efficient than comparing hashes, and I'd be reluctant to collisions anyway). Now since you mentioned a "bunch of if statements" you can get very close.<br>
> StringSwitch is caching the length of the string to compare and first checking the length before doing the actual comparison using memcmp. So the two constructs below it should be equivalent:<br>
><br>
> int i = StringSwitch<int>("abc").case("de", 1).case("fghi", 2).case("jkl", 3).default(-1);<br>
><br>
> and:<br>
><br>
> int i;<br>
> const char *str = "abc";<br>
> int len = strlen(str);<br>
> if(len == 2 && std::memcmp(str, "de", 2) {<br>
> i = 1;<br>
> } else if(len == 4 && std::memcmp(str, "fghi", 4) {<br>
> i = 2;<br>
> } else if(len == 3 && std::memcmp(str, "jkl", 3) {<br>
> i = 3;<br>
> } else {<br>
> i = -1<br>
> }<br>
<br>
</span>Also, note that this sequence is specifically intended to be jump threadable and recognized by the compiler’s optimizer. This allows the compiler to turn it into:<br>
<br>
<br>
int len = strlen(str);<br>
switch (len) {<br>
case 2:<br>
if (std::memcmp(str, "de", 2)<br>
i = 1;<br>
else (std::memcmp(str, “qr", 2)<br>
i = 1;<br>
else<br>
i = -1;<br>
case 4:<br>
..<br>
<br>
Which isn’t optimal perhaps, but isn’t bad either. The compiler should theoretically be able to do good things with back-to-back memcmps, but probably isn’t doing anything good there for long ones. Short memcmps should be turned into a load+compare, meaning they become another switch.<br>
<span><font color="#888888"><br>
-Chris<br>
<br>
</font></span></blockquote></div><br></div>
</div></div></blockquote></div><br></div>