mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-24 19:36:42 +00:00
Legacy .ai: gradient definitions are found by a key of their gradient
Each gradient fill looked for its definition by comparing its gradient with every definition written so far, so a document with thousands of distinct gradients spent most of the export in these comparisons. The writer now keeps each definition's place under a key made from every field of its gradient, the same key the editing data's writer uses, which moves to a module both writers share. Each candidate under a key is still compared in full, and the first equal definition in definition order is used. Spot color names, written spot colors and fonts are also found through maps and sets, while the lists the file is written from keep their order. The written file is unchanged, byte for byte. On Linux, writing 10,006 gradient fills (5,000 distinct and 5,000 of 50 others) took 79 ms of thread CPU time through the index and 291 ms comparing each definition in turn (median of 7 runs, release build).
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::Write;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
use vectorcraft_color::gradient::GradientGeom;
|
||||
use vectorcraft_color::swatch::REGISTRATION;
|
||||
@@ -14,6 +13,7 @@ use vectorcraft_geom::{Affine, BezPath, Rect, Shape, Vec2};
|
||||
|
||||
use super::{MAX_NEST, Writer, Xy};
|
||||
use crate::ai_ops::{self, average, unit};
|
||||
use crate::gradient_key::gradient_key;
|
||||
use crate::ps;
|
||||
|
||||
pub(in crate::ai_editing) const GRADIENTS: &str =
|
||||
@@ -64,27 +64,6 @@ fn same_gradient(a: &Gradient, b: &Gradient) -> bool {
|
||||
a == b
|
||||
}
|
||||
|
||||
/// A key of `g` for [`GradientDefs`]: the same gradients have the same key (each number by its
|
||||
/// bits, with -0 as 0); different gradients may share one.
|
||||
fn gradient_key(g: &Gradient) -> u64 {
|
||||
let bits = |x: f32| if x == 0.0 { 0 } else { x.to_bits() };
|
||||
let mut h = std::hash::DefaultHasher::new();
|
||||
std::mem::discriminant(&g.kind).hash(&mut h);
|
||||
std::mem::discriminant(&g.method).hash(&mut h);
|
||||
(g.dither, g.spread, g.repeats, g.stops.len()).hash(&mut h);
|
||||
for s in &g.stops {
|
||||
let (model, c) = match s.color {
|
||||
Color::Rgb { r, g, b } => (0u8, [r, g, b, 0.0]),
|
||||
Color::Cmyk { c, m, y, k } => (1, [c, m, y, k]),
|
||||
Color::Gray { k } => (2, [k, 0.0, 0.0, 0.0]),
|
||||
Color::Lab { l, a, b } => (3, [l, a, b, 0.0]),
|
||||
};
|
||||
(model, c.map(bits), [s.offset, s.opacity, s.midpoint, s.tint].map(bits)).hash(&mut h);
|
||||
(&s.swatch, s.plates.map(|p| p.map(bits))).hash(&mut h);
|
||||
}
|
||||
h.finish()
|
||||
}
|
||||
|
||||
/// The gradient definitions, in the order they are first used, found by name and by gradient
|
||||
/// without comparing each one.
|
||||
#[derive(Default)]
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
//! A key of a gradient for the gradient definitions the legacy `.ai` writer
|
||||
//! ([`crate::legacy_ai`]) and the editing data writer ([`crate::ai_editing`]) look up: the writers
|
||||
//! keep each definition's place under its gradient's key and compare only the gradients that
|
||||
//! share it.
|
||||
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
use vectorcraft_color::{Color, Gradient};
|
||||
|
||||
/// A key of `g`: the same gradients have the same key (each number by its bits, with -0 as 0);
|
||||
/// different gradients may share one.
|
||||
pub(crate) fn gradient_key(g: &Gradient) -> u64 {
|
||||
let bits = |x: f32| if x == 0.0 { 0 } else { x.to_bits() };
|
||||
let mut h = std::hash::DefaultHasher::new();
|
||||
std::mem::discriminant(&g.kind).hash(&mut h);
|
||||
std::mem::discriminant(&g.method).hash(&mut h);
|
||||
(g.dither, g.spread, g.repeats, g.stops.len()).hash(&mut h);
|
||||
for s in &g.stops {
|
||||
let (model, c) = match s.color {
|
||||
Color::Rgb { r, g, b } => (0u8, [r, g, b, 0.0]),
|
||||
Color::Cmyk { c, m, y, k } => (1, [c, m, y, k]),
|
||||
Color::Gray { k } => (2, [k, 0.0, 0.0, 0.0]),
|
||||
Color::Lab { l, a, b } => (3, [l, a, b, 0.0]),
|
||||
};
|
||||
(model, c.map(bits), [s.offset, s.opacity, s.midpoint, s.tint].map(bits)).hash(&mut h);
|
||||
(&s.swatch, s.plates.map(|p| p.map(bits))).hash(&mut h);
|
||||
}
|
||||
h.finish()
|
||||
}
|
||||
@@ -1696,6 +1696,119 @@ fn colliding_gradient_and_spot_names_are_numbered_as_before() {
|
||||
assert!(text.contains("%%DocumentCustomColors: (?)\n%%+ (? 2)\n%%+ (? 3)\n%%+ (? 2 2)\n"), "{text}");
|
||||
}
|
||||
|
||||
/// `doc` written with its gradient definitions found by comparing each in turn (`scanned`) or
|
||||
/// through their index.
|
||||
fn export_scanned(doc: &Document, scanned: bool) -> LegacyAiOutput {
|
||||
let o = LegacyAiOptions { created: Some(0), ..LegacyAiOptions::default() };
|
||||
super::writer::SCANNED_DEFINITIONS.with(|s| s.set(scanned));
|
||||
let out = export_legacy_ai(doc, &o);
|
||||
super::writer::SCANNED_DEFINITIONS.with(|s| s.set(false));
|
||||
out.unwrap()
|
||||
}
|
||||
|
||||
/// A document of rectangles filled with gradients: one with a gradient swatch `Sky` of its own;
|
||||
/// `distinct` with gradients of their own; `reused` with one of `ramps` others in turn; one with a
|
||||
/// gradient swatch `Dawn` whose gradient is the first of those `ramps`; two whose gradients differ
|
||||
/// only in the sign of a zero offset; and two with the same gradient, whose midpoint is not a
|
||||
/// number.
|
||||
fn many_gradients(distinct: u16, reused: u16, ramps: u16) -> Document {
|
||||
let mut doc = Document::new(1000.0, 1000.0);
|
||||
let layer = doc.layers[0].id;
|
||||
let shade = |i: u16, n: u16| f32::from(i) / f32::from(n.max(1));
|
||||
let mut paints = vec![gradient_from(Color::rgb(0.1, 0.2, 0.3), Some("Sky"))];
|
||||
for i in 0..distinct {
|
||||
paints.push(gradient_from(Color::rgb(shade(i, distinct), 0.5, 0.0), None));
|
||||
}
|
||||
for i in 0..reused {
|
||||
paints.push(gradient_from(Color::cmyk(0.0, shade(i % ramps.max(1), ramps), 0.5, 0.0), None));
|
||||
}
|
||||
paints.push(gradient_from(Color::cmyk(0.0, 0.0, 0.5, 0.0), Some("Dawn")));
|
||||
let stops = |offset: f32, midpoint: f32| {
|
||||
let mut first = GradientStop::new(offset, Color::rgb(0.2, 0.3, 0.4));
|
||||
first.midpoint = midpoint;
|
||||
let g = Gradient::new(GradientKind::Linear, vec![first, GradientStop::new(1.0, Color::BLACK)]);
|
||||
Paint::Gradient(Box::new(GradientPaint::new(g)))
|
||||
};
|
||||
paints.extend([stops(0.0, 0.5), stops(-0.0, 0.5), stops(0.0, f32::NAN), stops(0.0, f32::NAN)]);
|
||||
for (i, paint) in (0u16..).zip(paints) {
|
||||
let (x, y) = (f64::from(i % 40) * 25.0, f64::from(i / 40 % 40) * 25.0);
|
||||
let n = shape(&mut doc, shapes::rectangle(Rect::new(x, y, x + 20.0, y + 20.0)), paint, Paint::None);
|
||||
doc.insert(Some(layer), usize::MAX, n).unwrap();
|
||||
}
|
||||
doc
|
||||
}
|
||||
|
||||
/// Gradient definitions found through their index are those found by comparing each in turn: the
|
||||
/// file is the same, byte for byte, with the same warnings. An earlier definition of the same
|
||||
/// gradient serves a gradient swatch of another name; gradients that differ only in the sign of a
|
||||
/// zero share a definition; a gradient that is not equal to itself is defined each time.
|
||||
#[test]
|
||||
fn indexed_gradient_definitions_write_the_same_file() {
|
||||
let doc = many_gradients(300, 300, 7);
|
||||
let (indexed, scanned) = (export_scanned(&doc, false), export_scanned(&doc, true));
|
||||
let differ = indexed.bytes.iter().zip(&scanned.bytes).position(|(a, b)| a != b);
|
||||
assert!(indexed.bytes == scanned.bytes, "the files differ from byte {differ:?} ({} and {} bytes)", indexed.bytes.len(), scanned.bytes.len());
|
||||
assert_eq!(indexed.warnings, scanned.warnings);
|
||||
assert_eq!(indexed.setting_notes, scanned.setting_notes);
|
||||
let text = String::from_utf8(indexed.bytes).unwrap();
|
||||
let defined: Vec<&str> = text.lines().filter_map(|l| l.strip_suffix(" 0 2 Bd")).collect();
|
||||
// `Sky`, 300 of their own, 7 reused, 1 for both signs of zero and 2 for the gradient that
|
||||
// is not a number.
|
||||
assert_eq!(defined.len(), 1 + 300 + 7 + 1 + 2, "{defined:?}");
|
||||
assert_eq!(defined.first(), Some(&"(Sky)"));
|
||||
assert!(!defined.contains(&"(Dawn)"), "{defined:?}");
|
||||
}
|
||||
|
||||
/// A gradient is matched with the definitions of the same gradient, not with each definition:
|
||||
/// 1,000 gradients of their own and 1,000 of 10 others take a comparison or so each, where
|
||||
/// comparing each definition in turn takes hundreds of thousands.
|
||||
#[test]
|
||||
fn legacy_gradient_definitions_are_found_without_comparing_each() {
|
||||
let doc = many_gradients(1000, 1000, 10);
|
||||
let comparisons = |scanned: bool| {
|
||||
super::writer::GRADIENT_COMPARISONS.with(|c| c.set(0));
|
||||
export_scanned(&doc, scanned);
|
||||
super::writer::GRADIENT_COMPARISONS.with(std::cell::Cell::get)
|
||||
};
|
||||
let (indexed, scanned) = (comparisons(false), comparisons(true));
|
||||
assert!(indexed <= 2 * 2006, "{indexed} comparisons through the index");
|
||||
assert!(scanned > 100 * indexed, "{scanned} comparisons scanning, {indexed} through the index");
|
||||
}
|
||||
|
||||
/// The time to write 5,000 rectangles with gradients of their own and 5,000 with 50 others, with
|
||||
/// the definitions found by comparing each in turn and through their index: the median of 7 runs
|
||||
/// of each, alternating, with the files compared. Run with
|
||||
/// `cargo test --release -p vectorcraft-eps --lib -- --ignored --nocapture legacy_gradient_definitions_timing`.
|
||||
#[test]
|
||||
#[ignore = "timing"]
|
||||
fn legacy_gradient_definitions_timing() {
|
||||
let doc = many_gradients(5000, 5000, 50);
|
||||
let mut times = [vec![], vec![]];
|
||||
let mut files: [Option<Vec<u8>>; 2] = [None, None];
|
||||
let mut how = "";
|
||||
for _ in 0..7 {
|
||||
for (k, scanned) in [true, false].into_iter().enumerate() {
|
||||
let (start, h) = thread_time();
|
||||
let out = export_scanned(&doc, scanned);
|
||||
times[k].push(thread_time().0.saturating_sub(start));
|
||||
how = h;
|
||||
files[k].get_or_insert(out.bytes);
|
||||
}
|
||||
}
|
||||
assert_eq!(files[0], files[1]);
|
||||
for t in &mut times {
|
||||
t.sort();
|
||||
}
|
||||
let ms = |d: std::time::Duration| d.as_secs_f64() * 1000.0;
|
||||
println!("10,006 gradient fills, median of 7 runs ({how}):");
|
||||
println!("| definitions found | median (ms) | runs (ms) |");
|
||||
println!("|---|---:|---|");
|
||||
for (k, label) in ["comparing each in turn", "through the index"].into_iter().enumerate() {
|
||||
let runs: Vec<String> = times[k].iter().map(|d| format!("{:.1}", ms(*d))).collect();
|
||||
println!("| {label} | {:.1} | {} |", ms(times[k][3]), runs.join(", "));
|
||||
}
|
||||
}
|
||||
|
||||
/// 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::<u64>().ok()) {
|
||||
|
||||
@@ -10,6 +10,7 @@ use super::{
|
||||
TextObject, UNDECODED, Vec2, Write, ai_ops, latin, ps, stroke,
|
||||
};
|
||||
use super::{CONIC, CONIC_WEDGES, DEEP_DISTORTED, DIAMOND, GRAPHS, HUE_STOPS, PERCEPTUAL_STOPS, SPREAD, SPREAD_CAPPED};
|
||||
use crate::gradient_key::gradient_key;
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
@@ -18,6 +19,18 @@ thread_local! {
|
||||
/// Whether [`Numbered::take`] numbers names on this thread by searching the names given so
|
||||
/// far, one number at a time, for each number it tries.
|
||||
pub(super) static SCANNED_NAMES: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
|
||||
/// Whether [`Writer::defined`] finds a gradient's definition on this thread by comparing the
|
||||
/// gradient with each definition in turn.
|
||||
pub(super) static SCANNED_DEFINITIONS: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
|
||||
/// How many times [`Writer::defined`] has compared two gradients on this thread.
|
||||
pub(super) static GRADIENT_COMPARISONS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// Are gradients `a` and `b` the same?
|
||||
fn same_gradient(a: &Gradient, b: &Gradient) -> bool {
|
||||
#[cfg(test)]
|
||||
GRADIENT_COMPARISONS.with(|c| c.set(c.get() + 1));
|
||||
a == b
|
||||
}
|
||||
|
||||
/// An image's coverage ([`ImageBlob::coverage`]): its width, its height and one byte a pixel.
|
||||
@@ -34,7 +47,7 @@ pub(super) struct Writer<'a> {
|
||||
/// Spot colors used: the name written and full CMYK equivalent.
|
||||
pub(super) spots: Vec<(String, [f32; 4])>,
|
||||
/// The name each spot color (by its swatch's name) is written with ([`Self::spot`]).
|
||||
spot_names: Vec<(String, String)>,
|
||||
spot_names: std::collections::HashMap<String, String>,
|
||||
pub(super) warnings: Vec<String>,
|
||||
/// Notes on what the options asked for ([`crate::LegacyAiOutput::setting_notes`]).
|
||||
pub(super) setting_notes: Vec<String>,
|
||||
@@ -73,6 +86,13 @@ pub(super) struct Writer<'a> {
|
||||
gradient_names: Numbered,
|
||||
/// The names spot colors are written with ([`Self::spot`]).
|
||||
spot_written: Numbered,
|
||||
/// The names of [`Self::spots`].
|
||||
spot_set: std::collections::HashSet<String>,
|
||||
/// The PostScript names of [`Self::fonts`].
|
||||
font_set: std::collections::HashSet<String>,
|
||||
/// The places in [`Self::gradients`] of the definitions, in order, by their gradient's key
|
||||
/// ([`gradient_key`]).
|
||||
gradient_index: std::collections::HashMap<u64, Vec<usize>>,
|
||||
}
|
||||
|
||||
impl<'a> Writer<'a> {
|
||||
@@ -84,7 +104,7 @@ impl<'a> Writer<'a> {
|
||||
body: String::new(),
|
||||
gradients: vec![],
|
||||
spots: vec![],
|
||||
spot_names: vec![],
|
||||
spot_names: std::collections::HashMap::new(),
|
||||
warnings: vec![],
|
||||
setting_notes: vec![],
|
||||
locked: false,
|
||||
@@ -103,6 +123,9 @@ impl<'a> Writer<'a> {
|
||||
in_graph: false,
|
||||
gradient_names: Numbered::default(),
|
||||
spot_written: Numbered::default(),
|
||||
spot_set: std::collections::HashSet::new(),
|
||||
font_set: std::collections::HashSet::new(),
|
||||
gradient_index: std::collections::HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,16 +183,17 @@ impl<'a> Writer<'a> {
|
||||
/// Windows-1252 are written as `?` (with a warning), and it is numbered when another spot color
|
||||
/// is already written with that name; the name is the same each time.
|
||||
fn spot(&mut self, name: &str, cmyk: [f32; 4]) -> String {
|
||||
let written = match self.spot_names.iter().find(|(n, _)| n == name) {
|
||||
Some((_, w)) => w.clone(),
|
||||
let written = match self.spot_names.get(name) {
|
||||
Some(w) => w.clone(),
|
||||
None => {
|
||||
let base = self.latin_name(name, TITLE_AND_COLOR_NAMES);
|
||||
let written = self.spot_written.take(base);
|
||||
self.spot_names.push((name.to_string(), written.clone()));
|
||||
self.spot_names.insert(name.to_string(), written.clone());
|
||||
written
|
||||
}
|
||||
};
|
||||
if !self.spots.iter().any(|(n, _)| *n == written) {
|
||||
if !self.spot_set.contains(&written) {
|
||||
self.spot_set.insert(written.clone());
|
||||
self.spots.push((written.clone(), cmyk));
|
||||
}
|
||||
written
|
||||
@@ -178,7 +202,7 @@ impl<'a> Writer<'a> {
|
||||
/// The name spot color `name` (its swatch's name) is written with, once [`Self::spot`] has
|
||||
/// noted it.
|
||||
pub(super) fn spot_name<'n>(&'n self, name: &'n str) -> &'n str {
|
||||
self.spot_names.iter().find(|(n, _)| n == name).map_or(name, |(_, w)| w.as_str())
|
||||
self.spot_names.get(name).map_or(name, String::as_str)
|
||||
}
|
||||
|
||||
/// A top-level layer. False when it isn't a layer.
|
||||
@@ -831,15 +855,26 @@ impl<'a> Writer<'a> {
|
||||
/// The name of the definition of `g` (its swatch's name when it has one, with each character
|
||||
/// outside Windows-1252 written as `?` and a warning), defined the first time.
|
||||
fn define(&mut self, g: &Gradient, swatch: Option<&str>) -> String {
|
||||
if let Some((name, _)) = self.gradients.iter().find(|(_, d)| d == g) {
|
||||
let key = gradient_key(g);
|
||||
if let Some((name, _)) = self.defined(g, key).and_then(|i| self.gradients.get(i)) {
|
||||
return name.clone();
|
||||
}
|
||||
let base = self.latin_name(swatch.filter(|s| !s.is_empty()).unwrap_or("Gradient"), TITLE_AND_COLOR_NAMES);
|
||||
let name = self.gradient_names.take(base);
|
||||
self.gradient_index.entry(key).or_default().push(self.gradients.len());
|
||||
self.gradients.push((name.clone(), g.clone()));
|
||||
name
|
||||
}
|
||||
|
||||
/// The place in [`Self::gradients`] of the first definition of `g`, whose key is `key`.
|
||||
fn defined(&self, g: &Gradient, key: u64) -> Option<usize> {
|
||||
#[cfg(test)]
|
||||
if SCANNED_DEFINITIONS.with(std::cell::Cell::get) {
|
||||
return self.gradients.iter().position(|(_, d)| same_gradient(d, g));
|
||||
}
|
||||
self.gradient_index.get(&key)?.iter().copied().find(|i| self.gradients.get(*i).is_some_and(|(_, d)| same_gradient(d, g)))
|
||||
}
|
||||
|
||||
// ---------- type ----------
|
||||
|
||||
/// Type: a text object when the format can hold it as text, else its outlines (type with
|
||||
@@ -1016,7 +1051,8 @@ impl<'a> Writer<'a> {
|
||||
self.line_style(&st.stroke_layer());
|
||||
}
|
||||
let (name, truetype, ascent, descent) = font_of(st);
|
||||
if !self.fonts.iter().any(|(n, _)| *n == name) {
|
||||
if !self.font_set.contains(&name) {
|
||||
self.font_set.insert(name.clone());
|
||||
self.fonts.push((name.clone(), truetype));
|
||||
}
|
||||
let size = finite(st.size, 12.0);
|
||||
|
||||
@@ -32,12 +32,14 @@
|
||||
//! ([`export_ai_editing`], [`ai_private_data`]).
|
||||
//! - `ai_ops`: the path and line style operators those two write.
|
||||
//! - `ai_graph`: the records of a live graph those two write around its group.
|
||||
//! - `gradient_key`: the key those two find gradient definitions by.
|
||||
//! - [`dcs`]: DCS files, an EPS file with one program per printing plate, read for placing.
|
||||
|
||||
mod ai_editing;
|
||||
mod ai_graph;
|
||||
mod ai_ops;
|
||||
pub mod dcs;
|
||||
mod gradient_key;
|
||||
mod import;
|
||||
mod latin;
|
||||
mod legacy_ai;
|
||||
|
||||
Reference in New Issue
Block a user