DevTools Analysis

How Cursor Built Fast Regex Search with N-Gram Indexing

Cursor's AI-powered code editor needs sub-second regex search across entire codebases to keep agents productive. Ripgrep alone takes 15+ seconds on large monorepos because it scans every file. This post covers how Cursor built a client-side n-gram inverted index that narrows candidates before ripgrep runs — covering trigrams, bloom filter masks, sparse n-grams with frequency-based weight functions, memory-mapped file formats, and why the index lives on the client rather than a server.

Key takeaways

  • Ripgrep scans every file sequentially — in large monorepos this routinely takes 15+ seconds, which is unacceptable for agentic workflows where search runs continuously.
  • A trigram inverted index maps every 3-character sequence to the files containing it; querying intersects posting lists to narrow candidates before running ripgrep on just those files.
  • GitHub’s “Project Blackbird” approach adds bloom filter masks (locMask/nextMask) to each trigram entry, enabling “3.5-gram” matching without the index bloat of full quadgrams.
  • Sparse n-grams use a deterministic weight function (e.g. character-pair frequency) to extract variable-length tokens at natural boundaries — dramatically reducing both index size and query-time posting list intersections.
  • The index is client-side (not server-side) for four reasons: completeness (regex needs actual file content for final verification), latency, freshness (stale indexes cause agents to speculate), and privacy.

Why this comes up in interviews

Search infrastructure is a recurring system design topic — from autocomplete to full-text search to code search. This post gives you concrete indexing techniques (inverted indexes, n-grams, bloom filters) with real tradeoffs. Use these when asked to design a search system, explain indexing strategies, or discuss client-side vs. server-side architecture tradeoffs.

The problem: grep is too slow for agents

Ripgrep is fast at pattern matching within individual files, but it must scan every file in the repository sequentially. For large monorepos, this means 15+ second search times. In traditional development, a 15-second search is annoying but tolerable. For AI agents that search continuously as part of their workflow — investigating bugs, finding references, exploring unfamiliar code — it becomes a critical bottleneck. Each search blocks the agent’s next action, and agents may issue dozens of searches per task. The cumulative latency turns what should be a fast iteration cycle into minutes of waiting.

Interview angle

When designing search systems, the first question is whether you can afford a full scan or need an index. This is a clean example of when brute-force scanning hits a wall: when the consumer is automated and issues many queries in tight succession. In interviews, frame the problem as “at what query volume does scanning become unacceptable?” — it’s not just about data size, it’s about query frequency.

Inverted indexes and trigrams

An inverted index is a data structure that maps tokens to the documents containing them — like the index at the back of a textbook. For regex search, the question is what tokens to use. Cursor uses n-grams: overlapping character sequences of length n extracted from every file. Trigrams (n=3) are the sweet spot. Bigrams (n=2) produce posting lists that are too large — most 2-character pairs appear in nearly every file, so the index doesn’t filter well. Quadgrams (n=4) create too many unique keys, bloating the index. During indexing, every overlapping 3-character sequence from every file becomes an entry. During querying, a regex pattern is decomposed into its constituent trigrams, and the posting lists for those trigrams are intersected to find candidate files that contain all required sequences.

Interview angle

The n-gram size tradeoff is a classic precision-vs-recall-vs-cost question. Smaller n-grams give high recall but low precision (too many candidates). Larger n-grams give high precision but cost more storage and miss partial matches. Interviewers may ask you to reason about this tradeoff for different data types — the trigram sweet spot applies specifically to source code, where character diversity is high enough to make 3-grams discriminative.

Trigram queries with probabilistic masks

Plain trigram indexes have a precision problem: a 3-character match is often not selective enough. GitHub’s Project Blackbird approach augments each trigram posting list entry with two 8-bit bloom filter masks. The locMask encodes positions where the trigram appears within a file (position mod 8). The nextMask is a bloom filter of all characters that immediately follow that trigram in the file. Together, these enable “3.5-gram” matching — you get most of the filtering power of quadgrams without actually storing quadgram keys. Adjacent trigrams can be verified by rotating one trigram’s locMask left by one bit and intersecting with the next trigram’s locMask to confirm they appear at adjacent positions. The tradeoff: bloom filters saturate as files are modified, gradually losing their filtering power and requiring periodic rebuilds.

