Skip to content

Speed up decode_bounded on grid HEICs - #41

Merged
laurenspriem merged 3 commits into
ente:mainfrom
laurens-pilot:bounded-heic-overhead
Nov 25, 2026
Merged

laurenspriem merged 3 commits into
ente:mainfrom
laurens-pilot:bounded-heic-overhead

Conversation

@laurens-pilot

Copy link
Copy Markdown

On grid HEICs, decode_bounded took up to 1.29× as long as the regular decoder, although profiling showed it used only about 9% more CPU. It decoded tiles in batches and waited for the whole batch before starting the next one, so on phones with big and little cores every batch ran at the speed of its slowest tile. It also copied each tile into the output one pixel at a time on the calling thread.

Tiles now go through an ordered sliding window: as soon as the oldest tile is placed, the next one is read and started, with the same number of tiles in flight and the same memory accounting. Unrotated tiles are copied a row at a time, and rotated or mirrored tiles pixel by pixel in blocks of 16 rows, using precomputed strides.

Bounded decode time as a multiple of the regular decoder on a Pixel 10 (USB), medians of 3 alternating rounds of 5 decodes:

HEIC main this PR
iPhone 13 Pro Max, 12 MP 1.16× 0.68×
iPhone 15, 24 MP 1.16× 0.65×
iPhone 17 Pro, 12 MP 1.14× 0.62×
Galaxy S20, 12 MP 1.25× 0.70×
Galaxy Tab S9, 13 MP 1.29× 0.77×
56 MP panorama, downscaled to 6000 px 0.78× 0.56×
8 MP, not a grid 0.97× 0.96×

On a Samsung M36, an earlier version of this PR with a less eager scheduler measured 0.77–0.96×, against 0.96–1.11× on main.

Output is unchanged. On 307 HEIC, HEIF and AVIF files, including the bounded oracle corpus, at a max side of 6000 and 97, every decode gave the same dimensions and pixel hash as main and every refusal the same error, with and without parallel-grid and at budgets down to 40 MiB.

Not measured on an iPhone or in the app.

Source fork PR: laurens-pilot#29

@laurenspriem

Decode tiles through an ordered sliding window instead of batches that
wait for their slowest tile, and copy unscaled tiles into the output a
row at a time (or in 16-row blocks when rotated or mirrored) using
precomputed strides.
# Conflicts:
#	src/bounded.rs
#	src/bounded/resample.rs
@laurens-pilot

Copy link
Copy Markdown
Author

@codex review

@laurenspriem
laurenspriem merged commit 5b0a6af into ente:main Nov 25, 2026
3 checks passed
laurenspriem added a commit to ente/ente that referenced this pull request Nov 26, 2026
Update `ente-image`'s HEIC decoder pin from `5fbe1d2` to
[`5b0a6af`](ente/heic-decoder@5fbe1d2...5b0a6af).
It brings:

- Faster bounded decoding of grid HEICs (ente/heic-decoder#41). Measured
there on a Pixel 10, `decode_bounded` took 0.62–0.77× as long as the
regular decoder on 12–24 MP phone HEICs, against 1.14–1.29× before. This
is the path ML indexing, face thumbnails, OCR and the internal Android
HEIC viewer use.
- `decode_bounded` now decodes HEICs with an odd-origin crop or an
odd-sized rotation or mirror instead of refusing them
(ente/heic-decoder#39).
- The regular decoder now matches libheif on those crops, rotations and
mirrors in HEIC and AVIF, converting chroma to 4:4:4 before the
transform (ente/heic-decoder#37, #38, #40). A grid whose tiles need
different conversions now fails, as it does in libheif.
- AVIF items without a `colr` nclx property now use the AV1 stream's
range and matrix (ente/heic-decoder#40).

On 309 HEIC, HEIF and AVIF files (ente's test fixtures, phone HEICs,
libheif's examples, fuzz and stress files, and heic-decoder's generated
corpora), both decoders give the same pixels or the same refusal under
both pins on 294, including every real phone photo. The other 15 are
generated odd-crop and odd-size rotation and mirror files: the regular
decoder now gives different pixels, and `decode_bounded` decodes them
instead of refusing, with exactly the regular decoder's output.

Source fork PR: laurens-pilot#308

@laurenspriem

Co-authored-by: laurenspriem <laurens-pilot-git@lrns.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants