分享 比 ack 更快更好用的东东：the silver searcher
The Silver Searcher
An attempt to make something better than
ack (which itself is better than
What's so great about Ag?
- It searches code about 3–5× faster than
- It ignores file patterns from your
- If there are files in your source repo you don't want to search, just add their patterns to a
.agignorefile. *cough* extern *cough*
- The command name is 33% shorter than
How is it so fast?
- Searching for literals (no regex) uses Boyer-Moore-Horspool strstr.
- Files are
mmap()ed instead of read into a buffer.
- If you're building with PCRE 8.21 or greater, regex searches use the JIT compiler.
- Ag calls
pcre_study()before executing the regex on a jillion files.
- Instead of calling
fnmatch()on every pattern in your ignore files, non-regex patterns are loaded into an array and binary searched.
- Ag uses Pthreads to take advantage of multiple CPU cores and search files in parallel.
I've written several blog posts showing how I've improved performance. These include how I added pthreads, wrote my own
scandir(), benchmarked every revision to find performance regressions, and profiled with gprof and Valgrind.
brew install the_silver_searcher