Interview angle

Bloom filters are a frequent interview topic, but candidates rarely discuss their use inside inverted indexes. This is a concrete example of augmenting an index with probabilistic metadata to improve precision without increasing key cardinality. The saturation tradeoff is important — bloom filters have no false negatives but increasing false positive rates over time, which means the index stays correct but gets slower.

Sparse n-grams

Instead of extracting every overlapping trigram, sparse n-grams use a deterministic weight function to select variable-length tokens at natural boundaries. A weight function (such as CRC32 or character-pair frequency) assigns a score to each position in the text. An n-gram boundary is placed wherever the weight exceeds all interior weights — this produces longer n-grams in common regions and shorter ones in rare regions. At query time, the same weight function generates a minimal “covering” set of n-grams from the search pattern. A frequency-based weight function assigns high weights to rare character pairs, which produces fewer but more discriminative n-grams compared to uniform trigrams. This dramatically reduces both the number of index lookups needed per query and the size of posting lists that must be intersected.

Interview angle

Sparse n-grams are an advanced indexing technique that shows deep understanding of search infrastructure. The key insight is that not all n-grams are equally useful — rare character sequences are far more discriminative than common ones. In interviews, this maps to the broader principle of information-theoretic token selection: index the tokens that carry the most information, not all tokens uniformly.

Why client-side indexing

Cursor chose to build and store the index on the user’s machine rather than a server, for four reasons. First, completeness: regex search ultimately needs the actual file content for final pattern verification, and keeping files synchronized between client and server adds complexity and failure modes. Second, latency: Cursor’s agent operates at high tokens-per-second and issues searches in tight loops — network roundtrips for each search would negate the speed gains from indexing. Third, freshness: unlike semantic search indexes that tolerate some staleness, regex search must reflect the latest file state or agents will miss recently written code and waste time speculating. Fourth, privacy: client-side indexing avoids sending user codebases to a centralized server.

Interview angle

Client-side vs. server-side is a fundamental architecture decision in search. Most candidates default to server-side because that’s what web search engines do. Being able to articulate when client-side is better — low-latency requirements, freshness constraints, privacy sensitivity, need for complete local data — shows architectural maturity. This maps to broader distributed systems questions about where to place computation relative to data.

Implementation: memory-efficient file format

The index is stored in two files on disk. The lookup table is a sorted array of entries mapping n-gram hashes to offsets in the postings file. It is memory-mapped (mmap), so the OS loads only the pages that are actually accessed rather than reading the entire table into memory. Lookups use binary search on the hash values to find the offset, then seek directly to that position in the postings file to read the posting list. The postings file stores all posting lists sequentially on disk. Importantly, only the n-gram hashes are stored — not the full n-gram strings. Hash collisions are safe because they only broaden a posting list (adding extra candidate files), never causing false negatives. A file that shouldn’t match might appear as a candidate, but it will be filtered out during the final ripgrep verification pass. Index freshness is keyed to Git commits, with user and agent changes layered on top as incremental updates.

Architecture diagram showing the n-gram indexing pipeline, query flow with posting list intersection, and client-side architecture with memory-mapped access
Top: source files are tokenized into n-grams and stored in a lookup table + postings file. Middle: queries decompose into n-grams, intersect posting lists, and verify candidates with ripgrep. Bottom: everything runs client-side with memory-mapped I/O.

Interview angle

Memory-mapped I/O is an underappreciated technique in system design interviews. It lets you work with data structures larger than available RAM while the OS handles paging transparently. The hash collision insight is also important: in systems where false positives are acceptable but false negatives are not (search, spam filters, content deduplication), you can use smaller hashes and simpler data structures because collisions only hurt performance, not correctness.

Performance impact and agentic workflows

