Refactor query system for performance and memory reduction

This change introduces dynamic chunk sizing based on input size estimates, ensuring memory constraints are respected while allowing for smaller chunks that yield significant speed improvements. Additionally, index structures were optimized, resulting in drastic memory footprint reduction and faster query execution times.
This commit is contained in:
Eric Coissac committed 2026-10-15 12:21:38 +02:00
1 parent 025c40c0db
commit f9edefd135
6 files changed
+152 -3

No files matched your search

+24 -1
View File
@@ -192,17 +192,40 @@ pub fn run(args: QueryArgs) {
let overhead_multiplier =
BASE_OVERHEAD + n_genomes as u64 * BYTES_PER_KMER_PER_GENOME * detail_factor;
// Parallelism: a chunk is one worker's unit of work, so an input smaller
// than `n_workers` chunks leaves cores idle (two 48 MB files at the
// memory-derived 256 MiB cap = 2 chunks = 2 busy cores; measured 11.6 s
// against 2.5 s with 8 MiB chunks, see `DevDocMD/architecture/query.md`).
// When the input size can be estimated, aim for `CHUNKS_PER_WORKER` chunks
// per worker and never go below `MIN_CHUNK`. Stdin and URLs have no
// known size: the memory-derived size stands.
const CHUNKS_PER_WORKER: u64 = 2;
const MIN_CHUNK: u64 = 4 * 1024 * 1024;
let input_bytes: Option<u64> = args
.inputs
.iter()
.map(|p| obiread::estimated_decompressed_len(p))
.sum::<Option<u64>>();
let chunk_bytes = args
.chunk_size
.map(|mb| mb * 1024 * 1024)
.unwrap_or_else(|| {
let avail = available_memory_bytes();
let computed = avail / (n_workers as u64 * overhead_multiplier * SAFETY_FACTOR);
computed.clamp(4 * 1024 * 1024, 256 * 1024 * 1024) as usize
let by_memory = computed.clamp(4 * 1024 * 1024, 256 * 1024 * 1024);
match input_bytes {
Some(total) => {
let by_parallelism = (total / (n_workers as u64 * CHUNKS_PER_WORKER)).max(MIN_CHUNK);
by_memory.min(by_parallelism) as usize
}
None => by_memory as usize,
}
});
debug!(
chunk_bytes,
input_bytes,
n_genomes,
detail = args.detail,
overhead_multiplier,