Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
Seanaye/perf/batch opfs writes (#6875)
Sean Aye72b39098142e
diff --git a/crates/client/README.md b/crates/client/README.mdindex 13a551a916..0b8dc0ac58 100644--- a/crates/client/README.md+++ b/crates/client/README.md@@ -39,6 +39,14 @@ make retries incorrectly look unchanged. With no optimistic layers, the changedrecord keys also identify visible changes without duplicate before/after snapshots.Pending layers still use full effective-view comparison and rebasing.+## Browser OPFS writes++The OPFS adapter coalesces each Turso vectored write into batches of at most+1 MiB instead of making one synchronous browser call per WAL frame. Scratch+space is bounded to the same size. Batching never spans separate I/O operations+or delays completion/flushes; offset preflight, partial-write retries, and+first-error propagation retain their existing semantics.+## Projection refreshesHydration folds authoritative index mutations in order and writes only finaldiff --git a/crates/client/turso-opfs/src/browser.rs b/crates/client/turso-opfs/src/browser.rsindex 2aa8d74461..44e0dbb5bc 100644--- a/crates/client/turso-opfs/src/browser.rs+++ b/crates/client/turso-opfs/src/browser.rs@@ -946,6 +946,7 @@ struct TestFaults {connection_close_calls: usize,reset_fault: Option<ResetFault>,max_write_chunk: Option<usize>,+ write_calls: usize,next_write_error: Option<CompletionError>,write_error_after_chunks: Option<(usize, CompletionError)>,}@@ -1119,21 +1120,22 @@ impl File for OpfsFile {let expected =preflight_write_range(pos, buffers.iter().map(|buffer| buffer.as_slice().len()))?;let mut total = 0_usize;- for buffer in &buffers {- let written = write_all(- pos,- buffer.as_slice(),- self.owner,- self.session,- self.handle,- )?;- total = total- .checked_add(written)- .ok_or(CompletionError::ShortWrite)?;- pos = pos- .checked_add(written as u64)- .ok_or(CompletionError::ShortWrite)?;- }+ // OPFS has no native writev: one browser call per WAL page can+ // dominate COMMIT. Coalesce only this vector, with bounded scratch+ // space; write_all still handles partial writes and sync is unchanged.+ crate::write_batch::write_batches(+ buffers.iter().map(|buffer| buffer.as_slice()),+ |bytes| {+ let written = write_all(pos, bytes, self.owner, self.session, self.handle)?;+ total = total+ .checked_add(written)+ .ok_or(CompletionError::ShortWrite)?;+ pos = pos+ .checked_add(written as u64)+ .ok_or(CompletionError::ShortWrite)?;+ Ok(())+ },+ )?;if total == expected {completion_count(total)} else {@@ -2001,6 +2003,7 @@ fn write_once(#[cfg(test)]let (chunk_limit, injected_error) = REGISTRY.with(|registry| {let mut registry = registry.borrow_mut();+ registry.faults.write_calls += 1;let injected = registry.faults.next_write_error.take().or_else(|| {let fault = registry.faults.write_error_after_chunks.as_mut()?;if fault.0 == 0 {diff --git a/crates/client/turso-opfs/src/browser/test.rs b/crates/client/turso-opfs/src/browser/test.rsindex f855fada40..4852c5fcdb 100644--- a/crates/client/turso-opfs/src/browser/test.rs+++ b/crates/client/turso-opfs/src/browser/test.rs@@ -15,6 +15,8 @@ use web_sys::{FileSystemGetDirectoryOptions, FileSystemGetFileOptions, FileSystewasm_bindgen_test_configure!(run_in_dedicated_worker);+mod write_batch;+#[derive(Debug)]struct TrackedCompletion {completion: Completion,@@ -509,6 +511,7 @@ async fn real_worker_opfs_contract_and_consuming_lifecycle() {);assert_completion(&vectored, Ok(4));assert_eq!(main.size().expect("size after writes"), 10);+ write_batch::check(&main);let before_overflow = main.size().expect("size before overflow probes");let overflow = write_completion();diff --git a/crates/client/turso-opfs/src/browser/test/write_batch.rs b/crates/client/turso-opfs/src/browser/test/write_batch.rsnew file mode 100644index 0000000000..ad9e0b0324--- /dev/null+++ b/crates/client/turso-opfs/src/browser/test/write_batch.rs@@ -0,0 +1,129 @@+use super::*;+use crate::write_batch::MAX_WRITE_BATCH_BYTES;++fn calls() -> usize {+ REGISTRY.with(|registry| registry.borrow().faults.write_calls)+}++fn clear(file: &Arc<dyn File>) {+ let completion = truncate_completion();+ drop(file.truncate(0, completion.completion.clone()).unwrap());+ assert_completion(&completion, Ok(0));+}++fn read_bytes(file: &Arc<dyn File>, pos: u64, expected: &[u8]) {+ let bytes = Arc::new(Buffer::new(vec![0; expected.len()]));+ let completion = read_completion(bytes.clone());+ drop(file.pread(pos, completion.completion.clone()).unwrap());+ assert_completion(&completion, Ok(expected.len() as i32));+ assert_eq!(bytes.as_slice(), expected);+}++fn write(file: &Arc<dyn File>, pos: u64, buffers: Vec<Vec<u8>>) -> TrackedCompletion {+ let completion = write_completion();+ drop(+ file.pwritev(+ pos,+ buffers+ .into_iter()+ .map(|bytes| Arc::new(Buffer::new(bytes)))+ .collect(),+ completion.completion.clone(),+ )+ .unwrap(),+ );+ completion+}++pub(super) fn check(file: &Arc<dyn File>) {+ clear(file);+ for buffers in [vec![], vec![vec![], vec![]]] {+ let before = calls();+ let completion = write(file, u64::MAX, buffers);+ assert_completion(&completion, Ok(0));+ assert_eq!(calls(), before);+ assert_eq!(file.size().unwrap(), 0);+ }++ for buffers in [+ vec![+ vec![1; MAX_WRITE_BATCH_BYTES - 7],+ vec![],+ vec![2; 19],+ vec![3; MAX_WRITE_BATCH_BYTES + 13],+ ],+ (0..774)+ .map(|index| vec![(index % 251) as u8; 4096 + 24])+ .collect(),+ ] {+ clear(file);+ let expected = buffers.concat();+ let before = calls();+ let completion = write(file, 11, buffers);+ assert_completion(&completion, Ok(expected.len() as i32));+ assert_eq!(+ calls() - before,+ expected.len().div_ceil(MAX_WRITE_BATCH_BYTES),+ "bounded coalesced browser writes"+ );+ assert_eq!(file.size().unwrap(), 11 + expected.len() as u64);+ read_bytes(file, 11, &expected);+ }++ clear(file);+ REGISTRY.with(|registry| registry.borrow_mut().faults.max_write_chunk = Some(2));+ let before = calls();+ let completion = write(+ file,+ 0,+ vec![b"a".to_vec(), b"bc".to_vec(), b"def".to_vec()],+ );+ assert_completion(&completion, Ok(6));+ assert_eq!(+ calls() - before,+ 3,+ "short writes retry across input boundaries"+ );+ read_bytes(file, 0, b"abcdef");++ let injected = CompletionError::IOError(ErrorKind::StorageFull, "batched write failure");+ clear(file);+ REGISTRY.with(|registry| {+ registry.borrow_mut().faults.write_error_after_chunks = Some((1, injected));+ });+ let before = calls();+ let completion = write(+ file,+ 0,+ vec![b"a".to_vec(), b"bc".to_vec(), b"def".to_vec()],+ );+ assert_completion(&completion, Err(injected));+ assert_eq!(calls() - before, 2);+ assert_eq!(file.size().unwrap(), 2);+ read_bytes(file, 0, b"ab");++ clear(file);+ REGISTRY.with(|registry| registry.borrow_mut().faults.max_write_chunk = Some(0));+ let completion = write(file, 0, vec![b"a".to_vec(), b"b".to_vec()]);+ assert_completion(&completion, Err(CompletionError::ShortWrite));+ assert_eq!(file.size().unwrap(), 0);++ REGISTRY.with(|registry| {+ let mut registry = registry.borrow_mut();+ registry.faults.max_write_chunk = None;+ registry.faults.write_error_after_chunks = Some((1, injected));+ });+ let buffers = vec![+ vec![1; MAX_WRITE_BATCH_BYTES - 1],+ vec![2; MAX_WRITE_BATCH_BYTES + 9],+ vec![3; 4],+ ];+ let expected = buffers.concat();+ let before = calls();+ let completion = write(file, 0, buffers);+ assert_completion(&completion, Err(injected));+ assert_eq!(calls() - before, 2, "no writes after a failed batch");+ assert_eq!(file.size().unwrap(), MAX_WRITE_BATCH_BYTES as u64);+ read_bytes(file, 0, &expected[..MAX_WRITE_BATCH_BYTES]);+ clear(file);+}diff --git a/crates/client/turso-opfs/src/lib.rs b/crates/client/turso-opfs/src/lib.rsindex dc67c7cb8c..82d180c229 100644--- a/crates/client/turso-opfs/src/lib.rs+++ b/crates/client/turso-opfs/src/lib.rs@@ -18,6 +18,8 @@#[cfg(any(test, target_arch = "wasm32"))]mod state;+#[cfg(any(test, target_arch = "wasm32"))]+mod write_batch;#[cfg(target_arch = "wasm32")]mod browser;diff --git a/crates/client/turso-opfs/src/write_batch.rs b/crates/client/turso-opfs/src/write_batch.rsnew file mode 100644index 0000000000..cf9484f66c--- /dev/null+++ b/crates/client/turso-opfs/src/write_batch.rs@@ -0,0 +1,41 @@+//! Coalesce one vectored write without retaining bytes across I/O operations.++/// Bound both scratch space and the size of each browser write.+pub(crate) const MAX_WRITE_BATCH_BYTES: usize = 1024 * 1024;++/// The callback must write the entire batch or return an error. Stop on the first+/// error; the caller retains responsibility for offsets, short writes and flushes.+pub(crate) fn write_batches<'a, E>(+ buffers: impl Iterator<Item = &'a [u8]> + Clone,+ mut write: impl FnMut(&[u8]) -> Result<(), E>,+) -> Result<(), E> {+ let capacity = buffers.clone().fold(0_usize, |size, bytes| {+ size.saturating_add(bytes.len()).min(MAX_WRITE_BATCH_BYTES)+ });+ let mut batch = Vec::with_capacity(capacity);+ for mut bytes in buffers {+ while !bytes.is_empty() {+ // Avoid copying complete batches (including a single large buffer).+ if batch.is_empty() && bytes.len() >= capacity {+ let (chunk, rest) = bytes.split_at(capacity);+ write(chunk)?;+ bytes = rest;+ continue;+ }+ let count = bytes.len().min(capacity - batch.len());+ batch.extend_from_slice(&bytes[..count]);+ bytes = &bytes[count..];+ if batch.len() == capacity {+ write(&batch)?;+ batch.clear();+ }+ }+ }+ if !batch.is_empty() {+ write(&batch)?;+ }+ Ok(())+}++#[cfg(test)]+mod test;diff --git a/crates/client/turso-opfs/src/write_batch/test.rs b/crates/client/turso-opfs/src/write_batch/test.rsnew file mode 100644index 0000000000..4a49592842--- /dev/null+++ b/crates/client/turso-opfs/src/write_batch/test.rs@@ -0,0 +1,81 @@+use super::*;++fn assert_batches(buffers: &[Vec<u8>]) -> Vec<usize> {+ let expected = buffers.concat();+ let mut actual = Vec::new();+ let mut lengths = Vec::new();+ write_batches(buffers.iter().map(Vec::as_slice), |bytes| {+ assert!(!bytes.is_empty());+ assert!(bytes.len() <= MAX_WRITE_BATCH_BYTES);+ actual.extend_from_slice(bytes);+ lengths.push(bytes.len());+ Ok::<_, ()>(())+ })+ .unwrap();+ assert_eq!(actual, expected);+ assert_eq!(+ lengths.len(),+ expected.len().div_ceil(MAX_WRITE_BATCH_BYTES)+ );+ lengths+}++#[test]+fn empty_vectors_do_not_write() {+ assert!(assert_batches(&[]).is_empty());+ assert!(assert_batches(&[vec![], vec![]]).is_empty());+}++#[test]+fn small_buffers_and_empty_entries_preserve_order_in_one_write() {+ assert_eq!(+ assert_batches(&[b"12".to_vec(), vec![], b"345".to_vec(), vec![]]),+ [5]+ );+ assert_eq!(assert_batches(&[b"single buffer".to_vec()]), [13]);+}++#[test]+fn hundreds_of_wal_frames_use_bounded_batches_not_one_write_per_frame() {+ let buffers = (0..774)+ .map(|index| vec![(index % 251) as u8; 4096 + 24])+ .collect::<Vec<_>>();+ let lengths = assert_batches(&buffers);+ assert_eq!(lengths.len(), 4);+ assert_eq!(lengths[..3], [MAX_WRITE_BATCH_BYTES; 3]);+}++#[test]+fn batches_split_inside_buffers_and_at_exact_boundaries() {+ for prefix in [0, 1, MAX_WRITE_BATCH_BYTES - 1, MAX_WRITE_BATCH_BYTES] {+ for middle in [0, 1, MAX_WRITE_BATCH_BYTES, MAX_WRITE_BATCH_BYTES * 2 + 1] {+ assert_batches(&[+ vec![1; prefix],+ vec![],+ vec![2; middle],+ vec![3; MAX_WRITE_BATCH_BYTES + 17],+ ]);+ }+ }+}++#[test]+fn errors_stop_later_batches_and_preserve_the_written_prefix() {+ for fail_at in 0..3 {+ let buffers = vec![vec![1; MAX_WRITE_BATCH_BYTES + 1]; 2];+ let mut calls = 0;+ let mut written = Vec::new();+ let result = write_batches(buffers.iter().map(Vec::as_slice), |bytes| {+ let call = calls;+ calls += 1;+ if call == fail_at {+ return Err("storage full");+ }+ written.extend_from_slice(bytes);+ Ok(())+ });+ assert_eq!(result, Err("storage full"));+ assert_eq!(calls, fail_at + 1);+ assert_eq!(written, vec![1; fail_at * MAX_WRITE_BATCH_BYTES]);+ }+}