[PATCH] D133660: [Support] Add fast path for StringRef::find with needle of length 2.
    Tatsuyuki Ishi via Phabricator via llvm-commits 
    llvm-commits at lists.llvm.org
       
    Sun Sep 18 08:41:05 PDT 2022
    
    
  
ishitatsuyuki marked an inline comment as done.
ishitatsuyuki added inline comments.
================
Comment at: llvm/lib/Support/StringRef.cpp:165
+      HaystackWord = HaystackWord << 16 | (uint8_t) * ++Start;
+    return NeedleWord == HaystackWord ? Start - Data - 1 : npos;
+  }
----------------
efriedma wrote:
> How does this compare to just the following?
> 
> ```
> do {
>   if (std::memcmp(Start, Needle, 2) == 0)
>     return Start - Data;
>   ++Start;
> } while (Start < Stop);
> return npos;
> ```
> 
> Much easier to read, and the performance is probably competitive.
Thanks, memcmp is not as fast (always loads the Needle in the loop and loads 2 byte of Start instead of 1) but it should still provide sufficient benefit from inline expansion, so I've adopted the strategy.
Repository:
  rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133660/new/
https://reviews.llvm.org/D133660
    
    
More information about the llvm-commits
mailing list