Skip to content

Add benchmark-driven performance optimizations - #82

Merged
Shengyu Fu (shengyfu) merged 20 commits into
mainfrom
perf-benchmarks
May 3, 2026
Merged

Add benchmark-driven performance optimizations#82
Shengyu Fu (shengyfu) merged 20 commits into
mainfrom
perf-benchmarks

Conversation

@shengyfu

Copy link
Copy Markdown
Member

Summary

  • add Criterion benchmarks for trigram extraction, query execution, and index builds, including on-disk query and high-diversity index workloads
  • optimize OR query unions, lowercase trigram extraction, index-build memory usage, and index serialization
  • replace full-build inverted HashMap<u32, Vec<PostingEntry>> construction with flat postings sorted/grouped for writing
  • skip redundant query-time posting sort/dedup when on-disk posting lists are already sorted
  • bump workspace version to 0.1.20

Benchmark highlights

  • high-diversity 1,000-file index build: ~1.4659s -> 354.73ms, peak working set ~98.74MiB -> 43.58MiB
  • on-disk common literal query, 5,000 files: ~645.85us -> 336.27us
  • OR union query execution: previously measured ~70%+ faster across larger branch counts

Validation

  • cargo check --workspace
  • cargo fmt --all -- --check
  • cargo test --workspace
  • cargo clippy --workspace --benches -- -D warnings

Shengyu Fu (shengyfu) and others added 15 commits May 2, 2026 13:18
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Process walked files in bounded chunks so build_index no longer retains every file's per-trigram map before merging postings. This keeps file ID assignment deterministic while reducing peak memory on large repositories.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Restore LF endings after the index build batching change so the branch diff remains focused on the actual optimization.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add 2000-file and 5000-file index build cases so the benchmark exercises repositories large enough to cross the builder extraction batch boundary.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Let the index_build benchmark binary run an isolated child build with --peak-memory so Windows runs can report peak working set for large repository cases. Keep normal Criterion timing mode as the default.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Collect only successfully extracted file trigram maps for each build batch instead of storing Option entries and flattening them later. This keeps file IDs deterministic while reducing batch memory overhead.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Write files.bin entries directly to the buffered writer and pass path iterators into write_index_files. This avoids allocating a temporary Vec for each file entry and removes the intermediate path Vec in write_index_v2.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Encode posting entries into a reusable scratch buffer and write them in chunks instead of issuing one small write per posting. This reduces per-entry serialization overhead while preserving posting order and the on-disk format.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Write lookup entries to lookup.bin through a reusable scratch buffer while postings are serialized, avoiding retention of the full lookup entry vector for high-diversity indexes. Add a high-diversity index build benchmark and peak-memory probe to exercise this path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Collect build-time postings in a flat vector, sort by trigram and file id, and stream grouped postings directly to the on-disk writer. This avoids constructing a HashMap of per-trigram posting vectors for full index builds while preserving the existing snapshot writer path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Detect already sorted unique posting lists before sorting and deduplicating during mask-aware query execution. Add an on-disk common-literal query benchmark that exercises mmap posting decode and query execution against built indexes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Update the workspace package version and lockfile package entries for the performance optimization release.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 3, 2026 05:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a benchmark suite around tgrep-core and applies several performance-oriented changes to trigram extraction, query execution, and index serialization. It mainly targets the core indexing/query pipeline, where tgrep spends most of its time when building indexes and evaluating searches.

Changes:

  • Add Criterion benchmarks for trigram extraction, query execution, and index builds.
  • Optimize OR-query execution, lowercase trigram extraction, and on-disk index writing to reduce work and memory usage.
  • Bump the workspace/package version to 0.1.20 and update the lockfile for new benchmark dependencies.
Show a summary per file
File Description
tgrep-core/src/trigram.rs Optimizes merged trigram extraction by skipping lowercase-copy work when content has no ASCII uppercase bytes; adds matching tests.
tgrep-core/src/query.rs Reworks OR execution to union sorted results incrementally and adds posting-list sort/dedup helpers plus tests.
tgrep-core/src/ondisk.rs Splits file-entry validation/writing so files.bin entries can be streamed without temporary allocation; adds round-trip test coverage.
tgrep-core/src/builder.rs Replaces full-build inverted-map construction with batched flat postings, then writes index files from sorted postings.
tgrep-core/benches/trigram_extraction.rs Adds Criterion benchmarks for trigram extraction on lowercase and mixed-case corpora.
tgrep-core/benches/query_execution.rs Adds Criterion benchmarks for AND/OR execution and on-disk literal query workloads.
tgrep-core/benches/index_build.rs Adds Criterion benchmarks for index-build throughput and a Windows-only peak working set probe.
tgrep-core/Cargo.toml Adds benchmark dependencies and registers the new benchmark targets.
Cargo.toml Bumps the workspace version.
Cargo.lock Records new benchmark-related dependencies and updated crate versions.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 9/10 changed files
  • Comments generated: 3

Comment thread tgrep-core/benches/index_build.rs
Comment thread tgrep-core/src/builder.rs
Comment thread tgrep-core/src/builder.rs
Shengyu Fu (shengyfu) and others added 2 commits May 2, 2026 23:53
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 3, 2026 08:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comments suppressed due to low confidence (1)

tgrep-core/benches/index_build.rs:150

  • format_mib is only called from the Windows-specific peak-memory path, so it becomes dead code on non-Windows builds and causes the bench target to fail clippy -D warnings. This function should be gated the same way as its only caller.
fn format_mib(bytes: u64) -> f64 {
    bytes as f64 / 1024.0 / 1024.0
}
  • Files reviewed: 11/12 changed files
  • Comments generated: 5

Comment thread tgrep-core/benches/query_execution.rs
Comment thread tgrep-core/src/query.rs Outdated
Comment thread tgrep-core/src/query.rs Outdated
Comment thread tgrep-core/benches/index_build.rs
Comment thread tgrep-core/src/builder.rs
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Shengyu Fu <shengyfu@microsoft.com>
Copilot AI review requested due to automatic review settings May 3, 2026 17:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 11/12 changed files
  • Comments generated: 4

Comment thread tgrep-core/src/builder.rs
Comment thread tgrep-core/Cargo.toml
Comment thread tgrep-core/src/builder.rs
Comment thread tgrep-core/benches/index_build.rs
Shengyu Fu (shengyfu) and others added 2 commits May 3, 2026 10:38
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 3, 2026 18:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 12/13 changed files
  • Comments generated: 3

Comment thread tgrep-core/src/builder.rs
Comment thread tgrep-core/src/query.rs
Comment thread tgrep-core/src/builder.rs
@shengyfu
Shengyu Fu (shengyfu) merged commit a9b1da4 into main May 3, 2026
13 checks passed
@shengyfu
Shengyu Fu (shengyfu) deleted the perf-benchmarks branch May 3, 2026 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants