forge
Explore
Log in

Don't you dare go Hollow

Memoise the deblocker's level LUT per plane and pass

Browse files at this commit

4dff42ae5214a3b0edc70409da460f29626cbc69Ahmet Kaan GümüşParent: 7fd0e67
  • perf
    • memoise the deblocker's level LUT per plane and pass
Show full message
perf(av1): memoise the deblocker's level LUT per plane and pass

`EdgeCtx::new` builds `lf_level` over the whole (base level, ref frame)
domain -- 960 calls -- and the row pipeline builds an `EdgeCtx` per
superblock-row band instead of per frame, which turned ~20 builds a frame
into ~680 and cost +1.0% instructions:u on seg4k.

The table is a pure function of the frame's LoopFilterParams, the plane, the
pass and segment 0's own SEG_LVL_ALT_LF_* datum (the only thing `lf_level`
reads out of the FrameCtx at segment_id = 0), so it is memoised per thread
under exactly that key. seg4k at 16/(4,2): instructions:u 407.33 G -> 403.27 G,
i.e. back to the pre-pipeline 403.30 G.
crates/ec-av1/src/decode.rsmodified+49 -11
@@ -17963,6 +17963,54 @@ fn lf_level(
17963 lvl.clamp(0, 63)17963 lvl.clamp(0, 63)
17964 }17964 }
17965 17965
17966+thread_local! {
17967+ /// lane-pipefilt: [`EdgeCtx`]'s level LUT, memoised per thread. The table
17968+ /// is a pure function of the frame's [`LoopFilterParams`], the plane, the
17969+ /// pass and segment 0's own `SEG_LVL_ALT_LF_*` datum (the only thing
17970+ /// [`lf_level`] reads out of the `FrameCtx` at `segment_id = 0`), so one
17971+ /// build per plane and pass serves the whole frame. The pipeline builds
17972+ /// an `EdgeCtx` per superblock-row band rather than per frame, which
17973+ /// turned 20 builds a frame into ~680 -- 960 `lf_level` calls each.
17974+ static LVL_CACHE: std::cell::RefCell<
17975+ Vec<((usize, usize, LoopFilterParams, Option<i32>), [[u8; 15]; 64])>,
17976+ > = const { std::cell::RefCell::new(Vec::new()) };
17977+}
17978+
17979+/// `lf_level` over the whole `(base level, ref frame)` domain -- see
17980+/// [`LVL_CACHE`]. `lf_level`'s own first step is
17981+/// `(base0 + delta_lf).clamp(0, 63)`, so passing `b - base0` pins its base at
17982+/// `b` and the rest of the function is a pure function of `(b, ref_frame)`.
17983+fn lvl_table(
17984+ lf: &LoopFilterParams,
17985+ plane_idx: usize,
17986+ dir: usize,
17987+ base0: i32,
17988+ fctx: &crate::decode::FrameCtx,
17989+) -> [[u8; 15]; 64] {
17990+ let feature = if plane_idx == 0 { SEG_LVL_ALT_LF_Y_V + dir } else { plane_idx + 2 };
17991+ let key = (plane_idx, dir, *lf, seg_feature(0, feature, fctx));
17992+ if let Some(t) = LVL_CACHE.with(|c| {
17993+ c.borrow().iter().find(|(k, _)| *k == key).map(|(_, t)| *t)
17994+ }) {
17995+ return t;
17996+ }
17997+ let mut lvl = [[0u8; 15]; 64];
17998+ for (b, row) in lvl.iter_mut().enumerate() {
17999+ for (r, cell) in row.iter_mut().enumerate() {
18000+ *cell =
18001+ lf_level(lf, plane_idx, dir, (r as i32 - 7) as i8, b as i32 - base0, 0, fctx) as u8;
18002+ }
18003+ }
18004+ LVL_CACHE.with(|c| {
18005+ let mut c = c.borrow_mut();
18006+ if c.len() >= 8 {
18007+ c.remove(0);
18008+ }
18009+ c.push((key, lvl));
18010+ });
18011+ lvl
18012+}
18013+
17966 /// Spec 7.14.2's `set_lpf_parameters`, restricted to the uniform-level,18014 /// Spec 7.14.2's `set_lpf_parameters`, restricted to the uniform-level,
17967 /// no-segmentation, no-delta-LF path and to this decoder's own invariant18015 /// no-segmentation, no-delta-LF path and to this decoder's own invariant
17968 /// that a coded block's transform is always its own full size -- so a TU18016 /// that a coded block's transform is always its own full size -- so a TU
@@ -18015,17 +18063,7 @@ impl<'a> EdgeCtx<'a> {
18015 } else {18063 } else {
18016 lf.level[3]18064 lf.level[3]
18017 });18065 });
18018- // `lf_level`'s own first step is `(base0 + delta_lf).clamp(0, 63)`,18066+ let lvl = lvl_table(lf, plane_idx, dir, base0, fctx);
18019- // so passing `b - base0` pins its base at `b` and the rest of the
18020- // function is a pure function of `(b, ref_frame)`.
18021- let mut lvl = [[0u8; 15]; 64];
18022- for (b, row) in lvl.iter_mut().enumerate() {
18023- for (r, cell) in row.iter_mut().enumerate() {
18024- *cell =
18025- lf_level(lf, plane_idx, dir, (r as i32 - 7) as i8, b as i32 - base0, 0, fctx)
18026- as u8;
18027- }
18028- }
18029 Self {18067 Self {
18030 lf,18068 lf,
18031 n,18069 n,