diff --git a/crates/uorc-wasm/src/lib.rs b/crates/uorc-wasm/src/lib.rs index 9e673eab..2dfcca7d 100644 --- a/crates/uorc-wasm/src/lib.rs +++ b/crates/uorc-wasm/src/lib.rs @@ -1,4 +1,5 @@ -//! WebAssembly exports of the uorc client, with no unsafe code and no bindgen. +//! WebAssembly exports of the uorc client, with no bindgen; the only unsafe +//! code is the call of the page's progress import. //! //! The page asks for an input buffer of a given size, writes the bytes into //! wasm memory at the returned offset, calls an operation, then reads the @@ -285,6 +286,186 @@ pub extern "C" fn decompress_tree() -> i32 { })) } +/// Little-endian reader over a request in the input buffer. +struct Fields<'a> { + bytes: &'a [u8], + at: usize, +} + +impl<'a> Fields<'a> { + fn take(&mut self, n: usize) -> Result<&'a [u8], uorc::Error> { + let end = self.at.checked_add(n).ok_or(uorc::Error::Decode)?; + let out = self.bytes.get(self.at..end).ok_or(uorc::Error::Decode)?; + self.at = end; + Ok(out) + } + fn u32(&mut self) -> Result { + Ok(u32::from_le_bytes(self.take(4)?.try_into().expect("4 bytes")) as usize) + } + fn u64(&mut self) -> Result { + Ok(u64::from_le_bytes( + self.take(8)?.try_into().expect("8 bytes"), + )) + } + fn len(&mut self) -> Result { + usize::try_from(self.u64()?).map_err(|_| uorc::Error::Decode) + } + fn pieces(&mut self) -> Result, uorc::Error> { + let n = self.u32()?; + (0..n).map(|_| self.u64()).collect() + } + fn end(&self) -> Result<(), uorc::Error> { + if self.at == self.bytes.len() { + Ok(()) + } else { + Err(uorc::Error::Decode) + } + } +} + +fn plan_records(planned: Vec) -> Vec { + let mut out = (planned.len() as u32).to_le_bytes().to_vec(); + for frame in planned { + match frame { + uorc::Planned::Done { length, archive } => { + out.push(0); + out.extend(length.to_le_bytes()); + out.extend((archive.len() as u64).to_le_bytes()); + out.extend(archive); + } + uorc::Planned::Code { + bytes, + pieces, + window, + } => { + out.push(1); + out.extend((bytes.len() as u64).to_le_bytes()); + out.extend((pieces.len() as u32).to_le_bytes()); + for p in pieces { + out.extend(p.to_le_bytes()); + } + out.extend((window.len() as u32).to_le_bytes()); + } + } + } + out +} + +/// [`compress_frame`] without the coding, for a page that codes frames on +/// other cores: the output is `u32 count` then, per frame the input buffer +/// completes, `u8 0, u64 frameLength, u64 archiveLength, archive` for a +/// finished repeat, or +/// `u8 1, u64 frameLength, u32 pieceCount, u64 piece*, u32 window` for a frame +/// to code with [`code_frame`] after the `window` frames before it. Frames +/// follow the stream's input back to back, repeats included. +#[no_mangle] +pub extern "C" fn plan_frame() -> i32 { + finish(INPUT.with(|input| { + Ok(plan_records(ENCODER.with(|encoder| { + encoder.borrow_mut().plan(&input.borrow()) + }))) + })) +} + +/// [`compress_end`] without the coding (see [`plan_frame`]). +#[no_mangle] +pub extern "C" fn plan_end() -> i32 { + finish(Ok(plan_records( + ENCODER.with(|encoder| encoder.borrow_mut().plan_end()), + ))) +} + +/// Code one planned frame. The input buffer holds `u32 pieceCount, u64 +/// piece*, u32 window, u64 windowLength*`, then the window frames and the +/// frame, back to back. With `which` 0 or 1 the output is that scheduled +/// strategy's archive, or empty when it cannot beat the raw archive (pass +/// both to [`choose_frame`]); with any other `which`, the frame's archive. +#[no_mangle] +pub extern "C" fn code_frame(which: i32, steps: u64) -> i32 { + finish(INPUT.with(|input| { + let input = input.borrow(); + let mut f = Fields { + bytes: &input, + at: 0, + }; + let pieces = f.pieces()?; + let lengths: Vec = (0..f.u32()?).map(|_| f.len()).collect::>()?; + let window: Vec<&[u8]> = lengths + .iter() + .map(|&n| f.take(n)) + .collect::>()?; + let frame = &f.bytes[f.at..]; + if pieces.iter().sum::() != frame.len() as u64 { + return Err(uorc::Error::Decode); + } + match which { + 0 | 1 => Ok( + uorc::compress_strategy(frame, &pieces, &window, which as usize) + .unwrap_or_default(), + ), + _ => uorc::Planned::Code { + bytes: frame.to_vec(), + pieces, + window: window.iter().map(|w| w.to_vec()).collect(), + } + .code(steps), + } + })) +} + +/// The frame's archive from its two strategies' outputs of [`code_frame`]: +/// the input buffer holds `u32 pieceCount, u64 piece*, u64 frameLength, +/// frame, u64 firstLength, first, u64 secondLength, second` (an empty archive +/// for a strategy that could not beat raw). +#[no_mangle] +pub extern "C" fn choose_frame() -> i32 { + finish(INPUT.with(|input| { + let input = input.borrow(); + let mut f = Fields { + bytes: &input, + at: 0, + }; + let pieces = f.pieces()?; + let n = f.len()?; + let frame = f.take(n)?; + let mut archive = || -> Result>, uorc::Error> { + let n = f.len()?; + Ok(Some(f.take(n)?.to_vec()).filter(|a| !a.is_empty())) + }; + let (first, second) = (archive()?, archive()?); + f.end()?; + if pieces.iter().sum::() != frame.len() as u64 { + return Err(uorc::Error::Decode); + } + Ok(uorc::choose(frame, &pieces, first, second)) + })) +} + +#[cfg(target_arch = "wasm32")] +#[link(wasm_import_module = "uorc")] +extern "C" { + /// The page's progress callback: bytes done and length of the pass. + #[link_name = "progress"] + fn page_progress(done: f64, total: f64); +} + +#[cfg(target_arch = "wasm32")] +fn report(done: u64, total: u64) { + // SAFETY: an imported function of two numbers with no other effect on + // this module's memory. + unsafe { page_progress(done as f64, total as f64) } +} + +/// Report every coding and decoding pass to the page's `uorc.progress` +/// import (`on` nonzero), or stop reporting. +#[no_mangle] +pub extern "C" fn report_progress(on: i32) { + #[cfg(target_arch = "wasm32")] + uorc::ncm::set_progress((on != 0).then_some(report as uorc::ncm::Progress)); + #[cfg(not(target_arch = "wasm32"))] + let _ = on; +} + /// Start a file tree streamed through the frame operations: the input /// buffer lists its entries as `u32 count` then, per entry, `u8 kind`, /// `u32 pathLength`, path, `u64 length` (zero for a directory). The output is @@ -553,4 +734,101 @@ mod tests { } assert!(files.next().is_none()); } + + /// Frames planned in one place and coded elsewhere, a strategy at a + /// time, write the library's stream: what the page's workers do. + #[test] + fn planned_frames_coded_by_strategy_are_the_library_stream() { + let noise = |n: usize, seed: u32| -> Vec { + let mut x = seed; + (0..n) + .map(|_| { + x = x.wrapping_mul(1_103_515_245).wrapping_add(12_345); + b"abcdefgh \n"[(x >> 24) as usize % 11] + }) + .collect() + }; + let a = noise(uorc::FRAME, 1); + // a frame, the same frame again (a reference frame), then a short tail + let input = [&a[..], &a[..], &noise(70_000, 3)].concat(); + window_reset(); + let mut records = Vec::new(); + for piece in input.chunks(5_000_000) { + let (code, out) = run(piece, || plan_frame()); + assert_eq!(code, OK); + records.push(out); + } + records.push(run(&[], || plan_end()).1); + let (mut stream, mut at, mut frames) = (Vec::new(), 0usize, Vec::<&[u8]>::new()); + let mut references = 0; + for record in &records { + let mut f = Fields { + bytes: record, + at: 0, + }; + for _ in 0..f.u32().unwrap() { + let kind = f.take(1).unwrap()[0]; + let n = f.len().unwrap(); + let frame = &input[at..at + n]; + if kind == 0 { + let len = f.len().unwrap(); + stream.extend(f.take(len).unwrap()); + references += 1; + } else { + let pieces = f.pieces().unwrap(); + let before = &frames[frames.len() - f.u32().unwrap()..]; + let mut head = (pieces.len() as u32).to_le_bytes().to_vec(); + for p in &pieces { + head.extend(p.to_le_bytes()); + } + let mut request = head.clone(); + request.extend((before.len() as u32).to_le_bytes()); + for w in before { + request.extend((w.len() as u64).to_le_bytes()); + } + for w in before { + request.extend(*w); + } + request.extend(frame); + let (c0, first) = run(&request, || code_frame(0, 0)); + let (c1, second) = run(&request, || code_frame(1, 0)); + assert_eq!((c0, c1), (OK, OK)); + let mut choice = head; + for part in [frame, &first[..], &second[..]] { + choice.extend((part.len() as u64).to_le_bytes()); + choice.extend(part); + } + let (code, archive) = run(&choice, || choose_frame()); + assert_eq!(code, OK); + assert_eq!(archive, run(&request, || code_frame(-1, 0)).1); + stream.extend(archive); + } + frames.push(frame); + at += n; + } + } + assert!(references > 0); + assert_eq!(at, input.len()); + assert_eq!(stream, uorc::compress(&input, 0).expect("library stream")); + let archives = uorc::frames(&stream).expect("frames"); + assert!(archives.iter().any(|a| { + uorc::inspect(a).expect("inspect").representation == uorc::Representation::Reference + })); + window_reset(); + for archive in &archives { + if run(archive, || may_be_reference()).0 == 1 { + run(archive, || { + note_frame(); + OK + }); + } + } + let mut back = Vec::new(); + for archive in &archives { + let (code, output) = run(archive, || decompress_frame()); + assert_eq!(code, OK); + back.extend(output); + } + assert!(back == input); + } } diff --git a/crates/uorc/src/archive.rs b/crates/uorc/src/archive.rs index e4986f23..9c37ea84 100644 --- a/crates/uorc/src/archive.rs +++ b/crates/uorc/src/archive.rs @@ -867,11 +867,15 @@ pub fn window_decode(data: &[u8], window: &[&[u8]]) -> Option> { return None; } let (codec, era, payload) = window_payload(&pre[start..], k)?; - let history = window.concat(); let decode = || { if !codec.x86() { - return decode_with(codec, &history, payload, n); + return match codec.ncm_profile() { + // The model reads the window's frames in place. + Some(p) => crate::ncm::decode_canonical_window(window, payload, n, p), + None => crate::ulz::decode_with(&window.concat(), payload, n), + }; } + let history = window.concat(); let h = crate::x86::filter(&history, false, 0).0; let out = decode_with(codec, &h, payload, n)?; Some(crate::x86::filter(&out, true, history.len() as u64).0) diff --git a/crates/uorc/src/lib.rs b/crates/uorc/src/lib.rs index ca8161b1..05fed5fb 100644 --- a/crates/uorc/src/lib.rs +++ b/crates/uorc/src/lib.rs @@ -192,99 +192,138 @@ fn compress_pieces( frame::relabel(&archive, lengths) }; } - let history = window.concat(); - let identity = (!window.is_empty()).then(|| archive::window_identity(window)); - // The strategy portfolio (`Client.portfolio`): code the frame by the - // first BUDGET strategies its family's schedule orders and keep the - // smallest archive, the earlier one on a tie. A filtered strategy codes - // the filtered frame after the filtered window. + let [first, second] = portfolio::plan(frame); let raw = archive::raw_length(lengths); - // `code(strategy, learn, bound)`: the strategy's archive, or `None` when - // it cannot be shorter than `bound` bytes. An archive is at least its - // empty-payload form plus the payload, so the encoder stops once the - // payload passes `bound` less that form: an archive it would have - // finished could not have been chosen, and every archive it does finish - // is the one it always wrote. - let code = |strategy: portfolio::Strategy, learn: bool, bound: u64| -> Option> { - let filtered; - let (data, past) = if strategy.x86 { - filtered = ( - x86::filter(frame, false, history.len() as u64).0, - x86::filter(&history, false, 0).0, - ); - (&filtered.0[..], &filtered.1[..]) - } else { - (frame, &history[..]) - }; - let codec = if strategy.x86 { - archive::Codec::NcmX86 - } else { - archive::Codec::Ncm - }; - // The native ncm encoder is byte-identical to the model - // (`native_archives_are_the_model_archives`) and every encoder output - // decodes to its input, so a self-decode would only repeat its cost. - let frame_of = |profile: u32, payload: &[u8]| match &identity { - None => archive::linked_archive_profile(codec, profile, lengths, payload), - Some(identity) => { - archive::window_archive_profile(codec, profile, window, identity, lengths, payload) - } - }; - let stop = |profile: u32, bound: u64| -> usize { - let form = frame_of(profile, &[]).map_or(0, |a| a.len() as u64); - usize::try_from(bound.saturating_sub(form)).unwrap_or(usize::MAX) - }; - if !learn { - return ncm::encode_below(past, data, strategy.profile, stop(strategy.profile, bound)) - .and_then(|payload| frame_of(strategy.profile, &payload)); - } - // The learned profile (`Archive.learnedCandidate`): the strategy's - // profile without the contexts that saved nothing on this frame, - // coded too and kept when no larger. Fewer contexts decode faster. - let (payload, learned) = - ncm::encode_learn_below(past, data, strategy.profile, stop(strategy.profile, bound))?; - let plain = frame_of(strategy.profile, &payload); - if learned == strategy.profile { - return plain; - } - // the pruned frame is kept when no larger than the plain one - let keep = plain - .as_ref() - .map_or(bound, |p| bound.min(p.len() as u64 + 1)); - let pruned = ncm::encode_below(past, data, learned, stop(learned, keep)) - .and_then(|p| frame_of(learned, &p)); - match (plain, pruned) { - (Some(p), Some(l)) => Some(if l.len() <= p.len() { l } else { p }), - (p, l) => l.or(p), - } - }; // The scheduled strategies code side by side (a lone frame then uses two - // cores); the choice is the same in any order. The first also learns its - // context mask (`Client.portfolioLearned`). - // With a core to spare they code side by side, each bounded by the raw - // archive; otherwise one after the other, the second bounded by the - // first's archive as well (it is kept only when strictly smaller). - let [first, second] = portfolio::plan(frame); + // cores); the choice is the same in any order. With a core to spare each + // is bounded by the raw archive; otherwise the second is also bounded by + // the first's archive (it is kept only when strictly smaller). A bound + // never changes an archive, only whether a hopeless one finishes. let (a, b) = if side_by_side && cfg!(not(target_family = "wasm")) { std::thread::scope(|s| { - let other = s.spawn(|| code(second, false, raw)); + let other = s.spawn(|| code_strategy(frame, lengths, window, second, false, raw)); ( - code(first, true, raw), + code_strategy(frame, lengths, window, first, true, raw), other.join().expect("strategy thread"), ) }) } else { - let a = code(first, true, raw); + let a = code_strategy(frame, lengths, window, first, true, raw); let bound = a.as_ref().map_or(raw, |a| raw.min(a.len() as u64)); - (a, code(second, false, bound)) + ( + a, + code_strategy(frame, lengths, window, second, false, bound), + ) }; - let best = match (a, b) { + Ok(choose(frame, lengths, a, b)) +} + +/// The archive of scheduled strategy `which` (0 or 1, the order of +/// [`portfolio::plan`]) for a frame with objects of `lengths` after the +/// frames `window`, bounded by the raw archive: `None` when it cannot be +/// shorter. Coding both and passing them to [`choose`] gives the archive +/// [`compress_frame_with`] writes (with `steps == 0`), so callers on +/// separate cores (the page's workers) write the library's bytes. +pub fn compress_strategy( + frame: &[u8], + lengths: &[u64], + window: &[&[u8]], + which: usize, +) -> Option> { + let strategy = *portfolio::plan(frame).get(which)?; + code_strategy( + frame, + lengths, + window, + strategy, + which == 0, + archive::raw_length(lengths), + ) +} + +/// The frame's archive from its two strategies' archives (see +/// [`compress_strategy`]): the second only when strictly shorter than the +/// first, then the raw archive unless the kept one is strictly shorter. +pub fn choose( + frame: &[u8], + lengths: &[u64], + first: Option>, + second: Option>, +) -> Vec { + let best = match (first, second) { (Some(a), Some(b)) => Some(if b.len() < a.len() { b } else { a }), (a, b) => a.or(b), }; match best { - Some(linked) if (linked.len() as u64) < archive::raw_length(lengths) => Ok(linked), - _ => Ok(archive::raw_objects(frame, lengths)), + Some(linked) if (linked.len() as u64) < archive::raw_length(lengths) => linked, + _ => archive::raw_objects(frame, lengths), + } +} + +/// One strategy of the portfolio (`Client.portfolio`): its archive, or +/// `None` when it cannot be shorter than `bound` bytes. A filtered strategy +/// codes the filtered frame after the filtered window. With `learn`, the +/// learned profile is coded too and kept when no larger. +fn code_strategy( + frame: &[u8], + lengths: &[u64], + window: &[&[u8]], + strategy: portfolio::Strategy, + learn: bool, + bound: u64, +) -> Option> { + let history = window.concat(); + let identity = (!window.is_empty()).then(|| archive::window_identity(window)); + let filtered; + let (data, past) = if strategy.x86 { + filtered = ( + x86::filter(frame, false, history.len() as u64).0, + x86::filter(&history, false, 0).0, + ); + (&filtered.0[..], &filtered.1[..]) + } else { + (frame, &history[..]) + }; + let codec = if strategy.x86 { + archive::Codec::NcmX86 + } else { + archive::Codec::Ncm + }; + // The native ncm encoder is byte-identical to the model + // (`native_archives_are_the_model_archives`) and every encoder output + // decodes to its input, so a self-decode would only repeat its cost. + let frame_of = |profile: u32, payload: &[u8]| match &identity { + None => archive::linked_archive_profile(codec, profile, lengths, payload), + Some(identity) => { + archive::window_archive_profile(codec, profile, window, identity, lengths, payload) + } + }; + let stop = |profile: u32, bound: u64| -> usize { + let form = frame_of(profile, &[]).map_or(0, |a| a.len() as u64); + usize::try_from(bound.saturating_sub(form)).unwrap_or(usize::MAX) + }; + if !learn { + return ncm::encode_below(past, data, strategy.profile, stop(strategy.profile, bound)) + .and_then(|payload| frame_of(strategy.profile, &payload)); + } + // The learned profile (`Archive.learnedCandidate`): the strategy's + // profile without the contexts that saved nothing on this frame, + // coded too and kept when no larger. Fewer contexts decode faster. + let (payload, learned) = + ncm::encode_learn_below(past, data, strategy.profile, stop(strategy.profile, bound))?; + let plain = frame_of(strategy.profile, &payload); + if learned == strategy.profile { + return plain; + } + // the pruned frame is kept when no larger than the plain one + let keep = plain + .as_ref() + .map_or(bound, |p| bound.min(p.len() as u64 + 1)); + let pruned = ncm::encode_below(past, data, learned, stop(learned, keep)) + .and_then(|p| frame_of(learned, &p)); + match (plain, pruned) { + (Some(p), Some(l)) => Some(if l.len() <= p.len() { l } else { p }), + (p, l) => l.or(p), } } @@ -1198,36 +1237,97 @@ impl FrameEncoder { /// The next piece of input: returns the archives of the frames it /// completes, back to back (none until more than a frame is pending). pub fn push(&mut self, input: &[u8], steps: u64) -> Result, Error> { - let frames = self.framer.push(input); - self.code(frames, steps) + let planned = self.plan(input); + code_all(planned, steps) } /// The end of input: the archives of the remaining frames. pub fn finish(&mut self, steps: u64) -> Result, Error> { + let planned = self.plan_end(); + code_all(planned, steps) + } + + /// [`FrameEncoder::push`] without the coding: the frames it completes, + /// each a finished repeat or a frame with the window it reads, so that a + /// caller can code them on other cores ([`Planned::code`], or + /// [`compress_strategy`] and [`choose`]) and write the archives in order. + pub fn plan(&mut self, input: &[u8]) -> Vec { + let frames = self.framer.push(input); + self.planned(frames) + } + + /// [`FrameEncoder::finish`] without the coding (see [`FrameEncoder::plan`]). + pub fn plan_end(&mut self) -> Vec { let frames = self.framer.finish(); - self.code(frames, steps) + self.planned(frames) } - fn code(&mut self, frames: Vec, steps: u64) -> Result, Error> { + fn planned(&mut self, frames: Vec) -> Vec { let mut out = Vec::new(); for frame in frames { - let archive = match frame.reference { - Some(label) => reference_or_raw(&frame.bytes, &label, &frame.pieces), - None => { - let window: Vec<&[u8]> = self.before[self.before.len() - frame.window..] - .iter() - .map(Vec::as_slice) - .collect(); - compress_pieces(&frame.bytes, &frame.pieces, &window, steps, true)? - } - }; - out.extend_from_slice(&archive); + out.push(match frame.reference { + Some(label) => Planned::Done { + length: frame.bytes.len() as u64, + archive: reference_or_raw(&frame.bytes, &label, &frame.pieces), + }, + None => Planned::Code { + bytes: frame.bytes.clone(), + pieces: frame.pieces, + window: self.before[self.before.len() - frame.window..].to_vec(), + }, + }); self.before.push(frame.bytes); if self.before.len() > archive::WINDOW { self.before.remove(0); } } - Ok(out) + out + } +} + +fn code_all(planned: Vec, steps: u64) -> Result, Error> { + let mut out = Vec::new(); + for frame in planned { + out.extend_from_slice(&frame.code(steps)?); + } + Ok(out) +} + +/// A frame [`FrameEncoder::plan`] has cut. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Planned { + /// A repeat of an earlier run, already its archive. + Done { + /// The frame's input length. + length: u64, + /// Its archive. + archive: Vec, + }, + /// A frame still to code after the frames of its window (oldest first). + Code { + /// The frame's input bytes. + bytes: Vec, + /// The pieces of objects it holds. + pieces: Vec, + /// The frames it reads as history. + window: Vec>, + }, +} + +impl Planned { + /// The frame's archive, the one [`FrameEncoder::push`] writes for it. + pub fn code(&self, steps: u64) -> Result, Error> { + match self { + Planned::Done { archive, .. } => Ok(archive.clone()), + Planned::Code { + bytes, + pieces, + window, + } => { + let window: Vec<&[u8]> = window.iter().map(Vec::as_slice).collect(); + compress_pieces(bytes, pieces, &window, steps, true) + } + } } } diff --git a/crates/uorc/src/ncm.rs b/crates/uorc/src/ncm.rs index 1c0b2cf8..3db2581e 100644 --- a/crates/uorc/src/ncm.rs +++ b/crates/uorc/src/ncm.rs @@ -350,6 +350,81 @@ fn adapt(p: &mut u16, n: &mut u8, y: bool, rate: &[i32; 128]) { #[repr(C, align(64))] struct Bucket([u32; 16]); +/// A model table. In WebAssembly, whose memory never shrinks, a dropped +/// table is kept for the next frame's model of the same size: allocating +/// ~100 MiB of fresh tables per frame left the allocator's free space in +/// pieces, and the memory grew with the number of frames. +struct Table(Vec); + +trait Spare: Copy + 'static { + fn spares() -> &'static std::thread::LocalKey>>>; +} + +thread_local! { + static SPARE_BUCKETS: std::cell::RefCell>> = const { std::cell::RefCell::new(Vec::new()) }; + static SPARE_WORDS: std::cell::RefCell>> = const { std::cell::RefCell::new(Vec::new()) }; +} + +impl Spare for Bucket { + fn spares() -> &'static std::thread::LocalKey>>> { + &SPARE_BUCKETS + } +} + +impl Spare for u32 { + fn spares() -> &'static std::thread::LocalKey>>> { + &SPARE_WORDS + } +} + +impl Table { + /// `len` entries of `value`. + fn new(len: usize, value: T) -> Table { + if cfg!(target_family = "wasm") && len > 0 { + let kept = T::spares().with(|s| { + let mut s = s.borrow_mut(); + let at = s.iter().position(|t| t.len() == len)?; + Some(s.swap_remove(at)) + }); + if let Some(mut t) = kept { + t.fill(value); + return Table(t); + } + } + Table(vec![value; len]) + } +} + +impl Drop for Table { + fn drop(&mut self) { + if cfg!(target_family = "wasm") && !self.0.is_empty() { + let t = std::mem::take(&mut self.0); + T::spares().with(|s| { + let mut s = s.borrow_mut(); + // a model holds one bucket table and two word tables + if s.len() < 2 { + s.push(t); + } + }); + } + } +} + +impl std::ops::Deref for Table { + type Target = [T]; + #[inline(always)] + fn deref(&self) -> &[T] { + &self.0 + } +} + +impl std::ops::DerefMut for Table { + #[inline(always)] + fn deref_mut(&mut self) -> &mut [T] { + &mut self.0 + } +} + const SLOT_FRESH: u32 = 32768; /// [`adapt`] on a packed slot (probability low, count above it). @@ -381,7 +456,7 @@ struct Model { /// the low 16 bits and its count in the next 8, so a bucket is one 64-byte /// line and a lookup touches one line (slot 0's count is the bucket's /// check byte). - sl: Vec, + sl: Table, hist: Vec, /// History bytes ahead of the coded ones in `hist` (a window frame's /// preceding outputs): the match model reaches into them. @@ -393,9 +468,9 @@ struct Model { word1: u32, c0: u32, bit: u32, - mtab: Vec, + mtab: Table, profile: u32, - m2tab: Vec, + m2tab: Table, m2ptr: u32, m2len: u32, m2exp: u32, @@ -452,7 +527,10 @@ impl Model { /// The model for `n` bytes coded after `history` (a window frame's /// preceding outputs): the match model starts holding every history /// position, and every other model starts fresh. - fn new(n: u32, history: &[u8], profile: u32) -> Model { + /// A model for `n` bytes coded after `history`, the concatenation of its + /// pieces (a window decodes without first joining its frames). + fn new(n: u32, history: &[&[u8]], profile: u32) -> Model { + let before: usize = history.iter().map(|h| h.len()).sum(); let (mask, mb) = Model::sized(n); let mut act = [0; NC]; let mut na = 0; @@ -480,9 +558,9 @@ impl Model { stride, mask, mb, - sl: vec![Bucket([SLOT_FRESH; 16]); (mask as usize + 1) / 16], - hist: Vec::with_capacity(history.len() + n as usize), - hoff: history.len() as u32, + sl: Table::new((mask as usize + 1) / 16, Bucket([SLOT_FRESH; 16])), + hist: Vec::with_capacity(before + n as usize), + hoff: before as u32, pos: 0, c4: 0, c8: 0, @@ -490,9 +568,9 @@ impl Model { word1: 0, c0: 1, bit: 0, - mtab: vec![0; 1 << mb], + mtab: Table::new(1 << mb, 0), profile, - m2tab: if short { vec![0; 1 << mb] } else { Vec::new() }, + m2tab: Table::new(if short { 1 << mb } else { 0 }, 0), m2ptr: 0, m2len: 0, m2exp: 0, @@ -539,7 +617,9 @@ impl Model { a1: Apm::new(256 * 9), a2: Apm::new(1 << APM2_BITS), }; - m.hist.extend_from_slice(history); + for piece in history { + m.hist.extend_from_slice(piece); + } for p in 7..m.hoff { let k = m.match_key(p); m.mtab[k] = p; @@ -1242,7 +1322,7 @@ mod x86 { /// The decoder loop compiled for AVX2. #[target_feature(enable = "avx2")] pub(super) fn decoder( - history: &[u8], + history: &[&[u8]], payload: &[u8], limit: u64, canonical: bool, @@ -1504,6 +1584,32 @@ mod wasm { } } +/// Input bytes between two calls of the progress hook: 16 KiB, about a +/// tenth of a second of coding in the browser, so the page's bar moves +/// within a frame and soon after a file starts. +pub const TICK: usize = 1 << 14; + +/// A progress hook: bytes done, and the length of the pass. +pub type Progress = fn(u64, u64); + +std::thread_local! { + static PROGRESS: std::cell::Cell> = const { std::cell::Cell::new(None) }; +} + +/// Set (or clear) this thread's progress hook: every coding or decoding +/// pass over a frame calls it with the bytes done and the pass's length, at +/// the start and once per [`TICK`] bytes. It never changes a payload. +pub fn set_progress(hook: Option) { + PROGRESS.with(|p| p.set(hook)); +} + +#[inline(never)] +fn tick(done: usize, total: usize) { + if let Some(hook) = PROGRESS.with(std::cell::Cell::get) { + hook(done as u64, total as u64); + } +} + /// Encode `data` as an ncm payload, or `None` when the coder output would pass /// `2 * len + 64` bytes (the model abandons the encoding there) or the input /// does not fit the 4-byte length. @@ -1585,7 +1691,7 @@ fn encoder( stop: usize, ) -> Option<(Vec, u32)> { let n = u32::try_from(data.len()).ok()?; - let mut m = Model::new(n, history, profile); + let mut m = Model::new(n, &[history], profile); if learn { m.gain = Some(Box::new([0; NC])); } @@ -1607,7 +1713,10 @@ fn encode_bits( let mut out = Vec::with_capacity(data.len() / 2 + 16); out.extend_from_slice(&(data.len() as u32).to_be_bytes()); let (mut x1, mut x2) = (0u32, u32::MAX); - for &byte in data { + for (i, &byte) in data.iter().enumerate() { + if i % TICK == 0 { + tick(i, data.len()); + } for k in (0..8).rev() { let y = (byte >> k) & 1 == 1; let p = m.predict::(); @@ -1688,6 +1797,20 @@ pub fn decode_canonical_profile( run_profile(history, payload, n, true, profile) } +/// [`decode_canonical_profile`] after the frames of a window, without +/// joining them first: the model holds the only copy of the history. +pub fn decode_canonical_window( + window: &[&[u8]], + payload: &[u8], + n: u64, + profile: u32, +) -> Option> { + if declared_length(payload).map(u64::from) != Some(n) { + return None; + } + run_pieces(window, payload, n, true, profile) +} + fn run_decoder(history: &[u8], payload: &[u8], limit: u64, canonical: bool) -> Option> { run_profile(history, payload, limit, canonical, V03) } @@ -1700,6 +1823,17 @@ pub(crate) fn run_profile( limit: u64, canonical: bool, profile: u32, +) -> Option> { + run_pieces(&[history], payload, limit, canonical, profile) +} + +/// [`run_profile`] after a history given as pieces. +fn run_pieces( + history: &[&[u8]], + payload: &[u8], + limit: u64, + canonical: bool, + profile: u32, ) -> Option> { if !valid_profile(profile) { return None; @@ -1714,7 +1848,7 @@ pub fn decode_profile(history: &[u8], payload: &[u8], limit: u64, profile: u32) #[inline(always)] fn decoder( - history: &[u8], + history: &[&[u8]], payload: &[u8], limit: u64, canonical: bool, @@ -1725,10 +1859,11 @@ fn decoder( return None; } let m = Model::new(n, history, profile); + let before = m.hoff as usize; if m.stride == NINP_NARROW { - decode_bits::(m, history.len(), payload, canonical) + decode_bits::(m, before, payload, canonical) } else { - decode_bits::(m, history.len(), payload, canonical) + decode_bits::(m, before, payload, canonical) } } @@ -1750,7 +1885,10 @@ fn decode_bits( // coder bytes the encoder has emitted so far let mut emitted = 0usize; let (mut x1, mut x2) = (0u32, u32::MAX); - for _ in 0..n { + for i in 0..n as usize { + if i % TICK == 0 { + tick(i, n as usize); + } for _ in 0..8 { let p = m.predict::(); let xm = x1 + ((u64::from(x2 - x1) * u64::from(p)) >> 12) as u32; @@ -1784,6 +1922,9 @@ fn decode_bits( } let mut out = m.hist; out.drain(..history); + // Give back the history's share of the buffer: the output outlives the + // model (the next frame's window holds it). + out.shrink_to_fit(); Some(out) } diff --git a/docs/verification/pages-client.md b/docs/verification/pages-client.md index cb7c1cef..0ff180ee 100644 --- a/docs/verification/pages-client.md +++ b/docs/verification/pages-client.md @@ -34,7 +34,7 @@ reports), the same schedule `FrameDecoder` runs. - `planted_model_defects_are_rejected_uc_web_01`: eleven planted model defects, each rejected by its check. - `scripts/page-acceptance.mjs` (CI: `pages-client.yml`; local: `just pages`): - walks the three journeys in fresh pages, runs the 17 acceptance cases, records + walks the three journeys in fresh pages, runs the 18 acceptance cases, records every state change and checks every control against `enabledIn` at each one, and holds the gated measures. @@ -64,18 +64,57 @@ Planting the v0.3 wasm first showed a page defect: an async trap in the worker raised no `error` event, so the page stayed in `running`. The worker now reports any non-uorc failure as `stopped` and closes, and the page starts a new one. -## Measured (4 vCPU cloud VM, headless Chromium, SIMD128 build) +## The page uses the visitor's cores + +The page's worker plans each frame with the library (`FrameEncoder::plan`), and +a pool of workers codes the frame's two portfolio strategies side by side, then +the library's own rule (`uorc::choose`) keeps one. The pool holds one worker per +core, at most 16 and two per GiB of device memory; every worker shares one +compiled module. Bytes equal the library's: the acceptance compares every page +archive against `uorc::compress` (differingArchives 0), and the wasm test +`planned_frames_coded_by_strategy_are_the_library_stream` checks the same path +in Node. + +Progress moves within a frame: ncm reports every 16 KiB it codes, so the bar +never stands still for long on an 8 MiB frame (measure `progressGap`). The gate +found two stalls of over 2 s, both fixed: the first file waited behind the pool +workers' start (the page's worker now codes alone until two have loaded), and a +short last frame, coded when the stream ends, reported nothing. + +Same 10 MiB of generated prose, 4 vCPU, headless Chromium: + +| | Compress | Decompress | +| --- | --- | --- | +| One worker (main before the pool) | 168 KB/s (62.3 s) | 563 KB/s (18.6 s) | +| Worker pool | 369 KB/s (28.4 s) | 561 KB/s (18.7 s) | + +Decoding this input stays on one worker, as it does natively: every frame is a +window frame whose model reads the two frames before it. The archive is the +same 5,731 bytes in both rows. + +## Measured (4 vCPU cloud VM, headless Chromium, SIMD128 build, ncm v4) | Measure | Value | Gate | | --- | --- | --- | -| Longest page task while an operation runs | 0 ms (4 long tasks seen outside operations) | ≤ 200 ms | -| Cancel to ready | 112 ms | ≤ 3000 ms | -| Navigation to ready | 225 ms | ≤ 5000 ms | +| Cores the page may use without a pool worker | 0 | 0 | +| Longest time the progress bar stands still | 552 ms | ≤ 2000 ms | +| Longest page task while an operation runs | 0 ms | ≤ 200 ms | +| Cancel to ready | 155 ms | ≤ 3000 ms | +| Navigation to ready | 340 ms | ≤ 5000 ms | | Page archives differing from the library's | 0 | 0 | -| WebAssembly memory after a 3-frame (24 MiB) stream | 179 MiB | ≤ 256 MiB | +| WebAssembly memory, largest worker | 227 MiB | ≤ 256 MiB | | Controls without an accessible name | 0 | 0 | -| Compress, generated prose | 409 KB/s | measured | -| Decompress, generated prose | 439 KB/s | measured | +| Compress, the acceptance's 10 MiB of prose | 376 KB/s | measured | +| Decompress, the acceptance's 10 MiB of prose | 875 KB/s | measured | + +Decode memory is bounded. Measured in Node (same V8, SIMD128 build) on streams +of distinct 8 MiB frames: 226 MiB for 3 frames and 250 MiB for 6 or 12, flat once +the two-frame window is full (241, 257 and 257 MiB before). Two changes, same +bytes: ncm's model reads the window's frames in place instead of a joined copy, +and in WebAssembly, whose memory never shrinks, a model's tables (about 100 MiB +for an 8 MiB frame) are kept for the next frame's model instead of being +allocated again, which had left the allocator's free space in pieces. The 64 MiB +stack the exported reference code needs is part of every figure. Node (same V8) on 2 MB of repository text: 0.21 MB/s both ways without SIMD128, 0.29 MB/s with it, against 0.53 MB/s for one native core. Random input diff --git a/scripts/page-acceptance.mjs b/scripts/page-acceptance.mjs index 69178ca3..7a286fa7 100644 --- a/scripts/page-acceptance.mjs +++ b/scripts/page-acceptance.mjs @@ -37,6 +37,8 @@ const prose = Array.from({ length: 40000 }, () => { seed = (seed * 1103515245 + const file = (name, bytes) => { const p = path.join(dir, name); fs.mkdirSync(path.dirname(p), { recursive: true }); fs.writeFileSync(p, bytes); return p; }; const inputs = { text: file('text.txt', Buffer.from(prose)), + // 10 MiB of text: two frames, for the throughput measures. + large: file('large.txt', Buffer.from(prose.repeat(40)).subarray(0, 10 * 1048576)), other: file('other.bin', crypto.randomBytes(3000)), // Three identical 8 MiB frames: the second and third are reference frames. frames: file('frames.bin', Buffer.alloc(3 * 8 * 1048576)), @@ -183,10 +185,19 @@ const setFiles = async (id, files) => { const click = (selector) => evaluate(`document.querySelector(${JSON.stringify(selector)}).click()`); const tab = (op) => click(`#tab-${op}`); const text = (id) => evaluate(`document.getElementById(${JSON.stringify(id)}).textContent`); +// Run the chosen operation to its end, watching the progress bar: the +// longest time it stands still is the progressGap measure. async function runAndWait() { const t = Date.now(); await click('#run'); - await waitFor("document.body.dataset.state !== 'running'", 600000); + let value, moved = Date.now(); + for (;;) { + const [now, running, at] = [Date.now(), ...await evaluate("[document.body.dataset.state === 'running', document.getElementById('progress').value]")]; + if (!running) break; + if (at !== value) { value = at; moved = now; } else raise('progressGap', now - moved); + if (now - t > 600000) throw new Error('the operation did not end within 10 minutes'); + await sleep(100); + } return { state: await state(), ms: Date.now() - t }; } // The files the result offers: name and bytes. @@ -226,8 +237,16 @@ async function compressAndCompare(input, name) { const cases = { async 'compress-file'() { - const r = await compressAndCompare(inputs.text, 'text'); - measured.compressThroughput = Math.round(fs.statSync(inputs.text).size / r.ms); + await compressAndCompare(inputs.text, 'text'); + // A visitor's first file comes a few seconds after the page loads, once + // the pool has started. + await sleep(3000); + const r = await compressAndCompare(inputs.large, 'large'); + measured.compressThroughput = Math.round(fs.statSync(inputs.large).size / r.ms); + const workers = Number(await evaluate("document.querySelector('#result .memory').dataset.workers")); + const memory = await evaluate('navigator.deviceMemory'); + const may = Math.min(await evaluate('navigator.hardwareConcurrency'), 16, memory ? Math.floor(memory * 2) : 4); + raise('coresUnused', may < 2 ? 0 : Math.max(0, may - workers)); }, async 'compress-matches-library'() { await compressAndCompare(inputs.frames, 'frames'); @@ -241,7 +260,12 @@ const cases = { expect(r.state === 'done', `decompress: ${await text('status')}`); const [out] = await downloads(); expect(out.name === 'text.txt' && sha(out.bytes) === sha(fs.readFileSync(inputs.text)), `decompress restored ${out.name} with other bytes`); - measured.decompressThroughput = Math.round(fs.statSync(inputs.text).size / r.ms); + await setFiles('field-archive', [saved.large]); + const big = await runAndWait(); + expect(big.state === 'done', `decompress large: ${await text('status')}`); + const [restored] = await downloads(); + expect(restored.name === 'large.txt' && sha(restored.bytes) === sha(fs.readFileSync(inputs.large)), `decompress restored ${restored.name} with other bytes`); + measured.decompressThroughput = Math.round(fs.statSync(inputs.large).size / big.ms); }, async 'decompress-reference-frames'() { await need('compress-matches-library'); diff --git a/src/Uorc/Page.lex.tex b/src/Uorc/Page.lex.tex index 92959d45..26a712a0 100644 --- a/src/Uorc/Page.lex.tex +++ b/src/Uorc/Page.lex.tex @@ -780,7 +780,7 @@ name: boundedMemory, parameters: [], result: QualityRequirement, - body: (record QualityRequirement {characteristic := "performance efficiency", subcharacteristic := "resource utilization", text := "Compressing or decompressing one file holds a few frames in memory, not the whole file."}) + body: (record QualityRequirement {characteristic := "performance efficiency", subcharacteristic := "resource utilization", text := "Compressing or decompressing one file holds a few frames in memory, not the whole file. Decoding a stream of distinct 8 MiB frames in WebAssembly peaks at 226 MiB for three frames and at 250 MiB for six or twelve: the model reads the window's frames in place and its tables are kept for the next frame (241, 257 and 257 MiB before)."}) } definition{ @@ -794,7 +794,35 @@ name: browserThroughput, parameters: [], result: QualityRequirement, - body: (record QualityRequirement {characteristic := "performance efficiency", subcharacteristic := "time behaviour", text := "Compress and decompress throughput in the browser, measured and reported; the release sets no target."}) + body: (record QualityRequirement {characteristic := "performance efficiency", subcharacteristic := "time behaviour", text := "Compress and decompress throughput in the browser on 10 MiB of text, measured and reported. Measured on the same 10 MiB (4 vCPU, headless Chromium): one worker compressed it at 168 KB/s and decompressed it at 563 KB/s; with the worker pool the page compresses it at 369 KB/s and decompresses it at 561 KB/s. Decoding this input stays on one worker, as it does natively, because each of its frames is a window frame whose model reads the frames before it."}) +} + +definition{ + name: useTheCores, + parameters: [], + result: QualityRequirement, + body: (record QualityRequirement {characteristic := "performance efficiency", subcharacteristic := "resource utilization", text := "Compressing uses the visitor's cores: a pool of workers codes each frame's two scheduled strategies side by side, and frames side by side, and the archive is still the library's."}) +} + +definition{ + name: progressWithinFrame, + parameters: [], + result: QualityRequirement, + body: (record QualityRequirement {characteristic := "interaction capability", subcharacteristic := "self-descriptiveness", text := "Progress moves while a frame is coded, not only between frames, so a long operation never looks stalled."}) +} + +definition{ + name: coresUnused, + parameters: [], + result: QualityMeasure, + body: (record QualityMeasure {requirement := "Uorc.Page::useTheCores", quantity := "cores the page may use (at most 16, and two per GiB of device memory) without a pool worker", unit := "cores", threshold := 0, disposition := "gate"}) +} + +definition{ + name: progressGap, + parameters: [], + result: QualityMeasure, + body: (record QualityMeasure {requirement := "Uorc.Page::progressWithinFrame", quantity := "longest time the progress bar stands still while an operation runs", unit := "ms", threshold := 2000, disposition := "gate"}) } definition{ @@ -843,14 +871,14 @@ name: compressThroughput, parameters: [], result: QualityMeasure, - body: (record QualityMeasure {requirement := "Uorc.Page::browserThroughput", quantity := "compress throughput on text", unit := "KB/s", threshold := 0, disposition := "measured"}) + body: (record QualityMeasure {requirement := "Uorc.Page::browserThroughput", quantity := "compress throughput on 10 MiB of text, on every worker the page starts", unit := "KB/s", threshold := 0, disposition := "measured"}) } definition{ name: decompressThroughput, parameters: [], result: QualityMeasure, - body: (record QualityMeasure {requirement := "Uorc.Page::browserThroughput", quantity := "decompress throughput on text", unit := "KB/s", threshold := 0, disposition := "measured"}) + body: (record QualityMeasure {requirement := "Uorc.Page::browserThroughput", quantity := "decompress throughput on 10 MiB of text", unit := "KB/s", threshold := 0, disposition := "measured"}) } \end{semanticmodule} \end{lexlean} diff --git a/web/app.js b/web/app.js index 4266fc86..e75f1bdd 100644 --- a/web/app.js +++ b/web/app.js @@ -195,6 +195,7 @@ function showResult(result, nodes) { const memory = document.createElement("p"); memory.className = "memory"; memory.textContent = `${labels.memory}: ${fmt(Math.round(result.memory / 1048576))} MiB`; + memory.dataset.workers = String(result.workers ?? 0); $("result").replaceChildren(...nodes, memory); $("result").hidden = false; } diff --git a/web/index.html b/web/index.html index 568f8fc2..d8be1efa 100644 --- a/web/index.html +++ b/web/index.html @@ -123,7 +123,7 @@ - + diff --git a/web/uorc.js b/web/uorc.js index 16be7302..1e954e0c 100644 --- a/web/uorc.js +++ b/web/uorc.js @@ -61,10 +61,64 @@ function unpackEntries(bytes) { return entries; } -export async function load(url) { - const response = await fetch(url); - const { instance } = await WebAssembly.instantiateStreaming(response, {}); +// A coding request for one planned frame (see code_frame in uorc-wasm): +// u32 pieceCount, u64 piece*, u32 window, u64 windowLength*, then the window +// frames and the frame. +function frameRequest(pieces, window, frame) { + const size = 8 + 8 * pieces.length + 8 * window.length + window.reduce((n, w) => n + w.length, 0) + frame.length; + const out = new Uint8Array(size); + const view = new DataView(out.buffer); + let at = 0; + view.setUint32(at, pieces.length, true); at += 4; + for (const p of pieces) { view.setBigUint64(at, p, true); at += 8; } + view.setUint32(at, window.length, true); at += 4; + for (const w of window) { view.setBigUint64(at, BigInt(w.length), true); at += 8; } + for (const w of [...window, frame]) { out.set(w, at); at += w.length; } + return out; +} + +// The choice between a frame's two strategy archives (see choose_frame). +function chooseRequest(pieces, frame, first, second) { + const out = new Uint8Array(4 + 8 * pieces.length + 24 + frame.length + first.length + second.length); + const view = new DataView(out.buffer); + let at = 0; + view.setUint32(at, pieces.length, true); at += 4; + for (const p of pieces) { view.setBigUint64(at, p, true); at += 8; } + for (const part of [frame, first, second]) { + view.setBigUint64(at, BigInt(part.length), true); at += 8; + out.set(part, at); at += part.length; + } + return out; +} + +// A frame's coding runs a few passes (the scheduled strategies, a learned +// re-encode, and in small schedules the v0.3 entry), how many is known only +// as they run. After x passes (whole and part) the share done is +// 1 - (passes / (passes + 1))^x: under 1/passes per pass, then smaller +// steps, so the bar keeps moving through any extra pass and never moves back. +export function frameShare(passes = 3) { + let begun = 0, last = 0, best = 0; + const left = passes / (passes + 1); + return (done, total) => { + if (done < last) begun++; + last = done; + best = Math.max(best, Math.min(0.99, 1 - left ** (begun + done / Math.max(total, 1)))); + return best; + }; +} + +// `source` is the module's URL, or a WebAssembly.Module already compiled (a +// pool worker receives the page worker's). +export async function load(source) { + // The module reports each coding pass through uorc.progress (bytes done of + // the pass's length); `onPass` routes it to the operation running. + let onPass = () => {}; + const imports = { uorc: { progress: (done, total) => onPass(done, total) } }; + const { module, instance } = source instanceof WebAssembly.Module + ? { module: source, instance: await WebAssembly.instantiate(source, imports) } + : await WebAssembly.instantiateStreaming(fetch(source), imports); const x = instance.exports; + x.report_progress?.(1); const put = (alloc, bytes) => { const at = alloc(bytes.length); // may grow memory, so read the buffer after new Uint8Array(x.memory.buffer, at, bytes.length).set(bytes); @@ -101,6 +155,85 @@ export async function load(url) { at += length; } }; + // Code the stream the encoder has been started on (window_reset, or + // tree_start for a tree): here, a frame at a time, or with a pool of + // workers (see `workers`) on several cores, each frame's two scheduled + // strategies on two of them, frames in order. Either way the bytes are the + // library's. + const codeStream = async (blob, steps, progress, pool) => { + const total = blob.size; + // Until two pool workers have loaded, the page's worker codes alone, so + // the first file does not wait behind their start. + if (!pool || pool.ready < 2) { + const parts = []; + // A whole frame is coded as it is given; a shorter last one is coded + // when the stream ends. + const coding = (at, length) => { + const share = frameShare(); + onPass = (done, of) => progress(at + Math.floor(length * share(done, of)), total); + }; + for (let at = 0; at < total; at += frame) { + coding(at, Math.min(frame, total - at)); + parts.push(call(await bytesOf(blob.slice(at, at + frame)), () => x.compress_frame(BigInt(steps)))); + } + coding(total - (total % frame), total % frame); + parts.push(call(new Uint8Array(0), () => x.compress_end(BigInt(steps)))); + onPass = () => {}; + progress(total, total); + return new Blob(parts); + } + // Plan here, code on the pool. A frame's window is the frames before it. + const archives = []; + const recent = []; + const shares = new Map(); + let finished = 0; + const report = () => progress(Math.min(total, finished + [...shares.values()].reduce((n, v) => n + v, 0)), total); + const settle = (index, length, promise) => { + archives[index] = promise.then((archive) => { shares.delete(index); finished += length; report(); return archive; }); + }; + const job = (f, which) => ({ which, steps, pieces: f.pieces, window: f.window.map((r) => blob.slice(r.start, r.start + r.length)), frame: blob.slice(f.start, f.start + f.length) }); + const split = (f) => { + const parts = [0, 0]; + const both = [0, 1].map((which) => pool.code(job(f, which), (share) => { parts[which] = share; shares.set(f.index, (f.length * (parts[0] + parts[1])) / 2); report(); })); + settle(f.index, f.length, Promise.all(both).then(async ([first, second]) => + call(chooseRequest(f.pieces, await bytesOf(blob.slice(f.start, f.start + f.length)), first, second), () => x.choose_frame()))); + }; + let index = 0; + let position = 0; + const plan = (records) => { + const view = new DataView(records.buffer, records.byteOffset, records.byteLength); + let p = 0; + const n = view.getUint32(p, true); p += 4; + for (let i = 0; i < n; i++) { + const kind = records[p++]; + const length = Number(view.getBigUint64(p, true)); p += 8; + const f = { index: index++, start: position, length }; + if (kind === 0) { + const size = Number(view.getBigUint64(p, true)); p += 8; + archives[f.index] = Promise.resolve(records.slice(p, p + size)); p += size; + finished += length; + } else { + const count = view.getUint32(p, true); p += 4; + f.pieces = []; + for (let k = 0; k < count; k++) { f.pieces.push(view.getBigUint64(p, true)); p += 8; } + const window = view.getUint32(p, true); p += 4; + f.window = recent.slice(recent.length - window); + split(f); + } + recent.push({ start: position, length }); + if (recent.length > 2) recent.shift(); + position += length; + } + }; + for (let at = 0; at < total; at += frame) { + plan(call(await bytesOf(blob.slice(at, at + frame)), () => x.plan_frame())); + report(); + } + plan(call(new Uint8Array(0), () => x.plan_end())); + const parts = await Promise.all(archives); + progress(total, total); + return new Blob(parts); + }; return { compress: (bytes, steps = 0n) => call(bytes, () => x.compress(BigInt(steps))), decompress: (archive) => call(archive, () => x.decompress()), @@ -110,27 +243,21 @@ export async function load(url) { // Stream a File or Blob of any size through frames, a few in wasm memory // at a time; the result is a Blob of the concatenated archives, the same // bytes the native client writes. - async compressBlob(blob, steps = 0n, progress = () => {}) { - const parts = []; + async compressBlob(blob, steps = 0n, progress = () => {}, pool) { x.window_reset(); - for (let at = 0; at < blob.size; at += frame) { - const piece = await bytesOf(blob.slice(at, at + frame)); - parts.push(call(piece, () => x.compress_frame(BigInt(steps)))); - progress(Math.min(at + frame, blob.size), blob.size); - } - parts.push(call(new Uint8Array(0), () => x.compress_end(BigInt(steps)))); - progress(blob.size, blob.size); - return new Blob(parts); + return codeStream(blob, steps, progress, pool); }, async decompressBlob(blob, progress = () => {}) { const parts = []; await begin(blob); for (let at = 0; at < blob.size || parts.length === 0;) { const length = await archiveLength(blob, at); + onPass = (done, total) => progress(at + Math.floor((length * done) / Math.max(total, 1)), blob.size); parts.push(call(await bytesOf(blob.slice(at, at + length)), () => x.decompress_frame())); at += length; progress(at, blob.size); } + onPass = () => {}; return new Blob(parts); }, // Decode in memory and check; with an original, also compare. Returns one @@ -145,23 +272,10 @@ export async function load(url) { // Stream a file tree of any size: items are [{ path, kind, blob }] (no // blob for a directory). Each file is read a frame at a time; the result // is a Blob of the same bytes compressFiles writes. - async compressTreeBlob(items, steps = 0n, progress = () => {}) { + async compressTreeBlob(items, steps = 0n, progress = () => {}, pool) { x.window_reset(); const manifest = call(packListing(items), () => x.tree_start()); - const parts = [call(manifest, () => x.compress_frame(BigInt(steps)))]; - const total = items.reduce((n, e) => n + (e.blob?.size ?? 0), 0); - let done = 0; - for (const { blob } of items) { - for (let at = 0; blob && at < blob.size; at += frame) { - const piece = await bytesOf(blob.slice(at, at + frame)); - parts.push(call(piece, () => x.compress_frame(BigInt(steps)))); - done += piece.length; - progress(done, total); - } - } - parts.push(call(new Uint8Array(0), () => x.compress_end(BigInt(steps)))); - progress(total, total); - return new Blob(parts); + return codeStream(new Blob([manifest, ...items.map((e) => e.blob ?? new Blob([]))]), steps, progress, pool); }, // Restore a tree archive a frame at a time: [{ path, kind, blob }], with // no blob for a directory. A one-object archive is not a tree. @@ -200,6 +314,15 @@ export async function load(url) { const [info] = json(call(await bytesOf(blob.slice(0, length)), () => x.inspect())); return info.objects; }, + // One planned frame's archive (which -1), or one of its two strategies' + // (0 or 1; empty when it cannot beat the raw archive), reporting each + // pass to `pass(done, total)`: what a pool worker runs. + codeFrame(request, which, steps = 0n, pass = () => {}) { + onPass = pass; + try { return call(request, () => x.code_frame(which, BigInt(steps))); } finally { onPass = () => {}; } + }, + // The compiled module, for other workers to instantiate without a fetch. + module, // Bytes of WebAssembly memory the client holds now. memoryBytes: () => x.memory.buffer.byteLength, }; @@ -209,6 +332,8 @@ export async function load(url) { // progress, then the result or the failure's code. The page ends a running // operation by terminating the worker. const inWorker = typeof WorkerGlobalScope !== "undefined" && self instanceof WorkerGlobalScope; +// A pool worker codes for the page's worker and starts no pool of its own. +const inPool = new URL(import.meta.url).search === "?pool"; const EXAMPLES = { text: (n, seed) => { const t = new TextEncoder().encode(seed); return Uint8Array.from({ length: n }, (_, i) => t[i % t.length]); }, @@ -221,19 +346,39 @@ const EXAMPLES = { async function serve() { let uorc; try { - uorc = await load("./uorc_bg.wasm"); + // A pool worker instantiates the module its page worker sends first. + const sent = () => new Promise((resolve) => { self.onmessage = ({ data }) => resolve(data.module); }); + uorc = inPool ? await load(await sent()) : await load("./uorc_bg.wasm"); } catch (error) { self.postMessage({ type: "loadFailed", message: String(error?.message ?? error) }); return; } const bytesOf = async (file) => new Uint8Array(await file.arrayBuffer()); const progress = (done, total) => self.postMessage({ type: "progress", done, total }); + // Requests from the worker that started this one, when it is a pool worker: + // code one planned frame or strategy (see `pool`). + const helped = async ({ id, which, steps, pieces, window, frame }) => { + try { + const parts = await Promise.all([...window, frame].map(bytesOf)); + const share = frameShare(which < 0 ? 3 : which === 0 ? 2 : 1); + const archive = uorc.codeFrame(frameRequest(pieces, parts.slice(0, -1), parts.at(-1)), which, steps, + (done, total) => self.postMessage({ id, type: "share", share: share(done, total) })); + self.postMessage({ id, type: "coded", archive, memory: uorc.memoryBytes() }, [archive.buffer]); + } catch (error) { + self.postMessage({ id, type: "failed", code: error?.code ?? "stopped", message: String(error?.message ?? error) }); + if (!error?.code) self.close(); + } + }; + // A pool worker codes a little text once, so that its first frame runs on + // optimized code rather than the engine's first tier. + if (inPool) uorc.compress(EXAMPLES.text(1 << 16, "uorc pool worker warm-up ")); + const pool = inPool ? { size: 0, memory: 0, ready: 0, warm() {} } : workers(uorc.module); const run = { async compress({ files }) { const tree = files.length > 1 || files.some((f) => f.webkitRelativePath); if (!tree) { const [file] = files; - const archive = await uorc.compressBlob(file, 0n, progress); + const archive = await uorc.compressBlob(file, 0n, progress, pool); return { kind: "archive", input: file.size, files: [{ path: `${file.name}.uorc`, blob: archive }] }; } const dirs = new Set(); @@ -246,7 +391,7 @@ async function serve() { const total = files.reduce((n, f) => n + f.size, 0); const all = [...[...dirs].sort().map((path) => ({ path, kind: "directory" })), ...entries]; const root = files[0].webkitRelativePath ? files[0].webkitRelativePath.split("/")[0] : "files"; - const archive = await uorc.compressTreeBlob(all, 0n, progress); + const archive = await uorc.compressTreeBlob(all, 0n, progress, pool); return { kind: "archive", input: total, count: entries.length, files: [{ path: `${root}.uorc`, blob: archive }] }; }, async decompress({ archive }) { @@ -276,9 +421,10 @@ async function serve() { }, }; self.onmessage = async ({ data }) => { + if (data.id !== undefined) return helped(data); try { const result = await run[data.op](data); - self.postMessage({ type: "done", result: { ...result, memory: uorc.memoryBytes() } }); + self.postMessage({ type: "done", result: { ...result, memory: Math.max(uorc.memoryBytes(), pool.memory), workers: pool.size } }); } catch (error) { // Anything but a uorc failure (a trap, exhausted memory) leaves the // module unusable: report the stop and let the page start a new worker. @@ -287,6 +433,65 @@ async function serve() { } }; self.postMessage({ type: "ready" }); + pool.warm(); +} + +// The page worker's pool: one more uorc worker per core it may use, started +// once the page's worker is ready. Each holds its own module (a few frames of memory), so the +// count is also bounded by the device memory the browser reports, at about +// 512 MiB per worker. With fewer than two cores there is no pool. +function workers(module) { + const memory = self.navigator?.deviceMemory; + const size = Math.max(0, Math.min(self.navigator?.hardwareConcurrency ?? 1, 16, memory ? Math.floor(memory * 2) : 4)); + const idle = []; + const waiting = []; + const jobs = new Map(); + let started = false; + let next = 0; + const state = { size: size < 2 ? 0 : size, memory: 0, ready: 0 }; + const start = () => { + started = true; + for (let i = 0; i < state.size; i++) { + const w = new Worker(new URL("uorc.js?pool", import.meta.url), { type: "module" }); + w.postMessage({ module }); + w.onmessage = ({ data }) => { + if (data.type === "ready") { state.ready++; idle.push(w); pump(); return; } + if (data.type === "loadFailed") { fail(w, Object.assign(new Error(data.message), { code: undefined })); return; } + const job = jobs.get(data.id); + if (!job) return; + if (data.type === "share") job.onShare(data.share); + else { + jobs.delete(data.id); + if (data.type === "coded") { state.memory = Math.max(state.memory, data.memory); job.resolve(data.archive); } + else job.reject(Object.assign(new Error(data.message), data.code === "stopped" ? {} : { code: data.code })); + idle.push(w); + pump(); + } + }; + w.onerror = (event) => { event.preventDefault(); fail(w, new Error(event.message || "a pool worker stopped")); }; + } + }; + const fail = (w, error) => { + for (const [id, job] of jobs) if (job.worker === w) { jobs.delete(id); job.reject(error); } + while (waiting.length) waiting.shift().reject(error); + }; + const pump = () => { + while (idle.length && waiting.length) { + const w = idle.shift(); + const job = waiting.shift(); + job.worker = w; + jobs.set(job.id, job); + w.postMessage({ id: job.id, ...job.request }); + } + }; + // Started with the page's worker, so the first operation finds them loaded. + state.warm = () => { if (!started && state.size) start(); }; + state.code = (request, onShare) => new Promise((resolve, reject) => { + if (!started) start(); + waiting.push({ id: next++, request, onShare, resolve, reject }); + pump(); + }); + return state; } if (inWorker) serve();