With the n-gram index in place, regex search in Cursor skips the full-codebase scan entirely. The index narrows candidates to a small subset of files, and ripgrep only runs on those candidates for final verification. In benchmarks on large codebases like Chromium, the “grep phase” duration dropped substantially. For agent workflows specifically, the impact is qualitative, not just quantitative: removing search latency from the agent’s iteration loop means it can investigate more hypotheses, explore more of the codebase, and converge on solutions faster. The difference between a 15-second search and a sub-second search is the difference between an agent that feels stuck and one that feels fluid.

Interview angle

This section illustrates a key system design principle: optimizing for the bottleneck in a pipeline. The bottleneck was not pattern matching (ripgrep is already fast at that) but candidate selection (which files to match against). The index moves candidate selection from O(n) file scanning to O(k) posting list intersection where k is the number of query n-grams. In interviews, always identify the actual bottleneck before proposing optimizations.

Key concepts

Inverted Index

A data structure that maps content tokens (words, n-grams, terms) to the documents or files that contain them. Enables fast lookup of “which documents contain this token?” without scanning every document.

Like the index at the back of a textbook — instead of reading every page to find “binary search,” you look up the term in the index and jump directly to the relevant pages.

Trigram

A 3-character overlapping subsequence extracted from text. For the string “cursor”, the trigrams are “cur”, “urs”, “rso”, “sor”. Used as tokens in inverted indexes for regex search because they balance selectivity (filtering power) against index size.

Posting List

The list of documents (or file IDs) associated with a single token in an inverted index. For example, the trigram “err” might have a posting list of all files containing that 3-character sequence. Query processing intersects posting lists for multiple tokens to find candidate documents.

Bloom Filter

A space-efficient probabilistic data structure that tests whether an element is in a set. It can return false positives (“maybe in set”) but never false negatives (“definitely not in set”). In Cursor’s context, bloom filter masks augment trigram entries to approximate quadgram matching.

Like a bouncer with a guest list who might accidentally let in someone not on the list, but will never turn away someone who is on the list. Over time (saturation), the bouncer gets more lenient and lets in more uninvited guests.

Sparse N-Gram

A variable-length n-gram extracted at positions determined by a deterministic weight function rather than at every overlapping position. Produces fewer, more discriminative tokens than uniform trigrams by placing boundaries where character-pair rarity is highest.

Memory-Mapped I/O (mmap)

An OS feature that maps a file on disk into virtual memory, allowing programs to access file data as if it were in RAM. The OS loads pages on demand and manages caching transparently. Useful for working with data structures larger than available memory without explicit read/write calls.

Covering Algorithm

At query time, the algorithm that decomposes a search pattern into the minimal set of n-grams needed to look up in the index. Uses the same weight function as the indexing phase to ensure the generated query n-grams match what was indexed. Fewer covering n-grams means fewer posting list intersections.

N-Gram Weight Function

A deterministic function (e.g., CRC32 hash or character-pair frequency table) that assigns a numerical weight to each position in a text. N-gram boundaries are placed at local maxima of this function. Frequency-based weights assign high scores to rare character pairs, producing n-grams that are more selective during search.

Check your understanding

Cursor chose trigrams (3-character sequences) over bigrams or quadgrams for their inverted index. What is the primary reason trigrams are preferred?

  • Trigrams are the only n-gram size that supports regex pattern decomposition.
  • Bigram posting lists are too large (most pairs appear everywhere) and quadgram key spaces are too large — trigrams balance selectivity against index size.
  • Trigrams align with CPU cache line sizes, making intersection operations hardware-efficient.
  • The trigram approach was inherited from Google Code Search and has not been re-evaluated since.
Show answer

Bigram posting lists are too large (most pairs appear everywhere) and quadgram key spaces are too large — trigrams balance selectivity against index size.

Bigrams (n=2) lack selectivity — most 2-character pairs appear in nearly every file, so intersecting their posting lists barely reduces candidates. Quadgrams (n=4) create too many unique keys, bloating index size. Trigrams sit at the sweet spot where posting lists are selective enough to filter well but the key space remains manageable.

