reflex-search 1.0.3

A local-first, structure-aware code search engine for AI agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! Performance tests for Reflex
//!
//! These tests verify that Reflex meets its performance goals:
//! - Indexing: Fast enough for large codebases
//! - Queries: Sub-100ms on 10k+ files
//! - Incremental updates: Efficient reindexing

use reflex::{CacheManager, IndexConfig, Indexer, QueryEngine, QueryFilter};
use std::fs;
use std::time::Instant;
use tempfile::TempDir;

// ==================== Indexing Performance Tests ====================

#[test]
fn test_index_small_codebase_performance() {
    let temp = TempDir::new().unwrap();
    let project = temp.path();

    // Create 100 small Rust files
    for i in 0..100 {
        let content = format!("fn function_{}() {{\n    println!(\"test\");\n}}", i);
        fs::write(project.join(format!("file_{}.rs", i)), content).unwrap();
    }

    // Measure indexing time
    let cache = CacheManager::new(project);
    let indexer = Indexer::new(cache, IndexConfig::default());

    let start = Instant::now();
    let stats = indexer.index(project, false).unwrap();
    let duration = start.elapsed();

    assert_eq!(stats.total_files, 100);

    // Should index 100 files in under 2.5 seconds
    // (increased from 1s to account for 18 language parsers and system variability)
    assert!(
        duration.as_millis() < 2500,
        "Indexing 100 files took {}ms, expected < 2500ms",
        duration.as_millis()
    );

    println!("✓ Indexed {} files in {}ms", stats.total_files, duration.as_millis());
}

#[test]
fn test_index_medium_codebase_performance() {
    let temp = TempDir::new().unwrap();
    let project = temp.path();

    // Create 500 files with moderate content
    for i in 0..500 {
        let content = format!(
            "fn function_{}() {{\n    let x = {};\n    let y = x + 1;\n    println!(\"{{}} {{}}\", x, y);\n}}",
            i, i
        );
        fs::write(project.join(format!("file_{}.rs", i)), content).unwrap();
    }

    // Measure indexing time
    let cache = CacheManager::new(project);
    let indexer = Indexer::new(cache, IndexConfig::default());

    let start = Instant::now();
    let stats = indexer.index(project, false).unwrap();
    let duration = start.elapsed();

    assert_eq!(stats.total_files, 500);

    // Should index 500 files in under 5 seconds
    // (increased from 3s to account for 18 language parsers and system variability)
    assert!(
        duration.as_millis() < 5000,
        "Indexing 500 files took {}ms, expected < 5000ms",
        duration.as_millis()
    );

    println!("✓ Indexed {} files in {}ms", stats.total_files, duration.as_millis());
}

#[test]
fn test_incremental_reindex_performance() {
    let temp = TempDir::new().unwrap();
    let project = temp.path();

    // Create initial set of files
    for i in 0..100 {
        fs::write(
            project.join(format!("file_{}.rs", i)),
            format!("fn test_{}() {{}}", i)
        ).unwrap();
    }

    // Initial index
    let cache = CacheManager::new(project);
    let indexer = Indexer::new(cache, IndexConfig::default());
    indexer.index(project, false).unwrap();

    // Modify only 10 files
    for i in 0..10 {
        fs::write(
            project.join(format!("file_{}.rs", i)),
            format!("fn modified_{}() {{}}", i)
        ).unwrap();
    }

    // Measure incremental reindex time
    let cache = CacheManager::new(project);
    let indexer = Indexer::new(cache, IndexConfig::default());

    let start = Instant::now();
    indexer.index(project, false).unwrap();
    let duration = start.elapsed();

    // Incremental reindex should be fast (most files unchanged)
    // (increased from 1000ms to 1500ms to account for system variability)
    assert!(
        duration.as_millis() < 1500,
        "Incremental reindex took {}ms, expected < 1500ms",
        duration.as_millis()
    );

    println!("✓ Incremental reindex (10/100 files changed) took {}ms", duration.as_millis());
}

// ==================== Query Performance Tests ====================

