<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 10, 2015 at 4:17 AM, Chandler Carruth via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: chandlerc<br>
Date: Thu Sep 10 06:17:49 2015<br>
New Revision: 247269<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=247269&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=247269&view=rev</a><br>
Log:<br>
[ADT] Rewrite the StringRef::find implementation to be simpler, clearer,<br>
and tremendously less reliant on the optimizer to fix things.<br>
<br>
The code is always necessarily looking for the entire length of the<br>
string when doing the equality tests in this find implementation, but it<br>
previously was needlessly re-checking the size each time among other<br>
annoyances.<br>
<br>
By writing this so simply an ddirectly in terms of memcmp, it also is<br>
about 8x faster in a debug build, which in turn makes FileCheck about 2x<br>
faster in 'ninja check-llvm'. </blockquote><div><br></div><div>Should we deliberately build FileCheck optimized by default even in debug builds? I think we do something like that for llvm-tblgen, maybe we could broaden that option/flag/support?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This saves about 8% of the time for<br>
FileCheck-heavy parts of the test suite like the x86 backend tests.<br>
<br>
Modified:<br>
llvm/trunk/lib/Support/StringRef.cpp<br>
<br>
Modified: llvm/trunk/lib/Support/StringRef.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringRef.cpp?rev=247269&r1=247268&r2=247269&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringRef.cpp?rev=247269&r1=247268&r2=247269&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Support/StringRef.cpp (original)<br>
+++ llvm/trunk/lib/Support/StringRef.cpp Thu Sep 10 06:17:49 2015<br>
@@ -140,37 +140,44 @@ std::string StringRef::upper() const {<br>
/// \return - The index of the first occurrence of \arg Str, or npos if not<br>
/// found.<br>
size_t StringRef::find(StringRef Str, size_t From) const {<br>
+ if (From > Length)<br>
+ return npos;<br>
+<br>
+ const char *Needle = Str.data();<br>
size_t N = Str.size();<br>
- if (N > Length)<br>
+ if (N == 0)<br>
+ return From;<br>
+<br>
+ size_t Size = Length - From;<br>
+ if (Size < N)<br>
return npos;<br>
<br>
+ const char *Start = Data + From;<br>
+ const char *Stop = Start + (Size - N + 1);<br>
+<br>
// For short haystacks or unsupported needles fall back to the naive algorithm<br>
- if (Length < 16 || N > 255 || N == 0) {<br>
- for (size_t e = Length - N + 1, i = std::min(From, e); i != e; ++i)<br>
- if (substr(i, N).equals(Str))<br>
- return i;<br>
+ if (Size < 16 || N > 255) {<br>
+ do {<br>
+ if (std::memcmp(Start, Needle, N) == 0)<br>
+ return Start - Data;<br>
+ ++Start;<br>
+ } while (Start < Stop);<br>
return npos;<br>
}<br>
<br>
- if (From >= Length)<br>
- return npos;<br>
-<br>
// Build the bad char heuristic table, with uint8_t to reduce cache thrashing.<br>
uint8_t BadCharSkip[256];<br>
std::memset(BadCharSkip, N, 256);<br>
for (unsigned i = 0; i != N-1; ++i)<br>
BadCharSkip[(uint8_t)Str[i]] = N-1-i;<br>
<br>
- unsigned Len = Length-From, Pos = From;<br>
- while (Len >= N) {<br>
- if (substr(Pos, N).equals(Str)) // See if this is the correct substring.<br>
- return Pos;<br>
+ do {<br>
+ if (std::memcmp(Start, Needle, N) == 0)<br>
+ return Start - Data;<br>
<br>
// Otherwise skip the appropriate number of bytes.<br>
- uint8_t Skip = BadCharSkip[(uint8_t)(*this)[Pos+N-1]];<br>
- Len -= Skip;<br>
- Pos += Skip;<br>
- }<br>
+ Start += BadCharSkip[(uint8_t)Start[N-1]];<br>
+ } while (Start < Stop);<br>
<br>
return npos;<br>
}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>