Sneller Regex vs Ripgrep
ripgrep author here.<p>Very interesting! I tried reproducing it, but unfortunately:<p><pre><code>    $ sneller -j -S &quot;SELECT COUNT(Line) FROM &#x27;en.json&#x27; WHERE Line ~ &#x27;Sherlock [A-Z]\\w+&#x27;&quot; 
    expr.Unescape: unexpected backslash escape of &#x27;w&#x27; (0x77)
    $ sneller -j -S &quot;SELECT COUNT(Line) FROM &#x27;en.json&#x27; WHERE Line ~ &#x27;Sherlock [A-Z][a-z]+&#x27;&quot;
    CPU doesn&#x27;t support AVX-512
</code></pre>
And <i>that</i> is the primary reason why ripgrep doesn&#x27;t bother with AVX-512. Not because of some lack of skill as this blog suggests:<p>&gt; Additionally, ripgrep uses AVX2 and does not take advantage of AVX-512 instruction sets, but this can be forgiven given the specialized skills required for handcrafting for SkylakeX and Icelake&#x2F;Zen4 processors.<p>Namely, I tried running sneller on my CPU, which is a pretty recent i9-12900K, and not even it supports AVX-512. That&#x27;s because Intel has been dropping support for AVX-512 from its more recent consumer grade CPUs. ripgrep is running far more frequently on consumer grade CPUs, so supporting AVX-512 is probably not particularly advantageous. At least, it&#x27;s not obvious to me that it&#x27;s worth doing. And certainly, the skill argument isn&#x27;t entirely wrong. I&#x27;d have to invest developer time to make it work.<p>I think there are two other things worth highlighting from this blog.<p>First is that sneller seems to do quite well with compressed data. This is definitely not ripgrep&#x27;s strong suit. When you use ripgrep&#x27;s -z&#x2F;--search-zip flag, all it&#x27;s doing is shelling out to your gzip&#x2F;xz&#x2F;whatever executable to do the decompression work, which is then streamed into ripgrep for searching. So if your search speed tanks when using -z&#x2F;--search-zip, it&#x27;s likely because your decompression tools are slow, not because of ripgrep. But it&#x27;s a fair comparison from sneller&#x27;s perspective, because it seems to integrate the two.<p>Second is the issue of multi-threaded search. In ripgrep, the fundamental unit of work is &quot;search a file.&quot; ripgrep has no support for more granular parallelism. That is, if you give it one file, it&#x27;s limited to doing a single threaded search. ripgrep <i>could</i> do more granular parallelism, but it hasn&#x27;t been obviously worth it to me. If most searches are on a directory tree, then parallelizing at the level of each file is almost certainly good enough. Making ripgrep&#x27;s parallelism more fine grained is a fair bit of work too, and there would be a lot of fiddly stuff to get right. If I could run sneller easily, I&#x27;d probably try to see how it does in a more varied workload than what is presented in this blog. :-)<p>And finally some corrections:<p>&gt; However, when using a single thread, ripgrep appears to be slightly faster.<p>Not just slightly faster, over 2x faster!<p>The single threaded results for Regex2 and Regex3 for Sneller are quite nice! I&#x27;d be interested in hearing more about what you&#x27;re doing in the Regex2 case, since Sneller and ripgrep are about on par with the Regex3 case. Maybe a fail fast optimization?<p>&gt; The reason for this is that ripgrep uses the Boyer-Moore string search algorithm, which is a pattern matching algorithm that is often used for searching for substrings within larger strings. It is particularly efficient when the pattern being searched for is relatively long and the alphabet of characters being searched over is relatively small. Sneller does not use this substring search algorithm and as a result is slower than ripgrep with substrings. However, when long substrings are not present, Sneller outperforms ripgrep.<p>ripgrep has never used Boyer-Moore. (Okay, some years ago, ripgrep <i>could</i> use Boyer-Moore in certain niche cases. But that hasn&#x27;t been the case for a while and it was never the thing most commonly used). What ripgrep uses today is succinctly described here: <a href="https:&#x2F;&#x2F;github.com&#x2F;BurntSushi&#x2F;memchr#algorithms-used">https:&#x2F;&#x2F;github.com&#x2F;BurntSushi&#x2F;memchr#algorithms-used</a> (But it has always eschewed algorithms like Boyer-Moore in favor of more heuristic-y approaches based on a background frequency distribution of bytes.)<p>I think I would also contest the claim that &quot;long substrings&quot; are the key here. ripgrep is plenty fast with short substrings too. You&#x27;re correct that if you have <i>no</i> literals then ripgrep will get slower because it has to fall back to the regex engine. But I&#x27;d like to see more robust benchmarks there. Your Regex2 and Regex3 benchmarks raise more questions than it answers. :-)<p>&gt; Although the resulting .dot and .svg files may be somewhat clunky, we can still observe from the graph that the number of nodes and edges are small enough to use the branchless IceLake implementation. In this particular case, we only need 8 bits to encode the number of nodes and the number of distinct edges, enabling the tool to use (what we call) the 8-bit DFA implementation. For more details on how this works, see our post on regex implementations.<p>So this is talking about the DFA graph for the regex `Sherlock [A-Z]\w+`. It&#x27;s important to point out that, in ripgrep, `\w` is Unicode aware by default. Which makes it absolutely enormous. So I think the state graph you linked is probably only for the ASCII version of that regex.<p>Indeed, reading your regex blog[1], it perhaps looks like a lot of the tricks you use won&#x27;t work for Unicode, because Unicode tends to blow up finite automata.<p>If I could run Sneller, I&#x27;d probably try to poke it to see what its Unicode support looks like. From a quick glance of the source code, it also looks like you build full DFAs. So I would also try to poke it to see what happens when handed a particularly a not-so-small regex. (Building a DFA can take quite some time.)<p>Ah okay, I see, you put a max limit on the DFA: <a href="https:&#x2F;&#x2F;github.com&#x2F;SnellerInc&#x2F;sneller&#x2F;blob&#x2F;bb5adec564bf9869d8bb0f86b6ea2027c077a2b3&#x2F;regexp2&#x2F;Regexp2.go#L30">https:&#x2F;&#x2F;github.com&#x2F;SnellerInc&#x2F;sneller&#x2F;blob&#x2F;bb5adec564bf9869d...</a><p>Overall this is a very cool project!<p>Also, is the regex engine available as a standalone library? It would be interesting to benchmark it on its own. Although, if it only works when AVX-512 is available, then that is pretty limiting unfortunately.<p>[1]: <a href="https:&#x2F;&#x2F;sneller.io&#x2F;blog&#x2F;accelerating-regex-using-avx-512&#x2F;" rel="nofollow">https:&#x2F;&#x2F;sneller.io&#x2F;blog&#x2F;accelerating-regex-using-avx-512&#x2F;</a>