GitHub’s Project Blackbird augments trigram entries with locMask and nextMask bloom filter fields. What capability does this add?

  • It enables full quadgram matching by storing the complete 4th character alongside each trigram.
  • It allows approximate “3.5-gram” matching — verifying adjacent trigram positions and following characters without storing quadgram keys.
  • It compresses posting lists by encoding file IDs as bloom filter bits instead of integer arrays.
  • It tracks how recently each trigram was indexed, enabling time-bounded search results.
Show answer

It allows approximate “3.5-gram” matching — verifying adjacent trigram positions and following characters without storing quadgram keys.

The locMask encodes positions (mod 8) where trigrams appear, and nextMask is a bloom filter of characters following the trigram. By rotating locMask and intersecting with adjacent trigrams, the system approximates quadgram-level filtering (“3.5-grams”) without the index bloat of storing all 4-character keys.

Bloom filters in the trigram index can saturate over time as files are modified. What is the practical consequence of saturation?

  • The index starts returning false negatives — some matching files are missed entirely.
  • The index produces more false positives — more candidate files pass the filter and must be verified by ripgrep, degrading search speed but not correctness.
  • The index becomes corrupted and must be rebuilt from scratch before any queries can run.
  • Saturated bloom filters consume exponentially more memory, eventually exhausting the editor’s heap.
Show answer

The index produces more false positives — more candidate files pass the filter and must be verified by ripgrep, degrading search speed but not correctness.

Bloom filters guarantee no false negatives — a matching file is never missed. But as bits fill up from modifications, the false positive rate increases: more non-matching files pass the filter and enter the candidate set. The index stays correct (final ripgrep verification catches false positives) but gets slower. Periodic rebuilds restore filter performance.

Sparse n-grams use a deterministic weight function to place n-gram boundaries. A frequency-based weight function assigns high weights to rare character pairs. Why does this improve search performance compared to uniform trigrams?

  • Rare character pairs compress better, reducing index file size on disk.
  • It produces fewer, more discriminative n-grams — rare tokens have shorter posting lists, so fewer intersections are needed and each intersection eliminates more candidates.
  • High-weight boundaries force all n-grams to exactly 3 characters, ensuring compatibility with the trigram index.
  • Frequency-based weights enable case-insensitive matching without doubling the index size.
Show answer

It produces fewer, more discriminative n-grams — rare tokens have shorter posting lists, so fewer intersections are needed and each intersection eliminates more candidates.

Rare character pairs are highly selective — they appear in fewer files. By placing n-gram boundaries at these rare positions, the resulting tokens have shorter posting lists (fewer candidate files per token). The covering algorithm at query time also generates fewer n-grams from the search pattern, meaning fewer posting list lookups and intersections.

Cursor stores n-gram hashes (not full n-gram strings) in the lookup table. Hash collisions can occur where different n-grams map to the same hash. Why is this acceptable?

  • Hash collisions are statistically impossible with the hash function they use (CRC32).
  • Collisions only broaden posting lists (adding extra candidate files), never causing false negatives — correctness is preserved because ripgrep verifies all candidates.
  • Collisions are resolved by chaining — each hash bucket stores all colliding n-grams with their full strings.
  • The lookup table is rebuilt whenever a collision is detected, maintaining a perfect hash function.
Show answer

Collisions only broaden posting lists (adding extra candidate files), never causing false negatives — correctness is preserved because ripgrep verifies all candidates.

A hash collision merges two n-grams’ posting lists. This means a query might receive extra candidate files that don’t actually match, but it will never miss a file that does match. The final ripgrep pass over candidate files catches all false positives, so collisions affect performance (more candidates to verify) but never correctness.

Cursor builds the index client-side rather than server-side. Which of the following is NOT one of their stated reasons?

  • Client-side avoids network roundtrip latency for searches issued in tight agent loops.
  • Regex search requires actual file content for final verification — keeping files synchronized with a server adds complexity.
  • Client-side indexing enables distributed index sharding across multiple user machines for faster queries.
  • Client-side preserves privacy by not sending user codebases to a centralized server.
Show answer

Client-side indexing enables distributed index sharding across multiple user machines for faster queries.

