[BUG] frame_context lowercases the whole document once per query word
Author: rohitdevsolCreated Aug 9, 2026Updated Aug 21, 2026
Labelsbug
Bug Description
In src/memvid/frame.rs (around 386) , the code lowercases the entire document again for every single word in the query
.map(|needle| content.to_lowercase().matches(&needle).count()) Because content.to_lowercase() is inside the loop, a 30 word query(I tested with 10 first then 30) lowercases the whole document 30 times instead of once. That's a lot of wasted work and memory allocation on big documents.
The fix is one line - lowercase it once before the loop and reuse it:
let lowered = content.to_lowercase();
I benchmarked the pattern with cargo bench (30-word query, and a large document )
- buggy (current): ~552 µs
- fixed (lowercase once): ~496 µs
- about 10% faster, with no downside
my test screenshot
I performed benchmarks using this code.
use std::hint::black_box;
use criterion::{ criterion_group, criterion_main, Criterion };
fn count_buggy(content: &str, query: &str) -> usize {
let mut total = 0;
for word in query.split_whitespace() {
total += content.to_lowercase().matches(&word.to_lowercase()).count();
}
total
}
fn count_fixed(content: &str, query: &str) -> usize {
let lowered = content.to_lowercase();
let mut total = 0;
for word in query.split_whitespace() {
total += lowered.matches(&word.to_lowercase()).count();
}
total
}
fn bench(c: &mut Criterion) {
let content = "Memvid is fast and good. Other memory libraries are slow and noob. ".repeat(
1000
);
let query =
"memvid is good fast others are noob slow memvid is good fast \
others are noob slow memvid is good fast others are noob slow \
memvid is good fast";
c.bench_function("buggy", |b| {
b.iter(|| count_buggy(black_box(&content), black_box(query)))
});
c.bench_function("fixed", |b| {
b.iter(|| count_fixed(black_box(&content), black_box(query)))
});
}
criterion_group!(benches, bench);
criterion_main!(benches);Source: memvid/memvid