<p>The fastest substring search algorithm is going to depend on the context:</p>

<ol>
<li>the alphabet size (e.g. DNA vs English)</li>
<li>the needle length</li>
</ol>

<p>The 2010 paper <a href="http://arxiv.org/pdf/1012.2547v1.pdf">"The Exact String Matching Problem: a Comprehensive Experimental Evaluation"</a> gives tables with runtimes for 51 algorithms (with different alphabet sizes and needle lengths), so you can pick the best algorithm for your context.</p>

<p>All of those algorithms have C implementations, as well as a test suite, here:</p>

<p><a href="http://www.dmi.unict.it/~faro/smart/algorithms.php">http://www.dmi.unict.it/~faro/smart/algorithms.php</a></p>
What is the fastest substring search algorithm?
<p>OK, so I don't sound like an idiot I'm going to state the problem/requirements more explicitly:</p>

<ul>
<li>Needle (pattern) and haystack (text to search) are both C-style null-terminated strings. No length information is provided; if needed, it must be computed.</li>
<li>Function should return a pointer to the first match, or <code>NULL</code> if no match is found.</li>
<li><strong>Failure cases are not allowed. This means any algorithm with non-constant (or large constant) storage requirements will need to have a fallback case for allocation failure (and performance in the fallback care thereby contributes to worst-case performance).</strong></li>
<li>Implementation is to be in C, although a good description of the algorithm (or link to such) without code is fine too.</li>
</ul>

<p>...as well as what I mean by "fastest":</p>

<ul>
<li>Deterministic <code>O(n)</code> where <code>n</code> = haystack length. (But it may be possible to use ideas from algorithms which are normally <code>O(nm)</code> (for example rolling hash) if they're combined with a more robust algorithm to give deterministic <code>O(n)</code> results).</li>
<li>Never performs (measurably; a couple clocks for <code>if (!needle[1])</code> etc. are okay) worse than the naive brute force algorithm, especially on very short needles which are likely the most common case. (Unconditional heavy preprocessing overhead is bad, as is trying to improve the linear coefficient for pathological needles at the expense of likely needles.)</li>
<li>Given an arbitrary needle and haystack, comparable or better performance (no worse than 50% longer search time) versus any other widely-implemented algorithm.</li>
<li>Aside from these conditions, I'm leaving the definition of "fastest" open-ended. A good answer should explain why you consider the approach you're suggesting "fastest".</li>
</ul>

<p>My current implementation runs in roughly between 10% slower and 8 times faster (depending on the input) than glibc's implementation of Two-Way.</p>

<p><strong>Update: My current optimal algorithm is as follows:</strong></p>

<ul>
<li>For needles of length 1, use <code>strchr</code>.</li>
<li>For needles of length 2-4, use machine words to compare 2-4 bytes at once as follows: Preload needle in a 16- or 32-bit integer with bitshifts and cycle old byte out/new bytes in from the haystack at each iteration. Every byte of the haystack is read exactly once and incurs a check against 0 (end of string) and one 16- or 32-bit comparison.</li>
<li>For needles of length >4, use Two-Way algorithm with a bad shift table (like Boyer-Moore) which is applied only to the last byte of the window. To avoid the overhead of initializing a 1kb table, which would be a net loss for many moderate-length needles, I keep a bit array (32 bytes) marking which entries in the shift table are initialized. Bits that are unset correspond to byte values which never appear in the needle, for which a full-needle-length shift is possible.</li>
</ul>

<p>The big questions left in my mind are:</p>

<ul>
<li>Is there a way to make better use of the bad shift table? Boyer-Moore makes best use of it by scanning backwards (right-to-left) but Two-Way requires a left-to-right scan.</li>
<li>The only two viable candidate algorithms I've found for the general case (no out-of-memory or quadratic performance conditions) are <a href="http://www-igm.univ-mlv.fr/~lecroq/string/node26.html" rel="noreferrer">Two-Way</a> and <a href="http://www-igm.univ-mlv.fr/~lecroq/string/node27.html" rel="noreferrer">String Matching on Ordered Alphabets</a>. But are there easily-detectable cases where different algorithms would be optimal? Certainly many of the <code>O(m)</code> (where <code>m</code> is needle length) in space algorithms could be used for <code>m&lt;100</code> or so. It would also be possible to use algorithms which are worst-case quadratic if there's an easy test for needles which provably require only linear time.</li>
</ul>

<p>Bonus points for:</p>

<ul>
<li>Can you improve performance by assuming the needle and haystack are both well-formed UTF-8? (With characters of varying byte lengths, well-formed-ness imposes some string alignment requirements between the needle and haystack and allows automatic 2-4 byte shifts when a mismatching head byte is encountered. But do these constraints buy you much/anything beyond what maximal suffix computations, good suffix shifts, etc. already give you with various algorithms?)</li>
</ul>

<p><strong>Note:</strong> I'm well aware of most of the algorithms out there, just not how well they perform in practice. Here's a good reference so people don't keep giving me references on algorithms as comments/answers: <a href="http://www-igm.univ-mlv.fr/~lecroq/string/index.html" rel="noreferrer">http://www-igm.univ-mlv.fr/~lecroq/string/index.html</a></p>