Search with ripgrep

ripgrep is a line-oriented search tool that recursively searches your current directory for a regex pattern while respecting your gitignore rules. ripgrep has first class support on Windows, macOS and Linux, with binary downloads available for every release. ripgrep is similar to other popular search tools like The Silver Searcher, ack and grep.

Is it really faster than everything else?

Generally, yes. A large number of benchmarks with detailed analysis for each is available on my blog.

Summarizing, ripgrep is fast because:

  • It is built on top of Rust’s regex engine. Rust’s regex engine uses finite automata, SIMD and aggressive literal optimizations to make searching very fast.
  • Rust’s regex library maintains performance with full Unicode support by building UTF-8 decoding directly into its deterministic finite automaton engine.
  • It supports searching with either memory maps or by searching incrementally with an intermediate buffer. The former is better for single files and the latter is better for large directories. ripgrep chooses the best searching strategy for you automatically.
  • Applies your ignore patterns in .gitignore files using a RegexSet. That means a single file path can be matched against multiple glob patterns simultaneously.
  • It uses a lock-free parallel recursive directory iterator, courtesy of crossbeam and ignore.

Benchmark

This example searches the entire Linux kernel source tree (after running make defconfig && make -j8) for [A-Z]+_SUSPEND, where all matches must be words. Timings were collected on a system with an Intel i7-6900K 3.2 GHz, and ripgrep was compiled with SIMD enabled.

ToolCommandLine countTime
ripgrep (Unicode)rg -n -w '[A-Z]+_SUSPEND'4500.106s
git grepLC_ALL=C git grep -E -n -w '[A-Z]+_SUSPEND'4500.553s
The Silver Searcherag -w '[A-Z]+_SUSPEND'4500.589s
git grep (Unicode)LC_ALL=en_US.UTF-8 git grep -E -n -w '[A-Z]+_SUSPEND'4502.266s
siftsift --git -n -w '[A-Z]+_SUSPEND'4503.505s
ackack -w '[A-Z]+_SUSPEND'18786.823s
The Platinum Searcherpt -w -e '[A-Z]+_SUSPEND'45014.208s

Here’s another benchmark that disregards gitignore files and searches with a whitelist instead. The corpus is the same as in the previous benchmark, and the flags passed to each command ensure that they are doing equivalent work:

ToolCommandLine countTime
ripgreprg -L -u -tc -n -w '[A-Z]+_SUSPEND'4040.079s
ucgucg --type=cc -w '[A-Z]+_SUSPEND'3900.163s
GNU grepegrep -R -n --include='*.c' --include='*.h' -w '[A-Z]+_SUSPEND'4040.611s

And finally, a straight-up comparison between ripgrep and GNU grep on a single large file (~9.3GB, OpenSubtitles2016.raw.en.gz):

ToolCommandLine countTime
ripgreprg -w 'Sherlock [A-Z]\w+'52682.108s
GNU grepLC_ALL=C egrep -w 'Sherlock [A-Z]\w+'52687.014s

helm-ag with rg

1
2
3
4
5
6
(use-package helm-ag
:ensure t
:after (helm)
:config
(setq helm-ag-base-command "rg --color=always --smart-case --no-heading --line-number")
)

helm-do-grep-ag

1
(setq helm-grep-ag-command "rg --color=always --smart-case --no-heading --line-number %s %s %s")
0%