EPS import: release only the image decoder's own buffers from the memory budget

What a data source procedure allocates stays counted: only the sample planes,
the decoder's copies of the strings a procedure returns, and the split buffers
of an interleaved masked image stop counting once the image is decoded.
This commit is contained in:
Don Omondi
2026-11-07 07:10:41 -05:00
parent 5817cabb3a
commit 53f60823b2
4 changed files with 46 additions and 23 deletions
+27 -10
View File
@@ -451,6 +451,10 @@ impl Interp<'_> {
continue;
}
let want = step.min(n - o.len());
// The bytes of this read's own copy of a procedure's string: counted while it
// lives, released once it is appended (the string itself is the program's, and
// stays counted as long as the program can hold it).
let mut copied = 0;
let chunk = match src {
Source::File(f) => self.read(f, want)?,
// A string gives its bytes over and over.
@@ -462,11 +466,14 @@ impl Interp<'_> {
self.call(p.clone())?;
let s = self.pop_str()?.to_vec();
self.alloc(s.len())?;
copied = s.len();
s
}
};
*e = chunk.is_empty();
o.extend_from_slice(chunk.get(..chunk.len().min(n - o.len())).unwrap_or_default());
drop(chunk);
self.unalloc(copied);
}
}
Ok(out)
@@ -549,12 +556,16 @@ impl Interp<'_> {
}
return Ok(None);
}
// Interleaved data is read into this decoder's own buffers, split into the two images'
// samples: those stop counting once both are decoded (their pixels still count).
let mut charged = 0;
if interleave != 3.0 {
let src = image.sources.first().cloned().ok_or(PsError::Ps("undefined", "DataSource".into()))?;
let (pixels, samples) = if interleave == 2.0 {
let (mrow, drow) = (stencil.row_bytes(1), image.row_bytes(n));
let total = mrow.saturating_mul(stencil.h).saturating_add(drow.saturating_mul(image.h));
self.alloc(total)?;
charged = total;
let raw = self.source_bytes(&src, total.min(MAX_DATA))?;
by_rows(&raw, (mrow, stencil.h), (drow, image.h))
} else {
@@ -562,15 +573,21 @@ impl Interp<'_> {
stencil.bpc = image.bpc;
(stencil.w, stencil.h) = (image.w, image.h);
let row = (image.w * (n + 1) * usize::from(image.bpc)).div_ceil(8);
self.alloc(row.saturating_mul(image.h))?;
let raw = self.source_bytes(&src, row.saturating_mul(image.h).min(MAX_DATA))?;
charged = row.saturating_mul(image.h);
self.alloc(charged)?;
let raw = self.source_bytes(&src, charged.min(MAX_DATA))?;
by_samples(&raw, image.w, image.h, n, image.bpc)
};
stencil.sources = vec![Source::Str(pixels)];
image.sources = vec![Source::Str(samples)];
}
let Some(mut img) = self.decode_image(image)? else { return Ok(None) };
let Some(m) = self.decode_image(stencil)? else { return Ok(Some(img)) };
let decoded = match self.decode_image(image) {
Ok(Some(img)) => self.decode_image(stencil).map(|m| Some((img, m))),
other => other.map(|_| None),
};
self.unalloc(charged);
let Some((mut img, m)) = decoded? else { return Ok(None) };
let Some(m) = m else { return Ok(Some(img)) };
let (w, h, mw, mh) = (img.w as usize, img.h as usize, m.w as usize, m.h as usize);
for (i, px) in img.rgba.as_chunks_mut::<4>().0.iter_mut().enumerate() {
let (x, y) = (i % w, i / w);
@@ -689,12 +706,12 @@ impl Interp<'_> {
return ps_err("rangecheck", "DataSource");
}
let plane = row.checked_mul(s.h).filter(|p| *p <= MAX_DATA).ok_or(PsError::Limit("decoded data is too large"))?;
// The planes and the strings a procedure gives to fill them live only while the image is
// decoded: they stop counting once it is (the pixels made from them still count).
let before = self.allocated();
self.alloc(plane.saturating_mul(sources.len()))?;
// The sample planes are this decoder's own and live only while the image is decoded: they
// stop counting once it is (the pixels made from them still count). What the data source
// procedures allocate is the program's and stays counted.
let plane_bytes = plane.saturating_mul(sources.len());
self.alloc(plane_bytes)?;
let mut planes = self.read_sources(sources, row * s.h, if multi { row } else { row * s.h })?;
let transient = self.allocated().saturating_sub(before);
for p in &mut planes {
p.resize(row * s.h, 0);
}
@@ -771,7 +788,7 @@ impl Interp<'_> {
}
}
drop(planes);
self.unalloc(transient);
self.unalloc(plane_bytes);
Ok(Some(Pixels { w: s.w as u32, h: s.h as u32, rgba, inks, matrix: s.matrix }))
}
+2 -7
View File
@@ -350,13 +350,8 @@ impl<'a> Interp<'a> {
Ok(())
}
/// The bytes counted against [`MAX_MEMORY`] so far.
pub(crate) fn allocated(&self) -> usize {
self.allocated
}
/// Stop counting `bytes` the program no longer holds: buffers that lived only while one
/// operator ran, freed when it returned.
/// Stop counting `bytes` the program no longer holds: buffers an operator made for itself and
/// freed before it returned. Never for what the program's own procedures allocated.
pub(crate) fn unalloc(&mut self, bytes: usize) {
self.allocated = self.allocated.saturating_sub(bytes);
}
+15 -4
View File
@@ -1419,10 +1419,10 @@ fn the_planes_of_multi_source_images_are_charged() {
assert!(r.warnings.iter().any(|w| w.contains("too much memory")), "{:?}", r.warnings);
}
/// The planes an image is decoded from, and the strings a procedure gives to fill them, stop
/// counting against the memory budget once the image is decoded: they are freed then. A procedure
/// that hands back one large buffer for every image (1 MiB here, for a one-pixel image) charged the
/// whole buffer each time, and a file of many images came in as its preview.
/// The planes an image is decoded from, and the decoder's copies of the strings a procedure gives
/// to fill them, stop counting against the memory budget once the image is decoded: they are freed
/// then. A procedure that hands back one large buffer for every image (1 MiB here, for a one-pixel
/// image) charged the whole buffer each time, and a file of many images came in as its preview.
#[test]
fn an_images_transient_data_is_not_charged_after_it() {
let body = "/buf 1048576 string def\n1100 { 1 1 8 [1 0 0 1 0 0] { buf } image } repeat\n";
@@ -1433,6 +1433,17 @@ fn an_images_transient_data_is_not_charged_after_it() {
assert_eq!(images, 1100);
}
/// What a data source procedure allocates stays counted, whatever image it fills: a procedure
/// that keeps each new string (in an array here) still runs into the memory budget, however small
/// the images it fills are.
#[test]
fn strings_a_data_source_keeps_stay_charged() {
let body = "/keep 1100 array def /i 0 def\n\
1100 { 1 1 8 [1 0 0 1 0 0] { keep i 1048576 string put keep i get /i i 1 add def } image } repeat\n";
let r = import(&eps(body)).unwrap();
assert!(r.warnings.iter().any(|w| w.contains("too much memory")), "{:?}", r.warnings);
}
/// #3568: a single sample plane over the data limit is refused.
#[test]
fn an_oversized_image_plane_is_refused() {
+2 -2
View File
@@ -1,6 +1,6 @@
# File format parity
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-06 · **Change:** minor (EPS import: an image's transient data stops counting against the memory budget once it is decoded; EPS and `.ai` editing data: a transparency set inside a clipping group stays after its `Q`; SVG import: filter and mask regions clip, #5876, #5875; SVG import: Gaussian blurs keep `color-interpolation-filters`, #5877; PDF and `.ai` export: objects' URLs as links, #5785; SVG import: `color-interpolation="linearRGB"` gradients, #5878; `.ai` import: paths with a Calligraphic brush open brushed, #372) · **Target:** Adobe Illustrator 2026 (30.x)
> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-06 · **Change:** minor (EPS import: an image decoder's own buffers stop counting against the memory budget once it is decoded; EPS and `.ai` editing data: a transparency set inside a clipping group stays after its `Q`; SVG import: filter and mask regions clip, #5876, #5875; SVG import: Gaussian blurs keep `color-interpolation-filters`, #5877; PDF and `.ai` export: objects' URLs as links, #5785; SVG import: `color-interpolation="linearRGB"` gradients, #5878; `.ai` import: paths with a Calligraphic brush open brushed, #372) · **Target:** Adobe Illustrator 2026 (30.x)
Every format Illustrator opens, places, saves or exports, against VectorCraft's format table (`FORMATS`,
`OPEN_EXTS`, `PLACE_EXTS` in `crates/engine/src/cmd/fileio/formats.rs`). Illustrator's list comes from its public
@@ -212,7 +212,7 @@ the app's own editing copy of a file (an EPS after its `%%EOF`, a `.ai`'s `AIPri
| Date | Change | Summary |
|---|---|---|
| 2026-11-06 | minor | EPS import: the buffers an image is decoded from (its sample planes, and the strings a procedure data source hands back to fill them) stop counting against the PostScript reader's 1 GiB memory budget once the image is decoded, as they are freed then; the pixels kept still count. A user's Illustrator 25.2 EPS with 71 CMYK images (used locally, not committed) used to stop with "the program uses too much memory" and come in as its screen preview; it now opens with its art. The budget itself is unchanged. Scores and estimates unchanged. |
| 2026-11-06 | minor | EPS import: the buffers an image decoder makes for itself (its sample planes, its copies of the strings a procedure data source hands back, and the split buffers of an interleaved masked image) stop counting against the PostScript reader's 1 GiB memory budget once the image is decoded, as they are freed then; the pixels kept, and anything the program's own procedures allocate, still count. A user's Illustrator 25.2 EPS with 71 CMYK images (used locally, not committed) used to stop with "the program uses too much memory" and come in as its screen preview; it now opens with its art. The budget itself is unchanged. Scores and estimates unchanged. |
| 2026-11-06 | minor | EPS and `.ai` editing data: `Q` restores the painting state but not the transparency (`Xy`), which the writer only sets when the next object's differs from the last one written. A Screen glow followed by a clipping group set back to Normal no longer leaves the type after the group in Screen: two users' Illustrator 26.5 and 30 EPS posters (used locally, not committed) now show their gold heading and crescent, and both open from their layers (2.5% and 2.7% from the page before, under the limit after). Scores and estimates unchanged. |
| 2026-11-06 | minor | EPS and `.ai` editing data: a blend whose spine was never edited, written with its keys and no spine part, comes in as a live blend along the straight lines between its keys' centres (in its record's order) instead of as its drawn steps. Checked on six users' Illustrator EPS files (used locally, not committed): every blend in four of them now comes in live, their renders unchanged; two still have blends the reader can't match to their drawn steps. Scores and estimates unchanged. |
| 2026-11-06 | minor | EPS and `.ai` editing data: the art styles are found before the art is read, so objects in symbols defined ahead of `%AI9_BeginArtStyles` (as Illustrator 24 writes it) come in as themselves with their style instead of their drawn look. Checked on two users' EPS files (used locally, not committed): one's Symbols panel gains live Feather and Round Corners, its drawn-look count drops from 5 to 2, and both render unchanged; own-fixture test in `import::ai::tests`; scores and hours unchanged |