diff --git a/crates/eps/src/ai_editing/tests.rs b/crates/eps/src/ai_editing/tests.rs index f8e39a4048..3828bc3961 100644 --- a/crates/eps/src/ai_editing/tests.rs +++ b/crates/eps/src/ai_editing/tests.rs @@ -3811,6 +3811,165 @@ fn images_that_cant_be_decoded_are_mid_gray_boxes() { assert_eq!(paint(n.appearance.items[0].paint()), paint(&solid(Color::gray(0.5)))); } +/// A `w` × `h` RGB image whose pixels follow from `seed`, no two of its pixels gray. +fn seeded_png(seed: u32, (w, h): (u32, u32)) -> Vec { + let px = |x: u32, y: u32| { + let v = (x.wrapping_mul(73_856_093) ^ y.wrapping_mul(19_349_663) ^ seed.wrapping_mul(83_492_791)).to_le_bytes(); + image::Rgb([v[0], v[1] | 1, v[2] & 0xfe]) + }; + png_of(image::DynamicImage::ImageRgb8(image::RgbImage::from_fn(w, h, px))) +} + +/// A document placing `blobs` in turn on one layer, each `times` times: the first, the second and +/// so on, then the first again. Each blob is kept under `keys`' key at its index. +fn images_in_turn(blobs: &[ImageBlob], keys: &[&str], times: usize) -> Document { + let mut doc = Document::new(200.0, 100.0); + let layer = doc.layers[0].id; + for (blob, key) in blobs.iter().zip(keys) { + doc.images.insert((*key).to_string(), blob.clone()); + } + for i in 0..times * blobs.len() { + let key = keys[i % blobs.len()].to_string(); + let xf = Affine::translate((f64::from(u32::try_from(i).unwrap()), 10.0)) * Affine::scale(2.0); + let im = ImageObject { key, width: 4, height: 4, xf, link: None, placement: Default::default(), pixels: None }; + let node = Node::new(doc.alloc_id(), NodeKind::Image(Box::new(im))); + doc.insert(Some(layer), usize::MAX, node).unwrap(); + } + doc +} + +/// `doc` written keeping the pixels of each image read (`last_only` false) or of the image read +/// last alone, and how many times an image's pixels were read. +fn export_reading(doc: &Document, last_only: bool) -> (AiEditingOutput, usize) { + writer::LAST_SAMPLES_ONLY.with(|l| l.set(last_only)); + writer::SAMPLE_READS.with(|n| n.set(0)); + let out = export_ai_editing(doc, &opts()); + writer::LAST_SAMPLES_ONLY.with(|l| l.set(false)); + (out.unwrap(), writer::SAMPLE_READS.with(std::cell::Cell::get)) +} + +/// `doc` written both ways ([`export_reading`]), the files and warnings compared whole, and the +/// reads of each way. +fn reads_for_the_same_file(doc: &Document) -> (usize, usize) { + let (kept, kept_reads) = export_reading(doc, false); + let (last, last_reads) = export_reading(doc, true); + assert!(kept.program == last.program, "the files differ"); + assert_eq!(kept.header, last.header); + assert_eq!(kept.warnings, last.warnings); + (kept_reads, last_reads) +} + +/// Two images placed one after the other 25 times each are each read once in a save, and the file +/// is the same as when only the image read last is kept. +#[test] +fn images_placed_in_turn_are_read_once_a_save() { + // GUARDS: each image was read again whenever the other was placed between its copies. + let blobs = [ImageBlob::png(seeded_png(1, (4, 4))), ImageBlob::png(seeded_png(2, (4, 4)))]; + let doc = images_in_turn(&blobs, &["a", "b"], 25); + assert_eq!(reads_for_the_same_file(&doc), (2, 50)); + let out = export_ai_editing(&doc, &opts()).unwrap(); + assert_eq!(String::from_utf8_lossy(&out.program).matches("%AI5_BeginRaster").count(), 50); +} + +/// A document of one image placed many times reads it once, and one without images reads none. +#[test] +fn one_image_is_read_once_and_no_image_never() { + let doc = images_in_turn(&[ImageBlob::png(seeded_png(3, (4, 4)))], &["a"], 10); + assert_eq!(reads_for_the_same_file(&doc), (1, 1)); + assert_eq!(reads_for_the_same_file(&Document::new(200.0, 100.0)), (0, 0)); +} + +/// An image that can't be read, placed between copies of another, is read once too, and each copy +/// is still its gray outline with the same warning. +#[test] +fn an_image_that_cant_be_read_is_read_once_a_save() { + // GUARDS: an image that can't be read was read again whenever another image came between. + let blobs = [ImageBlob::png(b"not an image".to_vec()), ImageBlob::png(seeded_png(4, (4, 4)))]; + let doc = images_in_turn(&blobs, &["bad", "good"], 3); + assert_eq!(reads_for_the_same_file(&doc), (2, 6)); + let out = export_ai_editing(&doc, &opts()).unwrap(); + assert_eq!(out.warnings, [writer::IMAGES]); + assert_eq!(String::from_utf8_lossy(&out.program).matches("%AI5_BeginRaster").count(), 3); +} + +/// The same data under two MIME types is two images: each is read once, apart from the other. +#[test] +fn the_same_data_of_another_type_is_read_apart() { + let png = ImageBlob::png(seeded_png(5, (4, 4))); + let other = ImageBlob { mime: "image/x-other".into(), ..png.clone() }; + assert!(Arc::ptr_eq(&png.bytes, &other.bytes)); + let doc = images_in_turn(&[png, other], &["png", "other"], 3); + assert_eq!(reads_for_the_same_file(&doc), (2, 6)); +} + +/// Past the limits on the samples kept (bytes and images), an image is kept beside them in place +/// of the one before it, and the others are read again where they come: what is kept stays within +/// both limits, and the file is the same. +#[test] +fn images_past_the_limits_are_read_again_and_written_the_same() { + // Images of 48, 75 and 108 bytes of samples, then two of 48: 123 bytes and two images fit. + let sizes = [(4, 4), (5, 5), (6, 6), (4, 4), (4, 4)]; + let blobs: Vec<_> = (0u32..).zip(sizes).map(|(i, s)| ImageBlob::png(seeded_png(10 + i, s))).collect(); + let doc = images_in_turn(&blobs, &["a", "b", "c", "d", "e"], 3); + for (limits, kept_most, reads) in [((130, 8), (123, 2), 2 + 3 * 3), ((10_000, 3), (48 + 75 + 108, 3), 3 + 2 * 3)] { + writer::SAMPLE_LIMITS.with(|l| l.set(limits)); + writer::SAMPLES_KEPT.with(|k| k.set((0, 0, false))); + let (kept_reads, last_reads) = reads_for_the_same_file(&doc); + let (bytes, images, beside) = writer::SAMPLES_KEPT.with(std::cell::Cell::get); + writer::SAMPLE_LIMITS.with(|l| l.set((256 << 20, 256))); + assert!(bytes <= limits.0 && images <= limits.1, "{limits:?}: kept {bytes} bytes of {images} images"); + assert_eq!((bytes, images, beside), (kept_most.0, kept_most.1, true), "{limits:?}"); + assert_eq!((kept_reads, last_reads), (reads, 15), "{limits:?}"); + } +} + +/// The time this thread has run, and how it was measured. +fn thread_time() -> (std::time::Duration, &'static str) { + if let Some(ns) = std::fs::read_to_string("/proc/thread-self/schedstat").ok().and_then(|s| s.split_whitespace().next()?.parse::().ok()) { + return (std::time::Duration::from_nanos(ns), "thread CPU time"); + } + static START: std::sync::OnceLock = std::sync::OnceLock::new(); + (START.get_or_init(std::time::Instant::now).elapsed(), "wall-clock time") +} + +/// The time to write two 2048 × 2048 images placed one after the other 25 times each, keeping the +/// pixels of each image read and of the image read last alone: the median of 7 runs of each, +/// alternating, with the files compared. Run with +/// `cargo test --release -p vectorcraft-eps --lib -- --ignored --nocapture images_placed_in_turn_are_written_quickly`. +#[test] +#[ignore = "timing"] +fn images_placed_in_turn_are_written_quickly() { + let blobs = [ImageBlob::png(seeded_png(1, (2048, 2048))), ImageBlob::png(seeded_png(2, (2048, 2048)))]; + let doc = images_in_turn(&blobs, &["a", "b"], 25); + let mut times = [vec![], vec![]]; + let mut files: [Option>; 2] = [None, None]; + let mut reads = [0, 0]; + let mut how = ""; + for _ in 0..7 { + for (k, last_only) in [false, true].into_iter().enumerate() { + let (start, h) = thread_time(); + let (out, n) = export_reading(&doc, last_only); + times[k].push(thread_time().0.saturating_sub(start)); + how = h; + reads[k] = n; + files[k].get_or_insert(out.program); + } + } + assert!(files[0] == files[1], "the files differ"); + assert_eq!(reads, [2, 50]); + for t in &mut times { + t.sort(); + } + let ms = |d: std::time::Duration| d.as_secs_f64() * 1000.0; + println!("50 placements of two 2048 x 2048 images, median of 7 runs ({how}):"); + println!("| pixels kept | images read | median (ms) | runs (ms) |"); + println!("|---|---:|---:|---|"); + for (k, label) in ["each image read", "the image read last"].into_iter().enumerate() { + let runs: Vec = times[k].iter().map(|d| format!("{:.0}", ms(*d))).collect(); + println!("| {label} | {} | {:.0} | {} |", reads[k], ms(times[k][3]), runs.join(", ")); + } +} + #[test] fn sixteen_bit_images_are_written_at_eight_bits() { let mut doc = Document::new(200.0, 100.0); diff --git a/crates/eps/src/ai_editing/writer.rs b/crates/eps/src/ai_editing/writer.rs index bea247262b..6595f11ffd 100644 --- a/crates/eps/src/ai_editing/writer.rs +++ b/crates/eps/src/ai_editing/writer.rs @@ -35,7 +35,7 @@ pub(super) use texts::wraps_held; pub(super) use images::PAYLOAD; #[cfg(test)] -pub(super) use images::{IMAGE_DEPTH, IMAGES, LINKS_EMBEDDED}; +pub(super) use images::{IMAGE_DEPTH, IMAGES, LAST_SAMPLES_ONLY, LINKS_EMBEDDED, SAMPLE_LIMITS, SAMPLE_READS, SAMPLES_KEPT}; #[cfg(test)] pub(super) use paints::{GRADIENT_HUE, GRADIENT_MIX, GRADIENT_SHAPES, GRADIENTS, STOP_LINKS, SWATCH_PLACEMENT, free_gradient_name}; #[cfg(test)] @@ -308,9 +308,9 @@ pub(super) struct Writer<'a> { /// How many more envelopes and images with a Warp may be written live: as many as the reader /// checks in one file ([`crate::import::ENVELOPE_CHECKS`]), which it spends on both. checks_left: usize, - /// The image read last and its pixels ([`images::Samples::of`], `None` when they can't be - /// read): the pieces of a warped image, which follow one another, read them once. - last_samples: Option<(vectorcraft_doc::ImageBlob, Option)>, + /// The images read so far and their pixels: an image placed again, and each piece of a warped + /// image, is read once. + read_samples: images::SampleCache, /// The images written whole so far, by the address of their data (which each entry keeps), each /// with the index of its samples in [`Self::payloads`]. whole_images: HashMap>, usize)>, @@ -401,7 +401,7 @@ impl<'a> Writer<'a> { definition_links: 0, definition_symbol_nodes: 0, max_objects: crate::import::MAX_EDITING_OBJECTS, - last_samples: None, + read_samples: images::SampleCache::default(), whole_images: HashMap::new(), shown: vec![], windowed: None, diff --git a/crates/eps/src/ai_editing/writer/images.rs b/crates/eps/src/ai_editing/writer/images.rs index 3813deccf9..408827663f 100644 --- a/crates/eps/src/ai_editing/writer/images.rs +++ b/crates/eps/src/ai_editing/writer/images.rs @@ -1,6 +1,9 @@ //! Images: embedded ones as a raster with its samples, linked ones as a placed file with a preview //! (see [`crate::ai_editing`] for the form). +#[cfg(test)] +use std::cell::Cell; +use std::collections::HashMap; use std::fmt::Write; use std::sync::Arc; @@ -30,6 +33,87 @@ pub(super) const SAMPLES_LINE: &str = "XI\u{1}"; /// An image's pixels as the data holds them, and whether it had 16 bits a channel ([`Samples::of`]). pub(super) type Read = Arc<(Samples, bool)>; +/// The most bytes of samples a save keeps read ([`SampleCache`]): color and alpha together. +const KEPT_SAMPLE_BYTES: usize = 256 << 20; +/// The most images whose samples a save keeps read ([`SampleCache`]), those that can't be read +/// included. +const KEPT_IMAGES: usize = 256; + +#[cfg(test)] +thread_local! { + /// How many times [`Samples::of`] has read an image on this thread. + pub(in crate::ai_editing) static SAMPLE_READS: Cell = const { Cell::new(0) }; + /// Whether [`Writer::samples`] keeps only the image read last on this thread, to compare its + /// file with the one [`SampleCache`] gives. + pub(in crate::ai_editing) static LAST_SAMPLES_ONLY: Cell = const { Cell::new(false) }; + /// The bytes of samples and the images [`SampleCache`] keeps at most on this thread. + pub(in crate::ai_editing) static SAMPLE_LIMITS: Cell<(usize, usize)> = const { Cell::new((KEPT_SAMPLE_BYTES, KEPT_IMAGES)) }; + /// The most bytes of samples and images a [`SampleCache`] kept on this thread, and whether one + /// held an image beside them. + pub(in crate::ai_editing) static SAMPLES_KEPT: Cell<(usize, usize, bool)> = const { Cell::new((0, 0, false)) }; +} + +/// The bytes of samples and the images [`SampleCache`] keeps at most. +#[cfg(not(test))] +const fn sample_limits() -> (usize, usize) { + (KEPT_SAMPLE_BYTES, KEPT_IMAGES) +} + +#[cfg(test)] +fn sample_limits() -> (usize, usize) { + SAMPLE_LIMITS.with(Cell::get) +} + +/// The images read during a save and their pixels ([`Samples::of`], `None` when they can't be +/// read), so that an image placed again is read once: up to [`KEPT_IMAGES`] images and +/// [`KEPT_SAMPLE_BYTES`] of samples, and beside them the image read last that they have no room +/// for. An image is known by the address of its data and its MIME type; each entry keeps the +/// image, so no other data takes that address while the entry is kept. +#[derive(Default)] +pub(super) struct SampleCache { + /// By the address of the image's data, each image with that data and its pixels. + kept: HashMap)>>, + /// How many images `kept` holds. + images: usize, + /// The bytes of samples `kept` holds. + bytes: usize, + /// The image read last that `kept` had no room for, and its pixels. + last: Option<(ImageBlob, Option)>, +} + +/// Whether `a` is the image `b`: the same data and MIME type. +fn same_image(a: &ImageBlob, b: &ImageBlob) -> bool { + Arc::ptr_eq(&a.bytes, &b.bytes) && a.mime == b.mime +} + +impl SampleCache { + /// The pixels kept for `blob`, when it was read. + fn get(&self, blob: &ImageBlob) -> Option> { + let kept = self.kept.get(&(Arc::as_ptr(&blob.bytes) as usize)).and_then(|v| v.iter().find(|(b, _)| same_image(b, blob))); + kept.or(self.last.as_ref().filter(|(b, _)| same_image(b, blob))).map(|(_, read)| read.clone()) + } + + /// Keep `read`, the pixels of `blob`: with the others while both limits allow, else beside them + /// in place of the image read last. + fn keep(&mut self, blob: &ImageBlob, read: Option) { + let size = read.as_deref().map_or(0, |(s, _)| s.color.len().saturating_add(s.alpha.as_ref().map_or(0, Vec::len))); + let (most_bytes, most_images) = sample_limits(); + match self.bytes.checked_add(size).filter(|b| *b <= most_bytes) { + Some(bytes) if self.images < most_images => { + self.bytes = bytes; + self.images += 1; + self.kept.entry(Arc::as_ptr(&blob.bytes) as usize).or_default().push((blob.clone(), read)); + } + _ => self.last = Some((blob.clone(), read)), + } + #[cfg(test)] + SAMPLES_KEPT.with(|k| { + let (b, n, last) = k.get(); + k.set((b.max(self.bytes), n.max(self.images), last || self.last.is_some())); + }); + } +} + /// A copy of an image written as the part of it that its regions show ([`Writer::image`]). pub(super) struct Windowed { /// Where its raster is in the body. @@ -61,6 +145,8 @@ impl Samples { /// or RGB samples, with an alpha channel when they need one. `None` when they can't be decoded or are more than /// [`MAX_PIXELS`]; the flag is set when the image had 16 bits a channel. fn of(blob: &ImageBlob) -> Option<(Samples, bool)> { + #[cfg(test)] + SAMPLE_READS.with(|n| n.set(n.get() + 1)); // The header's size is checked before the inks are decoded. if blob.is_cmyk() && blob.header_size().is_some_and(|(w, h)| u64::from(w) * u64::from(h) <= MAX_PIXELS) @@ -301,17 +387,26 @@ impl Writer<'_> { ); } - /// The pixels of `blob` ([`Samples::of`]), read once for images that follow one another with - /// the same data. + /// The pixels of `blob` ([`Samples::of`]), read once in a save for each image the + /// [`SampleCache`] has room for. fn samples(&mut self, blob: &ImageBlob) -> Option { - if let Some((last, read)) = &self.last_samples - && Arc::ptr_eq(&last.bytes, &blob.bytes) - && last.mime == blob.mime - { - return read.clone(); + #[cfg(test)] + if LAST_SAMPLES_ONLY.with(Cell::get) { + if let Some((last, read)) = &self.read_samples.last + && Arc::ptr_eq(&last.bytes, &blob.bytes) + && last.mime == blob.mime + { + return read.clone(); + } + let read = Samples::of(blob).map(Arc::new); + self.read_samples.last = Some((blob.clone(), read.clone())); + return read; + } + if let Some(read) = self.read_samples.get(blob) { + return read; } let read = Samples::of(blob).map(Arc::new); - self.last_samples = Some((blob.clone(), read.clone())); + self.read_samples.keep(blob, read.clone()); read }