From c3d93a7072e80ad87f099738ac0ec37d0bbd31a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 6 Sep 2026 08:42:17 +0300 Subject: [PATCH] perf(av1): settle an all-zero transform block before the scan-order search Finding the end of block walks the scan backwards through one dependent grid load per position, so an all-zero block pays the whole scan -- once per priced candidate. A straight-line all-zero test runs first: it vectorises, and a block that codes anything at all almost always codes its DC, so the test stops at the first element. Byte-exact on all twelve ladder points; 1080p ladder -0.41% instructions:u (110.14G -> 109.69G, ABAB x2). --- crates/ec-av1/src/tile.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 660fd7ae..5b55e7d4 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -2266,10 +2266,19 @@ fn write_coeffs( ec_rng_trace(|| format!("EC_PLANE plane={plane} tell_before={}", enc.tell())); } let side = coding.side; - let eob = scan - .iter() - .rposition(|&pos| grid[pos as usize] != 0) - .map_or(0, |i| i + 1); + // The scan-order search reads the grid in scan order, one dependent load + // per position, and an all-zero block makes it walk every one of them -- + // which the pricer does for candidate after candidate. A straight-line + // pass settles that case first: it vectorises, and a block that codes + // anything at all almost always codes its DC, so it stops at the first + // element. + let eob = if grid.iter().all(|&level| level == 0) { + 0 + } else { + scan.iter() + .rposition(|&pos| grid[pos as usize] != 0) + .map_or(0, |i| i + 1) + }; enc.symbol(usize::from(eob == 0), &mut coding.txb_skip[skip_ctx]); if plane.is_some() { ec_rng_trace(|| {