<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">From a pure syntactic point of view, yes.<div class="">But it is meaningless as you are trying to think about a higher-level language construct and redundant check while you should really have a look at the optimized code if you're concerned with performance.</div><div class=""><br class=""></div><div class="">-- </div><div class="">Mehdi</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 10, 2016, at 9:57 AM, Anupama Chandrasekhar <<a href="mailto:anupama.lists@gmail.com" class="">anupama.lists@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">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 class=""><br class=""></div><div class="">Thanks.</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, Feb 8, 2016 at 12:00 PM, Anupama Chandrasekhar <span dir="ltr" class=""><<a href="mailto:anupama.lists@gmail.com" target="_blank" class="">anupama.lists@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="">The point I was wondering about is, say in the example, say the input string is "de"</div><div class=""><br class=""></div><div class="">int i = StringSwitch<int>("de")</div><div class=""><span style="white-space:pre-wrap" class="">                      </span>.case("de", 1)</div><div class=""><span style="white-space:pre-wrap" class="">                   </span>.case("fghi", 2)</div><div class=""><span style="white-space:pre-wrap" class="">                 </span>.case("jkl", 3)</div><div class=""><span style="white-space:pre-wrap" class="">                  </span>.default(-1);</div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">int len = strlen(str);</div><div class="">if(len == 2 && std::memcmp(str, "de", 2) {</div><div class="">   i = 1;</div><div class="">} else if(len == 4 && std::memcmp(str, "fghi", 4) {</div><div class="">   i = 2;</div><div class="">} else if(len == 3 && std::memcmp(str, "jkl", 3) {</div><div class="">   i = 3;</div><div class="">} else {</div><div class="">   i = -1</div><div class="">}</div><div class=""><br class=""></div></span><div class="">I would be performing only one if check.</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, Feb 8, 2016 at 10:05 AM, Chris Lattner <span dir="ltr" class=""><<a href="mailto:clattner@apple.com" target="_blank" class="">clattner@apple.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br class="">
> On Feb 5, 2016, at 4:42 PM, Mehdi Amini via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>> wrote:<br class="">
><br class="">
><br class="">
>> On Feb 5, 2016, at 2:43 PM, Anupama Chandrasekhar via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>> wrote:<br class="">
>><br class="">
>> Hi:<br class="">
>><br class="">
>> 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 class="">
><br class="">
> 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 class="">
> 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 class="">
><br class="">
> int i = StringSwitch<int>("abc").case("de", 1).case("fghi", 2).case("jkl", 3).default(-1);<br class="">
><br class="">
> and:<br class="">
><br class="">
> int i;<br class="">
> const char *str = "abc";<br class="">
> int len = strlen(str);<br class="">
> if(len == 2 && std::memcmp(str, "de", 2) {<br class="">
>   i = 1;<br class="">
> } else if(len == 4 && std::memcmp(str, "fghi", 4) {<br class="">
>   i = 2;<br class="">
> } else if(len == 3 && std::memcmp(str, "jkl", 3) {<br class="">
>   i = 3;<br class="">
> } else {<br class="">
>   i = -1<br class="">
> }<br class="">
<br class="">
</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 class="">
<br class="">
<br class="">
int len = strlen(str);<br class="">
switch (len) {<br class="">
case 2:<br class="">
   if (std::memcmp(str, "de", 2)<br class="">
     i = 1;<br class="">
   else (std::memcmp(str, “qr", 2)<br class="">
     i = 1;<br class="">
   else<br class="">
     i = -1;<br class="">
  case 4:<br class="">
..<br class="">
<br class="">
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 class="">
<span class=""><font color="#888888" class=""><br class="">
-Chris<br class="">
<br class="">
</font></span></blockquote></div><br class=""></div>
</div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></body></html>