From 549e442aff3ff418ca6636797d3c2b673f3953c2 Mon Sep 17 00:00:00 2001 From: osobh Date: Tue, 29 Sep 2026 21:16:01 -0500 Subject: [PATCH] FileBuilder::write: 64 KiB write buffer, not 1 MiB A 1 MiB BufWriter sits above glibc's 128 KiB mmap threshold; depending on allocator history it was mapped afresh by every write, and its page faults (1.6 M vs 13 K over the criterion write_2d_chunked group) made 1 MiB chunked writes 1.35x-1.83x slower than main. With 64 KiB they match main. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/clawhdf5/src/writer.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/clawhdf5/src/writer.rs b/crates/clawhdf5/src/writer.rs index eb05b97..7949d13 100644 --- a/crates/clawhdf5/src/writer.rs +++ b/crates/clawhdf5/src/writer.rs @@ -289,7 +289,12 @@ fn write_file_atomically_with>( let tmp_path = path.with_file_name(tmp_name); let result = (|| -> Result<(), E> { - let mut f = std::io::BufWriter::with_capacity(1 << 20, std::fs::File::create(&tmp_path)?); + // 64 KiB, below glibc's 128 KiB mmap threshold: a 1 MiB buffer was + // mapped afresh by each write once the threshold had not risen, and + // its page faults made a 1 MiB chunked write 1.6x slower (criterion + // `write_2d_chunked/512x512` after the smaller cases, 2026-09-29). + // Writes larger than the buffer go straight to the file. + let mut f = std::io::BufWriter::with_capacity(64 << 10, std::fs::File::create(&tmp_path)?); write(&mut f)?; f.flush()?; f.get_ref().sync_all()?;