|
| 1 | +use criterion::{BenchmarkId, Criterion, black_box, criterion_group, criterion_main}; |
| 2 | +use fff_search::types::{BigramFilter, BigramIndexBuilder}; |
| 3 | + |
| 4 | +/// Build a realistic bigram index for benchmarking. |
| 5 | +/// Simulates a large repo by generating varied content per file. |
| 6 | +fn build_test_index(file_count: usize) -> BigramFilter { |
| 7 | + let builder = BigramIndexBuilder::new(file_count); |
| 8 | + |
| 9 | + for i in 0..file_count { |
| 10 | + // Generate varied content so we get a mix of sparse and dense columns |
| 11 | + let content = format!( |
| 12 | + "struct File{i} {{ fn process() {{ let controller = read(path); }} }} // module {i}" |
| 13 | + ); |
| 14 | + builder.add_file_content(i, content.as_bytes()); |
| 15 | + } |
| 16 | + |
| 17 | + builder.compress() |
| 18 | +} |
| 19 | + |
| 20 | +fn bench_bigram_query(c: &mut Criterion) { |
| 21 | + let file_counts = [10_000, 100_000, 500_000]; |
| 22 | + |
| 23 | + for &file_count in &file_counts { |
| 24 | + let index = build_test_index(file_count); |
| 25 | + eprintln!( |
| 26 | + "Index ({} files): {} columns ({} dense, {} sparse)", |
| 27 | + file_count, |
| 28 | + index.columns_used(), |
| 29 | + index.dense_columns(), |
| 30 | + index.sparse_columns(), |
| 31 | + ); |
| 32 | + |
| 33 | + let mut group = c.benchmark_group(format!("bigram_query_{file_count}")); |
| 34 | + group.sample_size(500); |
| 35 | + |
| 36 | + let queries: &[(&str, &[u8])] = &[ |
| 37 | + ("short_2char", b"st"), |
| 38 | + ("medium_6char", b"struct"), |
| 39 | + ("long_14char", b"let controller"), |
| 40 | + ("multi_word", b"fn process"), |
| 41 | + ]; |
| 42 | + |
| 43 | + for (name, query) in queries { |
| 44 | + group.bench_with_input(BenchmarkId::from_parameter(name), query, |b, q| { |
| 45 | + b.iter(|| { |
| 46 | + let result = index.query(black_box(q)); |
| 47 | + black_box(&result); |
| 48 | + }); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + group.finish(); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn bench_bigram_is_candidate(c: &mut Criterion) { |
| 57 | + let index = build_test_index(500_000); |
| 58 | + let candidates = index.query(b"struct").unwrap(); |
| 59 | + |
| 60 | + c.bench_function("is_candidate_500k", |b| { |
| 61 | + b.iter(|| { |
| 62 | + let mut count = 0u32; |
| 63 | + for i in 0..500_000 { |
| 64 | + if BigramFilter::is_candidate(black_box(&candidates), i) { |
| 65 | + count += 1; |
| 66 | + } |
| 67 | + } |
| 68 | + black_box(count) |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + c.bench_function("count_candidates_500k", |b| { |
| 73 | + b.iter(|| BigramFilter::count_candidates(black_box(&candidates))); |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +fn bench_bigram_build(c: &mut Criterion) { |
| 78 | + let mut group = c.benchmark_group("bigram_build"); |
| 79 | + group.sample_size(10); |
| 80 | + |
| 81 | + let file_counts = [10_000, 100_000]; |
| 82 | + |
| 83 | + for &file_count in &file_counts { |
| 84 | + // Pre-generate content so we only measure index building |
| 85 | + let contents: Vec<String> = (0..file_count) |
| 86 | + .map(|i| { |
| 87 | + format!( |
| 88 | + "struct File{i} {{ fn process() {{ let controller = read(path); }} }} // mod {i}" |
| 89 | + ) |
| 90 | + }) |
| 91 | + .collect(); |
| 92 | + |
| 93 | + group.bench_with_input( |
| 94 | + BenchmarkId::new("build_and_compress", file_count), |
| 95 | + &file_count, |
| 96 | + |b, &fc| { |
| 97 | + b.iter(|| { |
| 98 | + let builder = BigramIndexBuilder::new(fc); |
| 99 | + for (i, content) in contents.iter().enumerate() { |
| 100 | + builder.add_file_content(i, content.as_bytes()); |
| 101 | + } |
| 102 | + let index = builder.compress(); |
| 103 | + black_box(index.columns_used()) |
| 104 | + }); |
| 105 | + }, |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + group.finish(); |
| 110 | +} |
| 111 | + |
| 112 | +criterion_group!( |
| 113 | + benches, |
| 114 | + bench_bigram_query, |
| 115 | + bench_bigram_is_candidate, |
| 116 | + bench_bigram_build, |
| 117 | +); |
| 118 | + |
| 119 | +criterion_main!(benches); |
0 commit comments