diff --git a/crates/clawhdf5-format/src/storage.rs b/crates/clawhdf5-format/src/storage.rs index c0a1c37..7d68eeb 100644 --- a/crates/clawhdf5-format/src/storage.rs +++ b/crates/clawhdf5-format/src/storage.rs @@ -198,6 +198,14 @@ pub fn read_exact_at( .saturating_add(len), available: len_usize(file), }; + // In-memory fast path: one dynamic call, then plain slicing. + if let Some(all) = file.as_contiguous() { + return usize::try_from(offset) + .ok() + .and_then(|start| all.get(start..start.checked_add(len)?)) + .map(Cow::Borrowed) + .ok_or_else(eof); + } match offset.checked_add(len as u64) { Some(end) if end <= file.len() => {} _ => return Err(eof()), @@ -271,6 +279,11 @@ pub fn read_upto( offset: u64, max: usize, ) -> Result, FormatError> { + if let Some(all) = file.as_contiguous() { + let start = usize::try_from(offset).map_or(all.len(), |o| o.min(all.len())); + let end = start.saturating_add(max).min(all.len()); + return Ok(Cow::Borrowed(&all[start..end])); + } let avail = file.len().saturating_sub(offset); let len = usize::try_from(avail).map_or(max, |a| a.min(max)); let bytes = file.read_at(offset, len)?;