Loop restoration stops re-reading its clamp and re-allocating
Browse files at this commita88f7f4920ab3b7b18e25c7335a1de12327cbb7eParent: e2f820e- perf
- loop restoration stops re-reading its clamp and re-allocating
1 changed file +47 -8
crates/ec-av1/src/restoration.rsmodified+47 -8
| @@ -562,7 +562,11 @@ fn apply_wiener_stripe( | |||
| 562 | let wiener_bias = 1i64 << (bd + 3); | 562 | let wiener_bias = 1i64 << (bd + 3); |
| 563 | let wiener_limit = 1i64 << (bd + 5); | 563 | let wiener_limit = 1i64 << (bd + 5); |
| 564 | let rows = h + 6; | 564 | let rows = h + 6; |
| 565 | - let mut inter = vec![0i32; rows * w]; | 565 | + // lane-perf7: reused across stripes, as the sgrproj `ab` grids are -- |
| 566 | + // this was a fresh allocation per stripe. Every entry is written below. | ||
| 567 | + let mut inter = WIENER_INTER_SCRATCH.with(|c| std::mem::take(&mut *c.borrow_mut())); | ||
| 568 | + inter.clear(); | ||
| 569 | + inter.resize(rows * w, 0i32); | ||
| 566 | for r in 0..rows { | 570 | for r in 0..rows { |
| 567 | let row = v_start as i64 - 3 + r as i64; | 571 | let row = v_start as i64 - 3 + r as i64; |
| 568 | for c in 0..w { | 572 | for c in 0..w { |
| @@ -589,15 +593,30 @@ fn apply_wiener_stripe( | |||
| 589 | inter[r * w + c] = round2(sum, 3).clamp(-wiener_bias, wiener_limit - 1 - wiener_bias) as i32; | 593 | inter[r * w + c] = round2(sum, 3).clamp(-wiener_bias, wiener_limit - 1 - wiener_bias) as i32; |
| 590 | } | 594 | } |
| 591 | } | 595 | } |
| 596 | + // lane-perf7: the clamp bound is the frame's, so it is read once here | ||
| 597 | + // instead of taking a thread-local read per output sample. | ||
| 598 | + let smax = i64::from(crate::decode::sample_max()); | ||
| 592 | for r in 0..h { | 599 | for r in 0..h { |
| 593 | for c in 0..w { | 600 | for c in 0..w { |
| 594 | let mut sum = 0i64; | 601 | let mut sum = 0i64; |
| 595 | for (t, &tap) in info.vfilter.iter().enumerate() { | 602 | for (t, &tap) in info.vfilter.iter().enumerate() { |
| 596 | sum += tap as i64 * inter[(r + t) * w + c] as i64; | 603 | sum += tap as i64 * inter[(r + t) * w + c] as i64; |
| 597 | } | 604 | } |
| 598 | - out[(v_start + r) * stride + h_start + c] = round2(sum, 11).clamp(0, crate::decode::sample_max() as i64) as u16; | 605 | + out[(v_start + r) * stride + h_start + c] = round2(sum, 11).clamp(0, smax) as u16; |
| 599 | } | 606 | } |
| 600 | } | 607 | } |
| 608 | + WIENER_INTER_SCRATCH.with(|c| *c.borrow_mut() = inter); | ||
| 609 | +} | ||
| 610 | + | ||
| 611 | +thread_local! { | ||
| 612 | + /// [`apply_wiener_stripe`]'s horizontal-pass intermediate, kept across | ||
| 613 | + /// calls so a stripe does not allocate (lane-perf7). | ||
| 614 | + static WIENER_INTER_SCRATCH: std::cell::RefCell<Vec<i32>> = | ||
| 615 | + const { std::cell::RefCell::new(Vec::new()) }; | ||
| 616 | + /// [`compute_ab`]'s two `(A, B)` grids, likewise (lane-perf7). | ||
| 617 | + #[allow(clippy::type_complexity)] | ||
| 618 | + static SGR_AB_SCRATCH: std::cell::RefCell<(Vec<(i32, i64)>, Vec<(i32, i64)>)> = | ||
| 619 | + const { std::cell::RefCell::new((Vec::new(), Vec::new())) }; | ||
| 601 | } | 620 | } |
| 602 | 621 | ||
| 603 | /// `av1_x_by_xplus1` (`restoration.c`): `round(256*z/(z+1))` for `z` in | 622 | /// `av1_x_by_xplus1` (`restoration.c`): `round(256*z/(z+1))` for `z` in |
| @@ -669,10 +688,17 @@ fn compute_ab( | |||
| 669 | h: usize, | 688 | h: usize, |
| 670 | r: i32, | 689 | r: i32, |
| 671 | s: i32, | 690 | s: i32, |
| 672 | -) -> Vec<(i32, i64)> { | 691 | + // lane-perf7: a reused scratch buffer. This used to be a fresh |
| 692 | + // `vec![(0, 0); (w + 2) * (h + 2)]` per call (two calls per sgrproj | ||
| 693 | + // stripe, ~70 KB each at a 64-wide unit), i.e. an allocation whose every | ||
| 694 | + // page then faulted in on first touch. Every entry is written below, so | ||
| 695 | + // the resize's fill value is never read. | ||
| 696 | + ab: &mut Vec<(i32, i64)>, | ||
| 697 | +) { | ||
| 673 | let gw = w + 2; | 698 | let gw = w + 2; |
| 674 | let gh = h + 2; | 699 | let gh = h + 2; |
| 675 | - let mut ab = vec![(0i32, 0i64); gw * gh]; | 700 | + ab.clear(); |
| 701 | + ab.resize(gw * gh, (0i32, 0i64)); | ||
| 676 | let n = ((2 * r + 1) * (2 * r + 1)) as i64; | 702 | let n = ((2 * r + 1) * (2 * r + 1)) as i64; |
| 677 | // lane-perf6: the box sums are separable. Summing every grid point's own | 703 | // lane-perf6: the box sums are separable. Summing every grid point's own |
| 678 | // (2r+1)^2 window called `lr_sample` -- whose stripe-boundary test is the | 704 | // (2r+1)^2 window called `lr_sample` -- whose stripe-boundary test is the |
| @@ -738,7 +764,6 @@ fn compute_ab( | |||
| 738 | ab[gi * gw + gj] = (a_val, b_val); | 764 | ab[gi * gw + gj] = (a_val, b_val); |
| 739 | } | 765 | } |
| 740 | } | 766 | } |
| 741 | - ab | ||
| 742 | } | 767 | } |
| 743 | 768 | ||
| 744 | /// Self-guided pixel filter (spec 7.17.3, libaom `sgrproj_filter_stripe` -> | 769 | /// Self-guided pixel filter (spec 7.17.3, libaom `sgrproj_filter_stripe` -> |
| @@ -769,8 +794,18 @@ fn apply_sgrproj_stripe( | |||
| 769 | let gw = w + 2; | 794 | let gw = w + 2; |
| 770 | let idx = |gi: i64, gj: i64| -> usize { (gi + 1) as usize * gw + (gj + 1) as usize }; | 795 | let idx = |gi: i64, gj: i64| -> usize { (gi + 1) as usize * gw + (gj + 1) as usize }; |
| 771 | 796 | ||
| 772 | - let ab0 = (r0 > 0).then(|| compute_ab(cdef, deblocked, stride, plane_w, plane_h, h_start, v_start, v_end, w, h, r0, s0)); | 797 | + let (mut buf0, mut buf1) = SGR_AB_SCRATCH.with(|c| { |
| 773 | - let ab1 = (r1 > 0).then(|| compute_ab(cdef, deblocked, stride, plane_w, plane_h, h_start, v_start, v_end, w, h, r1, s1)); | 798 | + let mut m = c.borrow_mut(); |
| 799 | + (std::mem::take(&mut m.0), std::mem::take(&mut m.1)) | ||
| 800 | + }); | ||
| 801 | + if r0 > 0 { | ||
| 802 | + compute_ab(cdef, deblocked, stride, plane_w, plane_h, h_start, v_start, v_end, w, h, r0, s0, &mut buf0); | ||
| 803 | + } | ||
| 804 | + if r1 > 0 { | ||
| 805 | + compute_ab(cdef, deblocked, stride, plane_w, plane_h, h_start, v_start, v_end, w, h, r1, s1, &mut buf1); | ||
| 806 | + } | ||
| 807 | + let ab0 = (r0 > 0).then_some(&buf0); | ||
| 808 | + let ab1 = (r1 > 0).then_some(&buf1); | ||
| 774 | 809 | ||
| 775 | // r8: call-unique debug dump -- r6's coordinate-only gate (`v_start==60`) | 810 | // r8: call-unique debug dump -- r6's coordinate-only gate (`v_start==60`) |
| 776 | // matched several unrelated calls across the gate's attempt sweep, so its | 811 | // matched several unrelated calls across the gate's attempt sweep, so its |
| @@ -806,6 +841,9 @@ fn apply_sgrproj_stripe( | |||
| 806 | xq[1] = (1 << SGRPROJ_PRJ_BITS) - xq[0] - info.xqd[1]; | 841 | xq[1] = (1 << SGRPROJ_PRJ_BITS) - xq[0] - info.xqd[1]; |
| 807 | } | 842 | } |
| 808 | 843 | ||
| 844 | + // lane-perf7: as in `apply_wiener_stripe`, the clamp bound is read once | ||
| 845 | + // per stripe instead of per output sample. | ||
| 846 | + let smax = i64::from(crate::decode::sample_max()); | ||
| 809 | for i in 0..h as i64 { | 847 | for i in 0..h as i64 { |
| 810 | for j in 0..w as i64 { | 848 | for j in 0..w as i64 { |
| 811 | let dgd = lr_sample( | 849 | let dgd = lr_sample( |
| @@ -879,10 +917,11 @@ fn apply_sgrproj_stripe( | |||
| 879 | ); | 917 | ); |
| 880 | } | 918 | } |
| 881 | } | 919 | } |
| 882 | - let out_v = round2(v, SGRPROJ_PRJ_BITS as u32 + SGRPROJ_RST_BITS).clamp(0, crate::decode::sample_max() as i64) as u16; | 920 | + let out_v = round2(v, SGRPROJ_PRJ_BITS as u32 + SGRPROJ_RST_BITS).clamp(0, smax) as u16; |
| 883 | out[(v_start as i64 + i) as usize * stride + (h_start as i64 + j) as usize] = out_v; | 921 | out[(v_start as i64 + i) as usize * stride + (h_start as i64 + j) as usize] = out_v; |
| 884 | } | 922 | } |
| 885 | } | 923 | } |
| 924 | + SGR_AB_SCRATCH.with(|c| *c.borrow_mut() = (buf0, buf1)); | ||
| 886 | } | 925 | } |
| 887 | 926 | ||
| 888 | /// Chunks one restoration unit's `[v_start, v_end)` extent into 64-row | 927 | /// Chunks one restoration unit's `[v_start, v_end)` extent into 64-row |