#[test]
fn test_fulltext_query_performance() {
    let temp = TempDir::new().unwrap();
    let project = temp.path();

    // Create 200 files
    for i in 0..200 {
        let content = format!("fn function_{}() {{\n    println!(\"hello world\");\n}}", i);
        fs::write(project.join(format!("file_{}.rs", i)), content).unwrap();
    }

    // Index
    let cache = CacheManager::new(project);
    let indexer = Indexer::new(cache, IndexConfig::default());
    indexer.index(project, false).unwrap();

    // Measure query time
    let cache = CacheManager::new(project);
    let engine = QueryEngine::new(cache);
    let filter = QueryFilter {
        limit: None,  // No limit for performance test
        ..Default::default()
    };

    let start = Instant::now();
    let results = engine.search("hello", filter).unwrap();
    let duration = start.elapsed();

    assert!(results.len() >= 200);

    // Query should be sub-150ms
    // (increased from 100ms to account for system variability)
    assert!(
        duration.as_millis() < 150,
        "Full-text query took {}ms, expected < 150ms",
        duration.as_millis()
    );

    println!("✓ Full-text query found {} results in {}ms", results.len(), duration.as_millis());
}

#[test]
fn test_symbol_query_performance() {
    let temp = TempDir::new().unwrap();
    let project = temp.path();

    // Create 100 files with various symbols
    for i in 0..100 {
        let content = format!(
            "fn greet_{}() {{}}\nstruct Point_{} {{}}\nimpl Point_{} {{\n    fn new() -> Self {{ Point_{} {{}} }}\n}}",
            i, i, i, i
        );
        fs::write(project.join(format!("file_{}.rs", i)), content).unwrap();
    }

    // Index
    let cache = CacheManager::new(project);
    let indexer = Indexer::new(cache, IndexConfig::default());
    indexer.index(project, false).unwrap();

    // Measure symbol query time
    let cache = CacheManager::new(project);
    let engine = QueryEngine::new(cache);
    let filter = QueryFilter {
        symbols_mode: true,
        use_contains: true,  // "gre" is substring of "greet", not at word boundary
        ..Default::default()
    };

    let start = Instant::now();
    let results = engine.search("gre", filter).unwrap();
    let duration = start.elapsed();

    assert!(results.len() >= 1);

    // Symbol query with runtime parsing may be slower for large result sets
    // Trigrams narrow to ~100 files, then tree-sitter parses each
    // On small codebases with good trigram filtering: <100ms
    // On larger codebases or broad patterns: may be 1-6 seconds
    // This is the trade-off: no upfront indexing cost, but query-time parsing
    // (increased from 5s to 6s to account for additional language parsers)
    assert!(
        duration.as_millis() < 6000,
        "Symbol query took {}ms, expected < 6000ms",
        duration.as_millis()
    );

    println!("✓ Symbol query found {} results in {}ms (runtime tree-sitter parsing)", results.len(), duration.as_millis());
}

#[test]
fn test_regex_query_performance() {
    let temp = TempDir::new().unwrap();
    let project = temp.path();

    // Create 150 files
    for i in 0..150 {
        let content = format!("fn test_{}() {{}}\nfn helper_{}() {{}}", i, i);
        fs::write(project.join(format!("file_{}.rs", i)), content).unwrap();
    }

    // Index
    let cache = CacheManager::new(project);
    let indexer = Indexer::new(cache, IndexConfig::default());
    indexer.index(project, false).unwrap();

    // Measure regex query time
    let cache = CacheManager::new(project);
    let engine = QueryEngine::new(cache);
    let filter = QueryFilter {
        use_regex: true,
        limit: None,  // No limit for performance test
        ..Default::default()
    };

    let start = Instant::now();
    let results = engine.search(r"fn test_\d+", filter).unwrap();
    let duration = start.elapsed();

    assert!(results.len() >= 150);

    // Regex query should be fast (trigram-optimized)
    assert!(
        duration.as_millis() < 200,
        "Regex query took {}ms, expected < 200ms",
        duration.as_millis()
    );

    println!("✓ Regex query found {} results in {}ms", results.len(), duration.as_millis());
}

