Skip to main content

Speed Tuning

ag is fast by default (mmap + parallel workers), but these flags help you squeeze more performance on large codebases or production servers.

Memory-Mapped I/O (--mmap)

ag can use mmap instead of sequential read() calls. This is faster when files are cached in RAM:

ag --mmap "pattern" /large/codebase/

Disable mmap (better for network filesystems or HDDs):

ag --nommap "pattern" /mnt/nfs/

Worker Threads (--workers)

ag uses multiple parallel worker threads. Reduce them on production servers to limit CPU impact:

# Limit to 2 worker threads
ag --workers 2 "ERROR" /var/log/

Limiting Results (-m)

Stop after N matches — useful for existence checks or getting a representative sample:

# Stop after finding the first 5 matches
ag -m 5 "FATAL" app.log

# Existence check: stop after first match
ag -m 1 -q "CRITICAL" app.log && echo "Found"

Skipping Binary Files

By default ag skips binary files automatically. If it guesses wrong, force text mode:

# Force ag to treat everything as text
ag -a "pattern" # --all-text

The Full Performance Stack

For maximum speed on a large ASCII log:

ag --mmap --workers 4 -Q "exact_string" /var/log/large.log
  • --mmap: use memory-mapped I/O
  • --workers 4: 4 parallel threads
  • -Q: fixed string (skip regex engine entirely)