forge
Explore
Log in

Don't you dare go Hollow

iz: sniff big uploads off the runtime; vectorizable contains()

Browse files at this commit

4a255c52247e8a087d48baa17fb4c928f647949aAhmet Kaan GümüşParent: f5cf7c5
- files.rs: uploads over 8MiB sniff on the blocking pool (detail.rs idiom)
- sniff.rs: contains() skips to the needle's first byte instead of
  comparing every window position
crates/iz-core/src/store/sniff.rsmodified+18 -3
@@ -121,9 +121,24 @@ fn zip_mime(bytes: &[u8]) -> &'static str {
121 /// Whether `haystack` holds `needle` anywhere in it. Used on zip bytes, where121 /// Whether `haystack` holds `needle` anywhere in it. Used on zip bytes, where
122 /// the entry names sit in the clear even when the entries themselves do not.122 /// the entry names sit in the clear even when the entries themselves do not.
123 fn contains(haystack: &[u8], needle: &[u8]) -> bool {123 fn contains(haystack: &[u8], needle: &[u8]) -> bool {
124- haystack124+ // Skip to the needle's first byte before comparing the rest — a
125- .windows(needle.len())125+ // window-per-position compare over a half-gigabyte upload stalls the
126- .any(|window| window == needle)126+ // runtime; the byte scan vectorizes, the full compare runs on hits only.
127+ let Some((&first, _)) = needle.split_first() else {
128+ return true;
129+ };
130+ let mut offset = 0usize;
131+ while let Some(at) = haystack[offset..].iter().position(|&b| b == first) {
132+ let start = offset + at;
133+ if haystack.len() - start < needle.len() {
134+ return false;
135+ }
136+ if &haystack[start..start + needle.len()] == needle {
137+ return true;
138+ }
139+ offset = start + 1;
140+ }
141+ false
127 }142 }
128 143
129 /// The office mime declared by the zip entry names in `bytes` — the central144 /// The office mime declared by the zip entry names in `bytes` — the central
crates/iz-web/src/files.rsmodified+17 -1
@@ -407,7 +407,23 @@ async fn upload(
407 let mut added: Result<(), Refusal> = Ok(());407 let mut added: Result<(), Refusal> = Ok(());
408 for (file_name, bytes) in files {408 for (file_name, bytes) in files {
409 let label = label_of(&file_name);409 let label = label_of(&file_name);
410- let mime_type = sniff(&bytes);410+ // Sniffing's slow paths scan the whole payload; past a size where a
411+ // worker-thread second is every other request waiting, the scan runs
412+ // on the blocking pool (same door detail.rs parses sheets through).
413+ let (bytes, mime_type) = if bytes.len() <= 8 * 1024 * 1024 {
414+ let mime_type = sniff(&bytes);
415+ (bytes, mime_type)
416+ } else {
417+ match tokio::task::spawn_blocking(move || {
418+ let mime_type = sniff(&bytes);
419+ (bytes, mime_type)
420+ })
421+ .await
422+ {
423+ Ok(pair) => pair,
424+ Err(_) => return Ok(back_to(&task_id, Some(Refusal::Unavailable))),
425+ }
426+ };
411 427
412 let this = match backend {428 let this = match backend {
413 StorageBackend::Local => store429 StorageBackend::Local => store