#[test]
fn test_filtered_query_performance() {
    let temp = TempDir::new().unwrap();
    let project = temp.path();

    // Create mixed language files
    for i in 0..100 {
        fs::write(project.join(format!("file_{}.rs", i)), "fn test() {}").unwrap();
        fs::write(project.join(format!("file_{}.py", i)), "def test(): pass").unwrap();
    }

    // Index
    let cache = CacheManager::new(project);
    let indexer = Indexer::new(cache, IndexConfig::default());
    indexer.index(project, false).unwrap();

    // Measure filtered query time
    let cache = CacheManager::new(project);
    let engine = QueryEngine::new(cache);
    let filter = QueryFilter {
        language: Some(reflex::Language::Rust),
        ..Default::default()
    };

    let start = Instant::now();
    let results = engine.search("test", filter).unwrap();
    let duration = start.elapsed();

    // Should only find Rust files
    assert!(results.iter().all(|r| r.path.ends_with(".rs")));

    // Filtered query should be fast
    assert!(
        duration.as_millis() < 150,
        "Filtered query took {}ms, expected < 150ms",
        duration.as_millis()
    );

    println!("✓ Filtered query found {} Rust files in {}ms", results.len(), duration.as_millis());
}

// ==================== Memory-mapped I/O Performance Tests ====================

#[test]
fn test_repeated_queries_use_cached_index() {
    let temp = TempDir::new().unwrap();
    let project = temp.path();

    // Create 100 files
    for i in 0..100 {
        fs::write(project.join(format!("file_{}.rs", i)), "fn test() {}").unwrap();
    }

    // Index once
    let cache = CacheManager::new(project);
    let indexer = Indexer::new(cache, IndexConfig::default());
    indexer.index(project, false).unwrap();

    // Measure 10 repeated queries
    let cache = CacheManager::new(project);
    let engine = QueryEngine::new(cache);
    let filter = QueryFilter::default();

    let start = Instant::now();
    for _ in 0..10 {
        let _ = engine.search("test", filter.clone()).unwrap();
    }
    let duration = start.elapsed();

    let avg_ms = duration.as_millis() / 10;

    // Each query should be fast (memory-mapped)
    // (increased from 50ms to 75ms to account for system variability)
    assert!(
        avg_ms < 75,
        "Average query time {}ms, expected < 75ms",
        avg_ms
    );

    println!("✓ 10 repeated queries averaged {}ms each", avg_ms);
}

// ==================== Scalability Tests ====================

#[test]
fn test_large_file_handling() {
    let temp = TempDir::new().unwrap();
    let project = temp.path();

    // Create one large file (1000 lines)
    let content = (0..1000)
        .map(|i| format!("fn function_{}() {{}}", i))
        .collect::<Vec<_>>()
        .join("\n");
    fs::write(project.join("large.rs"), content).unwrap();

    // Measure indexing time for large file
    let cache = CacheManager::new(project);
    let indexer = Indexer::new(cache, IndexConfig::default());

    let start = Instant::now();
    indexer.index(project, false).unwrap();
    let duration = start.elapsed();

    // Should handle large file efficiently
    // (increased from 500ms to 1000ms to account for system variability)
    assert!(
        duration.as_millis() < 1000,
        "Indexing 1000-line file took {}ms, expected < 1000ms",
        duration.as_millis()
    );

    println!("✓ Indexed 1000-line file in {}ms", duration.as_millis());
}

#[test]
fn test_many_small_files_handling() {
    let temp = TempDir::new().unwrap();
    let project = temp.path();

    // Create 1000 tiny files
    for i in 0..1000 {
        fs::write(project.join(format!("tiny_{}.rs", i)), "fn f() {}").unwrap();
    }

    // Measure indexing time
    let cache = CacheManager::new(project);
    let indexer = Indexer::new(cache, IndexConfig::default());

    let start = Instant::now();
    let stats = indexer.index(project, false).unwrap();
    let duration = start.elapsed();

    assert_eq!(stats.total_files, 1000);

    // Should handle many small files efficiently
    // (increased from 2000ms to 6000ms to account for 18 language parsers and system variability)
    assert!(
        duration.as_millis() < 6000,
        "Indexing 1000 small files took {}ms, expected < 6000ms",
        duration.as_millis()
    );

    println!("✓ Indexed 1000 small files in {}ms", duration.as_millis());
}