Cursor’s four reasons for client-side indexing are: completeness (regex needs local file content for verification), latency (no network roundtrips), freshness (regex indexes must reflect the latest file state), and privacy. Distributed sharding across user machines is not a factor — each user’s index is local to their own codebase.

The index lookup table is memory-mapped (mmap) rather than loaded entirely into RAM. What is the primary benefit of this approach?

  • mmap encrypts the file contents transparently, satisfying Cursor’s privacy requirements.
  • The OS loads only the pages actually accessed during lookups, keeping memory usage proportional to query patterns rather than total index size.
  • mmap allows multiple processes to write to the same file concurrently without locks.
  • Memory-mapped files are automatically compressed by the OS, reducing disk usage by 50-70%.
Show answer

The OS loads only the pages actually accessed during lookups, keeping memory usage proportional to query patterns rather than total index size.

Memory-mapped I/O lets the program access the lookup table as if it were in memory, but the OS only pages in the portions that are actually read. For a large index where any given query touches a small fraction of entries, this keeps the editor’s memory footprint low. The OS handles caching and eviction transparently.

A developer is designing a code search system for a monorepo with 500,000 files. The system must support regex search with sub-second latency. They propose building a server-side trigram index with results streamed over a WebSocket. What is the most significant risk with this approach?

  • WebSockets do not support binary data, so posting lists cannot be transmitted efficiently.
  • Server-side indexes cannot support regex — only keyword search is possible with inverted indexes.
  • The index may be stale relative to uncommitted local changes, causing searches to miss recently written code — a critical failure mode for developer workflows.
  • Trigram indexes only work with ASCII text and cannot handle Unicode source files.
Show answer

The index may be stale relative to uncommitted local changes, causing searches to miss recently written code — a critical failure mode for developer workflows.

For code search in active development, freshness is critical. A server-side index reflects the last synced state, but developers (and agents) constantly create and modify files locally. Missing a function definition written 30 seconds ago forces the developer or agent to waste time re-searching or speculating. Cursor specifically cites freshness as a key reason for client-side indexing.

In Cursor’s system, the regex search pipeline has two stages: (1) index lookup to find candidate files, then (2) ripgrep runs on only those candidates. If the index returns too many false-positive candidates, what happens?

  • The search returns incorrect results — files that don’t match the regex appear in the output.
  • Search correctness is maintained but latency increases, because ripgrep must scan more files for final verification.
  • The index automatically switches to quadgram mode to reduce false positives.
  • Cursor falls back to a full brute-force ripgrep scan of the entire repository.
Show answer

Search correctness is maintained but latency increases, because ripgrep must scan more files for final verification.

Ripgrep always runs on candidate files as the verification step, so correctness is guaranteed regardless of false positive count. But more false positives mean ripgrep must scan more files, increasing latency. This is why index precision matters — bloom filter saturation, hash collisions, and low-selectivity n-grams all increase false positives and degrade performance without affecting correctness.

The covering algorithm at query time decomposes a regex pattern into the minimal set of n-grams to look up in the index. Why is minimizing the number of covering n-grams important?

  • Each n-gram lookup requires a disk seek and posting list read — fewer lookups mean fewer I/O operations and faster query response.
  • The regex engine has a hard limit on the number of n-grams it can process per query.
  • Covering n-grams are transmitted to the server, and network bandwidth is the bottleneck.
  • More n-grams increase the probability of hash collisions in the lookup table, causing index corruption.
Show answer

Each n-gram lookup requires a disk seek and posting list read — fewer lookups mean fewer I/O operations and faster query response.

Each covering n-gram requires a binary search on the lookup table and a disk read of the corresponding posting list. The posting lists must then be intersected. Fewer covering n-grams means fewer I/O operations, fewer intersection computations, and faster overall query response. The covering algorithm ensures no redundant lookups while still guaranteeing that the candidate set includes all true matches.

#Search #Indexing #System Design #Performance #Developer Tools

Liked this? Engineer's Codex sends one deep dive and a link roundup every week.

No spam. Unsubscribe anytime.