mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
EPS import: measuring an image's data left out charges one operation per 16 bytes
Measuring the data of an image too large to hold charged one operation per 256 decoded bytes. A program that measures the same 300 MiB of compressed zeros in a loop decoded about 22.8 GB, about 72 passes, before its budget ran out, and the reading took about 55 s. measuring_data_too_large_to_hold_in_a_loop_runs_out_of_the_budget took 58 s alone and failed its 300 s guard in a loaded full test run. Measuring now charges one operation per 16 decoded bytes, and stops at what the budget still pays for at that rate. A program's budget grows by 64 operations per byte, so one image compressed up to 1024 to 1 is still measured within what its own bytes add. The loop now runs out of the budget in its fifth pass, and the test takes 3.6 s. The test also checks how much was measured: more than one pass and at most five. With the old rate it fails with 22781165568 bytes measured.
This commit is contained in:
1 parent
5d70217e0c
commit
130bb35fce
4 files changed
+47
-14
No files matched your search
@@ -21,3 +21,5 @@ mod ops;
|
||||
mod pixels;
|
||||
|
||||
pub(crate) use filters::Stream;
|
||||
#[cfg(test)]
|
||||
pub(crate) use ops::measured;
|
||||
@@ -8,6 +8,23 @@ use super::{
|
||||
/// Most bytes one filter or image reads.
|
||||
pub(super) const MAX_DATA: usize = 1 << 28;
|
||||
|
||||
/// Decoded bytes the measuring of an image's data left out charges one operation for. A program's
|
||||
/// budget grows by 64 operations for each byte of it, so the data of one image compressed up to
|
||||
/// 1024 to 1 is measured within what its own compressed bytes add.
|
||||
const MEASURED_PER_OP: usize = 16;
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// How many decoded bytes the measuring of images' data left out on this thread decoded.
|
||||
static MEASURED: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// [`MEASURED`] of this thread.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn measured() -> u64 {
|
||||
MEASURED.get()
|
||||
}
|
||||
|
||||
/// Most data sources one image may name (a DeviceN space has up to 32 components).
|
||||
const MAX_SOURCES: usize = 32;
|
||||
/// The longest chain of filters over one pending file (real programs stack two or three).
|
||||
@@ -538,10 +555,10 @@ impl Interp<'_> {
|
||||
// it was; the import goes on without the image either way.
|
||||
let Some(first) = chain.first() else { return };
|
||||
// Measuring decompresses the data, which is charged by the bytes decoded (one operation
|
||||
// per 256, as `spend_bytes` charges) and stops at what the budget still pays for:
|
||||
// data measured again and again (a reusable stream reset in a loop) runs out of the
|
||||
// budget instead of being decoded in full on every pass.
|
||||
let most = usize::try_from(self.ops_left().saturating_mul(256)).unwrap_or(usize::MAX);
|
||||
// per [`MEASURED_PER_OP`]) and stops at what the budget still pays for: data measured
|
||||
// again and again (a reusable stream reset in a loop) runs out of the budget after a
|
||||
// few passes instead of being decoded in full on every pass.
|
||||
let most = usize::try_from(self.ops_left().saturating_mul(MEASURED_PER_OP as u64)).unwrap_or(usize::MAX);
|
||||
let (used, decoded) = match &from {
|
||||
Some(r) => match &*r.borrow() {
|
||||
Stream::Buf { data, pos, .. } => encoded_len(first, data.get(*pos..).unwrap_or_default(), most),
|
||||
@@ -549,7 +566,9 @@ impl Interp<'_> {
|
||||
},
|
||||
None => encoded_len(first, self.lex.data(), most),
|
||||
};
|
||||
let paid = self.spend_bytes(decoded).is_ok();
|
||||
#[cfg(test)]
|
||||
MEASURED.set(MEASURED.get().saturating_add(decoded as u64));
|
||||
let paid = self.spend((decoded / MEASURED_PER_OP) as u64).is_ok();
|
||||
let Some(used) = used.filter(|_| paid) else {
|
||||
*f.borrow_mut() = Stream::Pending { chain, from };
|
||||
return;
|
||||
|
||||
@@ -7,6 +7,7 @@ use vectorcraft_color::{Color, GradientKind, Paint};
|
||||
use vectorcraft_doc::{AppearanceItem, ColorMode, Document, LineCap, LineJoin, Node, NodeKind};
|
||||
use vectorcraft_geom::{Point, Rect};
|
||||
|
||||
use crate::import::data::measured;
|
||||
use crate::import::graphics::{GState, Out, tilings_compared};
|
||||
use crate::import::interp::Interp;
|
||||
use crate::import::obj::Obj;
|
||||
@@ -1230,16 +1231,19 @@ fn read_within(body: &str, secs: u64) -> crate::Imported {
|
||||
|
||||
/// [`read_soon`], with how many patterns the tiling pattern lookups compared while reading.
|
||||
fn read_counting_tilings(body: &str) -> (crate::Imported, u64) {
|
||||
read_counting(body, NEVER_ENDS)
|
||||
let (read, compared, _) = read_counting(body, NEVER_ENDS);
|
||||
(read, compared)
|
||||
}
|
||||
|
||||
/// [`read_within`], with how many patterns the tiling pattern lookups compared while reading.
|
||||
fn read_counting(body: &str, secs: u64) -> (crate::Imported, u64) {
|
||||
/// [`read_within`], with how many patterns the tiling pattern lookups compared while reading, and
|
||||
/// how many decoded bytes the measuring of images' data left out decoded.
|
||||
fn read_counting(body: &str, secs: u64) -> (crate::Imported, u64, u64) {
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let ps = eps(body);
|
||||
std::thread::spawn(move || tx.send((import(&ps), tilings_compared())).ok());
|
||||
let (read, compared) = rx.recv_timeout(std::time::Duration::from_secs(secs)).unwrap_or_else(|_| panic!("reading {body} did not finish"));
|
||||
(read.unwrap(), compared)
|
||||
std::thread::spawn(move || tx.send((import(&ps), tilings_compared(), measured())).ok());
|
||||
let (read, compared, measured) =
|
||||
rx.recv_timeout(std::time::Duration::from_secs(secs)).unwrap_or_else(|_| panic!("reading {body} did not finish"));
|
||||
(read.unwrap(), compared, measured)
|
||||
}
|
||||
|
||||
/// Runs the program of `body` in an interpreter of its own on the 100 × 100 page of [`eps`]'s
|
||||
@@ -1264,9 +1268,15 @@ fn run_looking<T>(body: &str, look: fn(&Interp) -> T) -> T {
|
||||
/// (`search_in_a_loop_runs_out_of_the_budget` searches about 3 GB, 14 s on a fast desktop), so the
|
||||
/// limit here only catches a program that never stops.
|
||||
pub(super) fn runs_out_of_budget(body: &str) {
|
||||
let r = read_within(&format!("0 0 10 10 rectfill {body}"), NEVER_ENDS);
|
||||
runs_out_of_budget_measuring(body);
|
||||
}
|
||||
|
||||
/// [`runs_out_of_budget`] → how many decoded bytes the measuring of images' data left out decoded.
|
||||
pub(super) fn runs_out_of_budget_measuring(body: &str) -> u64 {
|
||||
let (r, _, measured) = read_counting(&format!("0 0 10 10 rectfill {body}"), NEVER_ENDS);
|
||||
assert_eq!(objects(&r.document).len(), 1, "{body}: {:?}", r.warnings);
|
||||
assert!(r.warnings.iter().any(|w| w.contains("the program runs too long")), "{body}: {:?}", r.warnings);
|
||||
measured
|
||||
}
|
||||
|
||||
/// A large file that runs far more operations than a small one may, but only a few hundred for
|
||||
|
||||
@@ -670,14 +670,16 @@ fn the_data_of_an_image_too_large_to_hold_is_skipped() {
|
||||
/// Measuring the data of an image too large to hold decompresses it, and that is charged to the
|
||||
/// operation budget: a program kept in a reusable stream, whose image over 300 MB of compressed
|
||||
/// zeros is left out and measured again each time a loop runs the stream from its start, runs out
|
||||
/// of the budget.
|
||||
/// of the budget after it measured the data in full more than once and at most five times.
|
||||
#[test]
|
||||
fn measuring_data_too_large_to_hold_in_a_loop_runs_out_of_the_budget() {
|
||||
let mut program =
|
||||
b"10000 10000 8 [1 0 0 1 0 0] currentfile << /Predictor 15 /Colors 3 /Columns 10000 >> /FlateDecode filter false 3 colorimage\n".to_vec();
|
||||
program.extend(crate::ps::deflate(&vec![0u8; 300 << 20]));
|
||||
let hex: String = program.iter().map(|b| format!("{b:02x}")).collect();
|
||||
super::tests::runs_out_of_budget(&format!("/P <{hex}> /ReusableStreamDecode filter def {{ P 0 setfileposition P cvx exec }} loop"));
|
||||
let measured =
|
||||
super::tests::runs_out_of_budget_measuring(&format!("/P <{hex}> /ReusableStreamDecode filter def {{ P 0 setfileposition P cvx exec }} loop"));
|
||||
assert!(((300 << 20) + 1..=5 * (300 << 20)).contains(&measured), "{measured} bytes measured");
|
||||
}
|
||||
|
||||
/// When an image too large to read is left out, the data of its mask (`InterleaveType` 3) is
|
||||
|
||||
Reference in new issue
Block a user