Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
it, `list[i]` or `.get(i)` for one item, `.len()` and `.is_empty()` as
before, and `.to_vec()` or `Vec::from(list)` where you need a `Vec`. It
compares equal to a slice, `Vec` or array of the same items, either way
round. It has no public constructor (#769).
round. It has no public constructor but `Default`, an empty list (#769).
- `IncrementalDecoder::header` leaves out of the metadata a last box of length
0 after the codestream of a type the metadata reports (`xml `, `uuid`,
`jp2i`, `uinf`, `rreq`), as more bytes can still change it. `finish`,
Expand Down Expand Up @@ -137,23 +137,21 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
keeps a key of it, not a copy. In a JP2 file a call also reads the boxes
before the codestream again, goes on with the walk to the codestream's
end and the walk of the boxes after it from where the last call left
them, reads each header box after the codestream once, and copies the
boxes the metadata reports (`xml `, `uuid`, `jp2i`, `uinf` and the first
`rreq`). The decoder keeps the walks past the boxes before the
codestream, with 24 to 48 bytes for each box after the codestream that
the metadata reports, for as long as it lives, a change of options
included, and of the other boxes there nothing but, for a last box of
length 0, a fixed record of where its read stopped. A last box of length
0 after the codestream runs to the end of the bytes fed: a `jp2h` there
is read on from its last whole child, and a `colr`, `pclr`, `cmap` or
`cdef` that passed is not read again. Any other box there, or one of
those four that has not passed, is checked on at most its first 258
bytes. That bounds the check, not the copy: such a box of a type the
metadata reports is copied whole into each header returned, so fed 4 KiB
at a time, 16 MiB of a last `xml ` box take 11 s over 4097 calls. A
caller who feeds such a file in small pieces can call `header` less
often. Fed 4 KiB at a time, a file with such a `jp2h` of a million
8-byte children takes about 50 ms in all (#756, #758).
them, reads each header box after the codestream once, and reads each
box the metadata reports (`xml `, `uuid`, `jp2i`, `uinf` and the first
`rreq`) once, into lists that later headers share (see the #769 entries
below). The decoder keeps the walks past the boxes before the
codestream for as long as it lives, a change of options included. Of
the boxes after it, it keeps a 24-byte record for each box the metadata
reports only until the lists hold it, and of the other boxes there
nothing but, for a last box of length 0, a fixed record of where its
read stopped. A last box of length 0 after the codestream runs to the
end of the bytes fed: a `jp2h` there is read on from its last whole
child, and a `colr`, `pclr`, `cmap` or `cdef` that passed is not read
again. Any other box there, or one of those four that has not passed,
is checked on at most its first 258 bytes. Fed 4 KiB at a time, a file
with such a `jp2h` of a million 8-byte children takes about 50 ms in
all (#756, #758).
- For the tiles over a cap, the decoder keeps the walk of the
tile-parts and the counts of the tiles' own headers, about 140 bytes for
each tile counted under its own header and up to 9 bytes for each other
Expand Down Expand Up @@ -231,8 +229,8 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- `IncrementalDecoder::header` reads each box the metadata reports once,
and each later header shares the lists of those boxes and their
payloads. Fed 4 KiB at a time, a last `xml ` box of length 0 of 16 MiB
took about 5 s over 4097 calls and takes about 7 ms, and a million 8-byte
`xml ` boxes took about 7 s over 2049 calls and take under 60 ms, less
took 4.4 to 5.0 s over 4097 calls and takes 6 to 14 ms, and a million
8-byte `xml ` boxes took about 7 s over 2049 calls and take 37 to 75 ms, less
than one read of the file. A payload of up to 15 bytes is held in its
entry and the longer ones read together share one allocation, so the
metadata holds up to about 3.2 bytes for each byte of metadata boxes
Expand Down
7 changes: 4 additions & 3 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ its calls, `header` also holds:
payload of over 15 bytes, its bytes, in an allocation shared with the
payloads read in the same call, behind a 16-byte header (8 on a 32-bit
target). That is at most about 3 bytes for each byte of such boxes fed,
whatever the pieces: 3.1 measured for 8-byte `xml ` boxes, 2.7 for 9-byte ones, and 2.4 for 24-byte
ones fed one box a call.
whatever the pieces: 3.1 measured for 8-byte `xml ` boxes, 2.7 for 9-byte
ones, and 2.4 for 24-byte ones fed one box a call.

The `IncrementalDecoder::header` rustdoc gives each of these in full (see
also "Work and memory" in
Expand Down Expand Up @@ -151,7 +151,8 @@ service](docs/resource-bounds.md#figures-for-a-decode-service).
times the input plus 128 KiB (see "The sample limit" in
[`docs/resource-bounds.md`](docs/resource-bounds.md)). The JP2 box walk
keeps a 24-byte record for each box it reads or reports and for each box
after the codestream that OpenJPEG reads or the metadata reports, and the image's metadata keeps an entry and a copy of
after the codestream that OpenJPEG reads or the metadata reports, and the
image's metadata keeps an entry and a copy of
the payload for each `xml `, `uuid`, `jp2i`, `uinf` and `colr` box: a
payload of up to 15 bytes in the entry, and the longer ones read together in
one allocation. So a file padded with small metadata boxes holds up to about
Expand Down
6 changes: 4 additions & 2 deletions docs/incremental-decoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,10 @@ them whole, into lists kept with the walk after the codestream
(`jp2::Lists`), and each later header shares those lists (`MetadataList`)
and their payloads (`Payload`). A list is a table of blocks of 64 entries,
each block set once, so a header that shares it keeps what it had while a
later call adds blocks after it. Fed 4 KiB at a time, a last `xml ` box of
length 0 of 16 MiB takes 7 to 9 ms over 4097 calls, about one read of the
later call adds blocks after it. An old header also keeps alive the blocks that
later calls set in its shared table, at most about twice its own blocks. Fed 4
KiB at a time, a last `xml ` box of length 0 of 16 MiB takes 6 to 14 ms over
4097 calls, about one read of the
file (4.4 to 5.0 s before, when each call copied it), and a million 8-byte
`xml ` boxes take 37 to 75 ms over 2049 calls, under one read of the file
(about 7 s before, when each call built the lists of every box so far). So a
Expand Down
15 changes: 8 additions & 7 deletions src/incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ impl IncrementalDecoder {
/// `jp2i`, `uinf`, `rreq`) is left out of the header, as more bytes can
/// still change it; [`finish`](Self::finish) and [`refine`](Self::refine)
/// report it. Fed 4 KiB at a time, a last `xml ` box of length 0 of 16 MiB
/// takes 6 to 12 ms over 4097 calls, one to a few reads of the file, most
/// takes 6 to 14 ms over 4097 calls, one to a few reads of the file, most
/// of it the calls' own cost of about 2 µs each, and a million 8-byte
/// `xml ` boxes take 37 to 75 ms over 2049 calls, under one read. So a call
/// costs the main header, the boxes before the codestream, and the child of
Expand Down Expand Up @@ -511,12 +511,13 @@ impl IncrementalDecoder {
/// bytes (40 for a `uuid`, 56 for a `uinf`; on a 32-bit target 20, and 36
/// for both) and, for a payload of over 15 bytes, its bytes, in an
/// allocation shared with the payloads read in the same call, behind a
/// 16-byte header (8 on a 32-bit target). That is about 3 bytes at most for each byte of such
/// boxes fed, whatever the pieces: a million 8-byte `xml ` boxes after the
/// codestream keep 25.8 MB, 3.1 bytes for each byte of them, and a million
/// of 9 bytes 2.7, fed 4 KiB or one box at a time. Fed 4 KiB at a time, the
/// 2049 calls on the 8-byte boxes take under one read of the file in all. A
/// call made while another thread is in one reads from the start.
/// 16-byte header (8 on a 32-bit target). That is about 3 bytes at most
/// for each byte of such boxes fed, whatever the pieces: a million 8-byte
/// `xml ` boxes after the codestream keep 25.8 MB, 3.1 bytes for each byte
/// of them, and a million of 9 bytes 2.7, fed 4 KiB or one box at a time.
/// Fed 4 KiB at a time, the 2049 calls on the 8-byte boxes take under one
/// read of the file in all. A call made while another thread is in one
/// reads from the start.
pub fn header(&self) -> Result<Option<Header>> {
// A call on another thread holding the lock reads from the start. A
// call that panicked may have left the walk half done, so it is
Expand Down
8 changes: 4 additions & 4 deletions src/pipeline/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,10 +808,10 @@ impl Over {
/// of 24 bytes (40 for a `uuid`, 56 for a `uinf`; on a 32-bit target 20, and
/// 36 for both) and, for a payload of over 15 bytes, its bytes in a chunk
/// shared with the payloads read in the same call, behind a 16-byte header
/// (8 on a 32-bit target). So about 3 bytes at most for each byte of those boxes fed, whatever the
/// pieces: a million 8-byte `xml ` boxes keep 25.8 MB, 3.1 bytes for each
/// byte of them, and a million of 9 bytes 2.7, fed 4 KiB or one box at a
/// time.
/// (8 on a 32-bit target). So about 3 bytes at most for each byte of those
/// boxes fed, whatever the pieces: a million 8-byte `xml ` boxes keep 25.8
/// MB, 3.1 bytes for each byte of them, and a million of 9 bytes 2.7, fed 4
/// KiB or one box at a time.
#[derive(Default)]
pub(crate) struct HeaderCache {
/// Boxed, so a decoder with nothing kept holds a pointer.
Expand Down
4 changes: 2 additions & 2 deletions tests/support/header_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ pub fn oracle(prefix: &[u8], options: &DecodeOptions) -> String {
/// malformed. A box read whole by its length reads the same. So an entry is
/// left out where it is the last of its list and the read of `prefix` and a
/// zero byte gives another entry there, or none. The lists have no public
/// constructor, so the entry is left out of the text, which lists them as a
/// slice's `Debug` does.
/// constructor but `Default` (an empty list), so the entry is left out of the
/// text, which lists them as a slice's `Debug` does.
pub fn without_open_box(header: &Header, prefix: &[u8], lenient: &DecodeOptions) -> String {
let mut text = format!("{header:?}");
let Some(jp2) = header.jp2.as_ref() else {
Expand Down
Loading