forge
Explore
Log in

Don't you dare go Hollow

Make the gate hit-counters thread-local so a delta is the test's own

Browse files at this commit

5c21ffafc526a37c71923b9fed85fcda8bed90cfAhmet Kaan GümüşParent: 0169a4e
  • fix
    • make the gate hit-counters thread-local so a delta is the test's own
Show full message
fix(av1): make the gate hit-counters thread-local so a delta is the test's own

The firing counters were process-global atomics and cargo runs tests in
parallel threads, so a gate's `hits() > before` delta could be satisfied by a
SIBLING gate's increments -- the gate would pass without its own feature ever
firing, which is the vacuous-gate failure this batch has already fixed twice by
other means. Eleven gates took that delta without holding the counter lock.

Measured before the fix: the free-partition gate reported rect_partition_hits=9
and partab_hits=2 alone, and 18 and 4 inside the full suite. Same stream, same
decode, double the count -- the difference was other tests.

Serializing those eleven on the existing lock fixed the correctness and took
the suite from 67 s to over 600 s, because it serializes every slow aomenc gate
against every other. Thread-local counters give the same isolation with no
serialization at all: the decoder runs single-threaded, so each test thread's
counts are inherently its own, and the added locks come back out.

After: the same gate reports 9 and 2 in both contexts. Full lib suite 232
passed / 0 failed in 67 s.
crates/ec-av1/src/decode.rsmodified+168 -92
@@ -56,18 +56,24 @@ const BLOCK_MI: u32 = 8;
5656 /// tests read (before/after, not the absolute value) to prove a stream
5757 /// actually exercised the filter-intra predictor rather than silently
5858 /// skipping it.
59-static FILTER_INTRA_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
59+thread_local! {
60+ static FILTER_INTRA_HITS: std::cell::Cell<usize> =
61+ const { std::cell::Cell::new(0) };
62+}
6063
6164 /// Current value of [`FILTER_INTRA_HITS`].
6265 pub(crate) fn filter_intra_hits() -> usize {
63- FILTER_INTRA_HITS.load(std::sync::atomic::Ordering::Relaxed)
66+ FILTER_INTRA_HITS.with(|c| c.get())
6467 }
6568
6669 /// How many 4-pixel deblocking edge groups [`edge_params`] has actually
6770 /// selected for filtering, across every call in the process -- the same
6871 /// before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream
6972 /// exercised `apply_deblock` rather than every edge landing on level 0.
70-static DEBLOCK_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
73+thread_local! {
74+ static DEBLOCK_HITS: std::cell::Cell<usize> =
75+ const { std::cell::Cell::new(0) };
76+}
7177 /// lane-comppin r4: decode-order picture counter for `EC_AV1_PREFILT_DUMP`,
7278 /// mirroring stream.rs's `pictures_decoded` so the two dumps index
7379 /// identically against aomdec's own `ec_frame_idx` in decodeframe.c.
@@ -76,7 +82,7 @@ static PREFILT_PICTURE_IDX: std::sync::atomic::AtomicUsize =
7682
7783 /// Current value of [`DEBLOCK_HITS`].
7884 pub(crate) fn deblock_hits() -> usize {
79- DEBLOCK_HITS.load(std::sync::atomic::Ordering::Relaxed)
85+ DEBLOCK_HITS.with(|c| c.get())
8086 }
8187
8288 /// How many [`read_tx_size`] reads resolved a `tx_depth` strictly less than
@@ -85,11 +91,14 @@ pub(crate) fn deblock_hits() -> usize {
8591 /// actually exercised a *split* transform under `TxMode::Select` rather than
8692 /// every block coincidentally resolving `depth=0` (indistinguishable from
8793 /// `TxMode::Largest` pixel-wise).
88-static TX_DEPTH_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
94+thread_local! {
95+ static TX_DEPTH_HITS: std::cell::Cell<usize> =
96+ const { std::cell::Cell::new(0) };
97+}
8998
9099 /// Current value of [`TX_DEPTH_HITS`].
91100 pub(crate) fn tx_depth_hits() -> usize {
92- TX_DEPTH_HITS.load(std::sync::atomic::Ordering::Relaxed)
101+ TX_DEPTH_HITS.with(|c| c.get())
93102 }
94103
95104 /// How many [`read_coeffs`] reads resolved a `tx_type` outside `TX_CLASS_2D`
@@ -98,24 +107,33 @@ pub(crate) fn tx_depth_hits() -> usize {
98107 /// [`TX_DEPTH_HITS`], proving a stream actually exercised the class-split
99108 /// `eob_pt`/1D-neighbour context path rather than every block coincidentally
100109 /// landing on `DCT_DCT`.
101-static TX_CLASS1_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
110+thread_local! {
111+ static TX_CLASS1_HITS: std::cell::Cell<usize> =
112+ const { std::cell::Cell::new(0) };
113+}
102114
103115 /// How many `uv_mode` reads resolved to a directional chroma mode (neither
104116 /// `DC_PRED` nor `UV_CFL_PRED`), across every call in the process -- the same
105117 /// before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream
106118 /// actually exercised chroma's own directional predictor.
107-static DIRECTIONAL_UV_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
119+thread_local! {
120+ static DIRECTIONAL_UV_HITS: std::cell::Cell<usize> =
121+ const { std::cell::Cell::new(0) };
122+}
108123
109124 /// Current value of [`DIRECTIONAL_UV_HITS`].
110125 pub(crate) fn directional_uv_hits() -> usize {
111- DIRECTIONAL_UV_HITS.load(std::sync::atomic::Ordering::Relaxed)
126+ DIRECTIONAL_UV_HITS.with(|c| c.get())
112127 }
113128
114129 /// How many `angle_delta`/`angle_delta_uv` symbols this decoder has read as
115130 /// something other than [`ANGLE_DELTA_ZERO`], across every call in the
116131 /// process -- the same before/after counter pattern as [`FILTER_INTRA_HITS`],
117132 /// proving a stream actually exercised a nonzero angle delta.
118-static ANGLE_DELTA_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
133+thread_local! {
134+ static ANGLE_DELTA_HITS: std::cell::Cell<usize> =
135+ const { std::cell::Cell::new(0) };
136+}
119137
120138 /// How many [`read_single_ref`] reads resolved to a reference other than
121139 /// `LAST_FRAME`, across every call in the process -- the same before/after
@@ -123,11 +141,14 @@ static ANGLE_DELTA_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::Ato
123141 /// exercised a non-`LAST_FRAME` reference (`GOLDEN_FRAME`, this decoder's
124142 /// only other supported one so far) rather than every block coincidentally
125143 /// resolving `LAST_FRAME`.
126-static NON_LAST_REF_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
144+thread_local! {
145+ static NON_LAST_REF_HITS: std::cell::Cell<usize> =
146+ const { std::cell::Cell::new(0) };
147+}
127148
128149 /// Current value of [`NON_LAST_REF_HITS`].
129150 pub(crate) fn non_last_ref_hits() -> usize {
130- NON_LAST_REF_HITS.load(std::sync::atomic::Ordering::Relaxed)
151+ NON_LAST_REF_HITS.with(|c| c.get())
131152 }
132153
133154 /// Per-reference [`NON_LAST_REF_HITS`] breakdown (lane-av1refs): one counter
@@ -136,51 +157,80 @@ pub(crate) fn non_last_ref_hits() -> usize {
136157 /// can prove THAT reference fired rather than any non-`LAST_FRAME` one --
137158 /// `NON_LAST_REF_HITS` alone cannot tell a `LAST2_FRAME` gate from a
138159 /// `GOLDEN_FRAME` draw it did not ask for.
139-static LAST2_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
140-static LAST3_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
141-static GOLDEN_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
142-static BWDREF_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
143-static ALTREF2_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
144-static ALTREF_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
160+thread_local! {
161+ static LAST2_HITS: std::cell::Cell<usize> =
162+ const { std::cell::Cell::new(0) };
163+}
164+thread_local! {
165+ static LAST3_HITS: std::cell::Cell<usize> =
166+ const { std::cell::Cell::new(0) };
167+}
168+thread_local! {
169+ static GOLDEN_HITS: std::cell::Cell<usize> =
170+ const { std::cell::Cell::new(0) };
171+}
172+thread_local! {
173+ static BWDREF_HITS: std::cell::Cell<usize> =
174+ const { std::cell::Cell::new(0) };
175+}
176+thread_local! {
177+ static ALTREF2_HITS: std::cell::Cell<usize> =
178+ const { std::cell::Cell::new(0) };
179+}
180+thread_local! {
181+ static ALTREF_HITS: std::cell::Cell<usize> =
182+ const { std::cell::Cell::new(0) };
183+}
145184
146185 /// How many [`read_comp_mode`] reads resolved `COMPOUND_REFERENCE` (lane-av1comp)
147186 /// -- proves a `reference_select` stream actually reached a block that picked
148187 /// compound, not just that the header bit was set. Every such block is
149188 /// refused after this fires ([`decode_inter_block`]/[`decode_inter_block8`]),
150189 /// so this only ever counts attempted reads, never a completed decode.
151-static COMP_MODE_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
190+thread_local! {
191+ static COMP_MODE_HITS: std::cell::Cell<usize> =
192+ const { std::cell::Cell::new(0) };
193+}
152194
153195 /// Current value of [`COMP_MODE_HITS`].
154196 pub(crate) fn comp_mode_hits() -> usize {
155- COMP_MODE_HITS.load(std::sync::atomic::Ordering::Relaxed)
197+ COMP_MODE_HITS.with(|c| c.get())
156198 }
157199
158200 /// How many blocks decoded `skip_mode == 1` (lane-av1comp round 14) --
159201 /// proves a real `skip_mode_present` stream actually reached a block that
160202 /// picked it, not just that the frame header bit was set.
161-static SKIP_MODE_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
203+thread_local! {
204+ static SKIP_MODE_HITS: std::cell::Cell<usize> =
205+ const { std::cell::Cell::new(0) };
206+}
162207
163208 /// Current value of [`SKIP_MODE_HITS`].
164209 pub(crate) fn skip_mode_hits() -> usize {
165- SKIP_MODE_HITS.load(std::sync::atomic::Ordering::Relaxed)
210+ SKIP_MODE_HITS.with(|c| c.get())
166211 }
167212
168213 /// lane-motionmode: how many blocks actually decoded `obmc_selected == true`
169214 /// (as opposed to just reaching an eligible `read_motion_mode` symbol read)
170215 /// -- the gate's own proof that a real aomenc `--enable-obmc=1` stream
171216 /// reached [`obmc_blend`], not just the header bit.
172-static OBMC_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
217+thread_local! {
218+ static OBMC_HITS: std::cell::Cell<usize> =
219+ const { std::cell::Cell::new(0) };
220+}
173221
174222 /// Current value of [`OBMC_HITS`].
175223 pub(crate) fn obmc_hits() -> usize {
176- OBMC_HITS.load(std::sync::atomic::Ordering::Relaxed)
224+ OBMC_HITS.with(|c| c.get())
177225 }
178226
179227 /// lane-motionmode round 3: same proof as [`OBMC_HITS`], narrowed to
180228 /// `decode_inter_block8`'s own 8x8-leaf `read_motion_mode` -- a subset of
181229 /// [`OBMC_HITS`] (both fire together on an 8x8 hit), lets the gate tell an
182230 /// 8x8-leaf OBMC block apart from a 16x16+ one.
183-static OBMC_HITS_8: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
231+thread_local! {
232+ static OBMC_HITS_8: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
233+}
184234
185235 /// lane-warp round 1: how many blocks resolved `motion_mode_allowed` to
186236 /// `WARPED_CAUSAL`-eligible (3-symbol read, `num_proj_ref >= 1` under
@@ -188,21 +238,27 @@ static OBMC_HITS_8: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUs
188238 /// -- the gate's proof that a fixture really exercises the alphabet this
189239 /// round changed, even though the block itself still refuses (warp
190240 /// estimation/filter not ported).
191-static WARP_SELECTED_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
241+thread_local! {
242+ static WARP_SELECTED_HITS: std::cell::Cell<usize> =
243+ const { std::cell::Cell::new(0) };
244+}
192245
193246 /// Current value of [`WARP_SELECTED_HITS`].
194247 pub(crate) fn warp_selected_hits() -> usize {
195- WARP_SELECTED_HITS.load(std::sync::atomic::Ordering::Relaxed)
248+ WARP_SELECTED_HITS.with(|c| c.get())
196249 }
197250
198251 /// How many blocks decoded `interintra == 1` with the non-wedge blended
199252 /// prediction applied (lane-interintra r1) -- the gate's proof that a
200253 /// stream actually exercised interintra.
201-static INTERINTRA_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
254+thread_local! {
255+ static INTERINTRA_HITS: std::cell::Cell<usize> =
256+ const { std::cell::Cell::new(0) };
257+}
202258
203259 /// Current value of [`INTERINTRA_HITS`].
204260 pub(crate) fn interintra_hits() -> usize {
205- INTERINTRA_HITS.load(std::sync::atomic::Ordering::Relaxed)
261+ INTERINTRA_HITS.with(|c| c.get())
206262 }
207263
208264 /// How many blocks decoded `comp_group_idx == 1` (masked COMPOUND_REFERENCE,
@@ -210,39 +266,47 @@ pub(crate) fn interintra_hits() -> usize {
210266 /// `mask_type` consumed entropy-exact -- lane-maskcomp r1's proof that a
211267 /// gate fixture actually exercises this alphabet, even though the block
212268 /// still refuses (the mask blend itself is unported).
213-static MASKED_COMPOUND_HITS: std::sync::atomic::AtomicUsize =
214- std::sync::atomic::AtomicUsize::new(0);
269+thread_local! {
270+ static MASKED_COMPOUND_HITS: std::cell::Cell<usize> =
271+ const { std::cell::Cell::new(0) };
272+}
215273
216274 /// Current value of [`MASKED_COMPOUND_HITS`].
217275 pub(crate) fn masked_compound_hits() -> usize {
218- MASKED_COMPOUND_HITS.load(std::sync::atomic::Ordering::Relaxed)
276+ MASKED_COMPOUND_HITS.with(|c| c.get())
219277 }
220278
221279 /// How many blocks decoded a real `COMPOUND_WEDGE` block (`comp_group_idx ==
222280 /// 1`, `compound_type == 0`) -- lane-wedge r3's proof a gate fixture
223281 /// actually exercised the wedge blend, not just the DIFFWTD half of
224282 /// `MASKED_COMPOUND_HITS`.
225-static WEDGE_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
283+thread_local! {
284+ static WEDGE_HITS: std::cell::Cell<usize> =
285+ const { std::cell::Cell::new(0) };
286+}
226287
227288 /// Current value of [`WEDGE_HITS`].
228289 pub(crate) fn wedge_hits() -> usize {
229- WEDGE_HITS.load(std::sync::atomic::Ordering::Relaxed)
290+ WEDGE_HITS.with(|c| c.get())
230291 }
231292
232293 /// How many blocks decoded a real wedge-INTERINTRA block
233294 /// (`interintra == 1 && use_wedge_interintra == 1`) -- lane-wii r2's proof
234295 /// a gate fixture exercised the wedge blend (fixed sign 0), not just the
235296 /// smooth arm counted by `INTERINTRA_HITS`.
236-static WII_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
297+thread_local! {
298+ static WII_HITS: std::cell::Cell<usize> =
299+ const { std::cell::Cell::new(0) };
300+}
237301
238302 /// Current value of [`WII_HITS`].
239303 pub(crate) fn wii_hits() -> usize {
240- WII_HITS.load(std::sync::atomic::Ordering::Relaxed)
304+ WII_HITS.with(|c| c.get())
241305 }
242306
243307 /// Current value of [`OBMC_HITS_8`].
244308 pub(crate) fn obmc_hits_8() -> usize {
245- OBMC_HITS_8.load(std::sync::atomic::Ordering::Relaxed)
309+ OBMC_HITS_8.with(|c| c.get())
246310 }
247311
248312 /// lane-rectgate r1: how many 32x32 quadrants actually decoded a
@@ -252,47 +316,56 @@ pub(crate) fn obmc_hits_8() -> usize {
252316 /// never writes" refusal below) -- the gate's proof that a free-partition
253317 /// aomenc stream actually exercised a non-`NONE`/`SPLIT` partition rather than
254318 /// coincidentally never selecting one.
255-static EXTENDED_PARTITION_HITS: std::sync::atomic::AtomicUsize =
256- std::sync::atomic::AtomicUsize::new(0);
319+thread_local! {
320+ static EXTENDED_PARTITION_HITS: std::cell::Cell<usize> =
321+ const { std::cell::Cell::new(0) };
322+}
257323
258324 /// Current value of [`EXTENDED_PARTITION_HITS`].
259325 pub(crate) fn extended_partition_hits() -> usize {
260- EXTENDED_PARTITION_HITS.load(std::sync::atomic::Ordering::Relaxed)
326+ EXTENDED_PARTITION_HITS.with(|c| c.get())
261327 }
262328
263329 /// lane-partitions r1: how many 32x32 quadrants decoded a real
264330 /// `PARTITION_HORZ`/`PARTITION_VERT` (two true rectangular strips, unlike
265331 /// `PARTITION_HORZ_B`'s square-context stand-in) -- the free-partition
266332 /// gate's proof this round's arms actually fired.
267-static RECT_PARTITION_HITS: std::sync::atomic::AtomicUsize =
268- std::sync::atomic::AtomicUsize::new(0);
333+thread_local! {
334+ static RECT_PARTITION_HITS: std::cell::Cell<usize> =
335+ const { std::cell::Cell::new(0) };
336+}
269337
270338 /// Current value of [`RECT_PARTITION_HITS`].
271339 pub(crate) fn rect_partition_hits() -> usize {
272- RECT_PARTITION_HITS.load(std::sync::atomic::Ordering::Relaxed)
340+ RECT_PARTITION_HITS.with(|c| c.get())
273341 }
274342
275343 /// lane-rectwire r2: how many `PARTITION_HORZ`/`VERT` strips actually decoded
276344 /// real (non-skip) coefficients through [`read_coeffs_rect`] -- proves the
277345 /// rect coefficient reader itself fired, not just the strip-level partition
278346 /// symbol [`RECT_PARTITION_HITS`] already counts.
279-static RECT_COEFF_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
347+thread_local! {
348+ static RECT_COEFF_HITS: std::cell::Cell<usize> =
349+ const { std::cell::Cell::new(0) };
350+}
280351
281352 /// Current value of [`RECT_COEFF_HITS`].
282353 pub(crate) fn rect_coeff_hits() -> usize {
283- RECT_COEFF_HITS.load(std::sync::atomic::Ordering::Relaxed)
354+ RECT_COEFF_HITS.with(|c| c.get())
284355 }
285356
286357 /// lane-partab r1: how many 32x32 quadrants decoded an AB partition
287358 /// (PARTITION_HORZ_A / VERT_A / VERT_B -- two 16x16 squares plus one
288359 /// 16x32/32x16 strip). Like [`RECT_PARTITION_HITS`], this is the
289360 /// free-partition gate's proof this round's arms actually fired.
290-static PARTAB_HITS: std::sync::atomic::AtomicUsize =
291- std::sync::atomic::AtomicUsize::new(0);
361+thread_local! {
362+ static PARTAB_HITS: std::cell::Cell<usize> =
363+ const { std::cell::Cell::new(0) };
364+}
292365
293366 /// Current value of [`PARTAB_HITS`].
294367 pub(crate) fn partab_hits() -> usize {
295- PARTAB_HITS.load(std::sync::atomic::Ordering::Relaxed)
368+ PARTAB_HITS.with(|c| c.get())
296369 }
297370
298371 /// How many [`read_inter_compound_mode`] reads actually happened
@@ -301,11 +374,14 @@ pub(crate) fn partab_hits() -> usize {
301374 /// mvstack build), not just that `comp_mode` fired. Still refused right
302375 /// after (no MV assignment or MC wired), so this only counts attempted
303376 /// reads.
304-static COMPOUND_MODE_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
377+thread_local! {
378+ static COMPOUND_MODE_HITS: std::cell::Cell<usize> =
379+ const { std::cell::Cell::new(0) };
380+}
305381
306382 /// Current value of [`COMPOUND_MODE_HITS`].
307383 pub(crate) fn compound_mode_hits() -> usize {
308- COMPOUND_MODE_HITS.load(std::sync::atomic::Ordering::Relaxed)
384+ COMPOUND_MODE_HITS.with(|c| c.get())
309385 }
310386
311387 /// How many query blocks folded in at least one temporal MV candidate
@@ -390,7 +466,7 @@ pub(crate) fn ref_hits(ref_frame: i8) -> usize {
390466 ALTREF_FRAME => &ALTREF_HITS,
391467 other => panic!("ref_hits: no per-reference counter for ref_frame {other}"),
392468 };
393- c.load(std::sync::atomic::Ordering::Relaxed)
469+ c.with(|v| v.get())
394470 }
395471
396472 thread_local! {
@@ -418,12 +494,12 @@ pub(crate) fn set_enable_edge_filter(value: bool) {
418494
419495 /// Current value of [`ANGLE_DELTA_HITS`].
420496 pub(crate) fn angle_delta_hits() -> usize {
421- ANGLE_DELTA_HITS.load(std::sync::atomic::Ordering::Relaxed)
497+ ANGLE_DELTA_HITS.with(|c| c.get())
422498 }
423499
424500 /// Current value of [`TX_CLASS1_HITS`].
425501 pub(crate) fn tx_class1_hits() -> usize {
426- TX_CLASS1_HITS.load(std::sync::atomic::Ordering::Relaxed)
502+ TX_CLASS1_HITS.with(|c| c.get())
427503 }
428504 const SUB_MI: u32 = 4;
429505 const MI: usize = 4;
@@ -944,7 +1020,7 @@ fn read_coeffs(
9441020
9451021 let class = TxClass::of(tx_type);
9461022 if class != TxClass::TwoD {
947- TX_CLASS1_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1023+ TX_CLASS1_HITS.with(|c| c.set(c.get() + 1));
9481024 }
9491025 let eob = read_eob(dec, coding, class);
9501026 if trace {
@@ -1894,7 +1970,7 @@ fn read_intra_mode_rect(
18941970 return Err(unsupported("a smooth or paeth chroma mode (round 2)"));
18951971 }
18961972 let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) {
1897- DIRECTIONAL_UV_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1973+ DIRECTIONAL_UV_HITS.with(|c| c.set(c.get() + 1));
18981974 read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED])
18991975 } else {
19001976 0
@@ -1906,7 +1982,7 @@ fn read_intra_mode_rect(
19061982 {
19071983 let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0;
19081984 if use_filter_intra {
1909- FILTER_INTRA_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1985+ FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1));
19101986 let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode);
19111987 filter_intra = Some(fi_mode);
19121988 }
@@ -2020,7 +2096,7 @@ fn decode_block_rect(
20202096 let ctx = tx_size_context_rect(neighbours, (mi_r, mi_c), bw, bh);
20212097 let depth = dec.symbol(&mut cdfs.tx_size_cat2[ctx]);
20222098 if depth != 0 {
2023- TX_DEPTH_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2099+ TX_DEPTH_HITS.with(|c| c.set(c.get() + 1));
20242100 // A split transform is predicted per transform unit, each taking its
20252101 // edges from the previous unit's reconstruction (the square path's own
20262102 // per-TU loop does exactly that). Predicting the whole strip in one
@@ -2217,12 +2293,12 @@ fn decode_block_rect(
22172293 None,
22182294 );
22192295 neighbours.record_rect(at, bw, bh, mode, &[luma_levels, u_levels, v_levels]);
2220- RECT_COEFF_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2296+ RECT_COEFF_HITS.with(|c| c.set(c.get() + 1));
22212297 }
22222298 }
22232299 neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip);
22242300 neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0);
2225- RECT_PARTITION_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2301+ RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1));
22262302 Ok(())
22272303 }
22282304
@@ -2233,7 +2309,7 @@ fn decode_block_rect(
22332309 fn read_angle_delta(dec: &mut SymbolDecoder, cdf: &mut [u16]) -> i32 {
22342310 let symbol = dec.symbol(cdf);
22352311 if symbol != ANGLE_DELTA_ZERO {
2236- ANGLE_DELTA_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2312+ ANGLE_DELTA_HITS.with(|c| c.set(c.get() + 1));
22372313 }
22382314 symbol as i32 - ANGLE_DELTA_ZERO as i32
22392315 }
@@ -2330,7 +2406,7 @@ fn read_intra_mode(
23302406 // so a CFL block (uv_mode==13, outside `V_PRED..=D67_PRED`) already takes
23312407 // the `else` branch below and never reads one either way.
23322408 let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) {
2333- DIRECTIONAL_UV_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2409+ DIRECTIONAL_UV_HITS.with(|c| c.set(c.get() + 1));
23342410 read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED])
23352411 } else {
23362412 0
@@ -2381,7 +2457,7 @@ fn read_intra_mode(
23812457 );
23822458 }
23832459 if use_filter_intra {
2384- FILTER_INTRA_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2460+ FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1));
23852461 let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode);
23862462 if trace {
23872463 eprintln!("TRACE filter_intra_mode value={fi_mode}");
@@ -3709,7 +3785,7 @@ fn read_tx_size(
37093785 _ => unreachable!("decode_block/decode_leaf8 only call this at 8/16/32/64"),
37103786 };
37113787 if depth != 0 {
3712- TX_DEPTH_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
3788+ TX_DEPTH_HITS.with(|c| c.set(c.get() + 1));
37133789 }
37143790 side >> depth
37153791 }
@@ -3845,7 +3921,7 @@ fn edge_params(
38453921 [4, 8, 14, 14, 14][dim]
38463922 };
38473923 let level = if cur_level != 0 { cur_level } else { pv_level };
3848- DEBLOCK_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
3924+ DEBLOCK_HITS.with(|c| c.set(c.get() + 1));
38493925 Some((len, level))
38503926 }
38513927
@@ -5190,7 +5266,7 @@ fn read_single_ref(
51905266 }
51915267 };
51925268 if ref_frame != LAST_FRAME {
5193- NON_LAST_REF_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
5269+ NON_LAST_REF_HITS.with(|c| c.set(c.get() + 1));
51945270 let c = match ref_frame {
51955271 LAST2_FRAME => &LAST2_HITS,
51965272 LAST3_FRAME => &LAST3_HITS,
@@ -5200,7 +5276,7 @@ fn read_single_ref(
52005276 ALTREF_FRAME => &ALTREF_HITS,
52015277 _ => unreachable!("read_single_ref only ever returns LAST_FRAME..=ALTREF_FRAME"),
52025278 };
5203- c.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
5279+ c.with(|v| v.set(v.get() + 1));
52045280 }
52055281 ref_frame
52065282 }
@@ -5851,7 +5927,7 @@ fn read_comp_mode(
58515927 let ctx = reference_mode_ctx(above, left);
58525928 let compound = dec.symbol(&mut cdfs.comp_mode[ctx]) == 1;
58535929 if compound {
5854- COMP_MODE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
5930+ COMP_MODE_HITS.with(|c| c.set(c.get() + 1));
58555931 }
58565932 compound
58575933 }
@@ -5955,7 +6031,7 @@ fn read_inter_compound_mode(
59556031 ref_mv_ctx: usize,
59566032 ) -> u8 {
59576033 let ctx = cdf::COMPOUND_MODE_CTX_MAP[ref_mv_ctx >> 1][new_mv_ctx.min(4)];
5958- COMPOUND_MODE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
6034+ COMPOUND_MODE_HITS.with(|c| c.set(c.get() + 1));
59596035 dec.symbol(&mut cdfs.inter_compound_mode[ctx]) as u8
59606036 }
59616037
@@ -6235,7 +6311,7 @@ fn decode_inter_block(
62356311 );
62366312 }
62376313 if skip_mode {
6238- SKIP_MODE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
6314+ SKIP_MODE_HITS.with(|c| c.set(c.get() + 1));
62396315 }
62406316
62416317 let skip_ctx = usize::from(neighbours.above_skip[c]) + usize::from(neighbours.left_skip[r]);
@@ -6483,7 +6559,7 @@ fn decode_inter_block(
64836559 // verified vs independent C dump (wedge.rs).
64846560 let wedge_index = dec.symbol(&mut cdfs.wedge_idx[wedge_bsize]);
64856561 let wedge_sign = dec.literal(1);
6486- WEDGE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
6562+ WEDGE_HITS.with(|c| c.set(c.get() + 1));
64876563 wedge_mask = Some(
64886564 crate::wedge::wedge_masks()
64896565 .codebook(side)
@@ -6494,7 +6570,7 @@ fn decode_inter_block(
64946570 let mask_type = dec.literal(1);
64956571 diffwtd_mask_type = Some(mask_type as u8);
64966572 }
6497- MASKED_COMPOUND_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
6573+ MASKED_COMPOUND_HITS.with(|c| c.set(c.get() + 1));
64986574 }
64996575 // corner-cut (lane-av1comp r16/r17, lane-av1blend r1): r16/r17
65006576 // called this a `predict_compound_intermediate`/`combine_compound`
@@ -7166,7 +7242,7 @@ fn decode_inter_block(
71667242 // NO sign symbol follows -- libaom fixes
71677243 // INTERINTRA_WEDGE_SIGN 0 (blockd.h).
71687244 let wedge_index = dec.symbol(&mut cdfs.wedge_idx[wedge_bsize]);
7169- WII_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
7245+ WII_HITS.with(|c| c.set(c.get() + 1));
71707246 wedge_mask = Some((
71717247 crate::wedge::wedge_masks()
71727248 .codebook(side)
@@ -7174,7 +7250,7 @@ fn decode_inter_block(
71747250 side,
71757251 ));
71767252 } else {
7177- INTERINTRA_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
7253+ INTERINTRA_HITS.with(|c| c.set(c.get() + 1));
71787254 }
71797255 interintra_mode = Some(ii);
71807256 }
@@ -7263,11 +7339,11 @@ fn decode_inter_block(
72637339 0 => {}
72647340 1 => {
72657341 obmc_selected = true;
7266- OBMC_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
7342+ OBMC_HITS.with(|c| c.set(c.get() + 1));
72677343 }
72687344 _ => {
72697345 warped_selected = true;
7270- WARP_SELECTED_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
7346+ WARP_SELECTED_HITS.with(|c| c.set(c.get() + 1));
72717347 let mut samples = find_samples(
72727348 grid,
72737349 mi_row,
@@ -7316,7 +7392,7 @@ fn decode_inter_block(
73167392 } else {
73177393 obmc_selected = dec.symbol(&mut cdfs.obmc[bsize_idx]) == 1;
73187394 if obmc_selected {
7319- OBMC_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
7395+ OBMC_HITS.with(|c| c.set(c.get() + 1));
73207396 }
73217397 }
73227398 }
@@ -7962,7 +8038,7 @@ fn decode_inter_block8(
79628038 usize::from(neighbours.above_skip_mode[c]) + usize::from(neighbours.left_skip_mode[r]);
79638039 let skip_mode = skip_mode_present && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1;
79648040 if skip_mode {
7965- SKIP_MODE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
8041+ SKIP_MODE_HITS.with(|c| c.set(c.get() + 1));
79668042 }
79678043
79688044 let skip_ctx = usize::from(above_skip) + usize::from(left_skip);
@@ -8079,7 +8155,7 @@ fn decode_inter_block8(
80798155 // leaf's own comment.
80808156 let wedge_index = dec.symbol(&mut cdfs.wedge_idx[3]);
80818157 let wedge_sign = dec.literal(1);
8082- WEDGE_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
8158+ WEDGE_HITS.with(|c| c.set(c.get() + 1));
80838159 wedge_mask = Some(
80848160 crate::wedge::wedge_masks()
80858161 .codebook(SIDE)
@@ -8089,7 +8165,7 @@ fn decode_inter_block8(
80898165 let mask_type = dec.literal(1);
80908166 diffwtd_mask_type = Some(mask_type as u8);
80918167 }
8092- MASKED_COMPOUND_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
8168+ MASKED_COMPOUND_HITS.with(|c| c.set(c.get() + 1));
80938169 }
80948170 // corner-cut (lane-av1comp r16/r17, lane-av1blend r1): same
80958171 // reference-slot defect as the 16x16 leaf's own mask above
@@ -8471,7 +8547,7 @@ fn decode_inter_block8(
84718547 // lane-wii r2: same adapting `wedge_index` symbol as the
84728548 // 16/32 leaf, fixed sign 0 -- see that site's comment.
84738549 let wedge_index = dec.symbol(&mut cdfs.wedge_idx[3]);
8474- WII_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
8550+ WII_HITS.with(|c| c.set(c.get() + 1));
84758551 wedge_mask = Some((
84768552 crate::wedge::wedge_masks()
84778553 .codebook(SIDE)
@@ -8479,7 +8555,7 @@ fn decode_inter_block8(
84798555 SIDE,
84808556 ));
84818557 } else {
8482- INTERINTRA_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
8558+ INTERINTRA_HITS.with(|c| c.set(c.get() + 1));
84838559 }
84848560 interintra_mode = Some(ii);
84858561 }
@@ -8509,11 +8585,11 @@ fn decode_inter_block8(
85098585 0 => {}
85108586 1 => {
85118587 obmc_selected = true;
8512- OBMC_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
8513- OBMC_HITS_8.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
8588+ OBMC_HITS.with(|c| c.set(c.get() + 1));
8589+ OBMC_HITS_8.with(|c| c.set(c.get() + 1));
85148590 }
85158591 _ => {
8516- WARP_SELECTED_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
8592+ WARP_SELECTED_HITS.with(|c| c.set(c.get() + 1));
85178593 return Err(unsupported(
85188594 "an 8x8 leaf that coded WARPED_CAUSAL (motion_mode == 2): \
85198595 av1_find_projection/the affine warp filter are not \
@@ -8525,8 +8601,8 @@ fn decode_inter_block8(
85258601 // `default_obmc_cdf`'s own index: square bsize 8/16/32/64 -> 0/1/2/3.
85268602 obmc_selected = dec.symbol(&mut cdfs.obmc[0]) == 1;
85278603 if obmc_selected {
8528- OBMC_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
8529- OBMC_HITS_8.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
8604+ OBMC_HITS.with(|c| c.set(c.get() + 1));
8605+ OBMC_HITS_8.with(|c| c.set(c.get() + 1));
85308606 }
85318607 }
85328608 }
@@ -9459,7 +9535,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
94599535 // mi-granular `left_side_mi` rows of a last-block-in-
94609536 // tile strip leak 32-vs-16 only until the per-tile
94619537 // `Neighbours::new` reset.
9462- EXTENDED_PARTITION_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
9538+ EXTENDED_PARTITION_HITS.with(|c| c.set(c.get() + 1));
94639539 let at32 = at;
94649540 decode_inter_block(
94659541 &mut dec,
@@ -9628,7 +9704,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
96289704 // (mvstack.rs, `motion_mode_eligible`) now derive from
96299705 // `write_w`/`write_h`'s true 32x16 footprint -- the
96309706 // pin's fix (r1's finding).
9631- RECT_PARTITION_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
9707+ RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1));
96329708 decode_inter_block(
96339709 &mut dec,
96349710 &mut cdfs,
@@ -9729,7 +9805,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
97299805 PARTITION_VERT => {
97309806 // lane-rect r2: mirror of PARTITION_HORZ above with
97319807 // width/height swapped.
9732- RECT_PARTITION_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
9808+ RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1));
97339809 decode_inter_block(
97349810 &mut dec,
97359811 &mut cdfs,
@@ -9831,7 +9907,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
98319907 // lane-partab r1: two 16x16 squares on top + a true
98329908 // 32x16 strip below (libaom decode_partition
98339909 // PARTITION_HORZ_A: TL, TR, bottom strip).
9834- PARTAB_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
9910+ PARTAB_HITS.with(|c| c.set(c.get() + 1));
98359911 decode_inter_block(
98369912 &mut dec,
98379913 &mut cdfs,
@@ -9988,7 +10064,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
998810064 PARTITION_VERT_A => {
998910065 // lane-partab r1: mirror of PARTITION_HORZ_A with
999010066 // width/height swapped (TL, BL, right 16x32 strip).
9991- PARTAB_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
10067+ PARTAB_HITS.with(|c| c.set(c.get() + 1));
999210068 decode_inter_block(
999310069 &mut dec,
999410070 &mut cdfs,
@@ -10146,7 +10222,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
1014610222 // lane-partab r1: true 16x32 strip on the left + two
1014710223 // 16x16 squares on the right (libaom decode_partition
1014810224 // PARTITION_VERT_B: left strip, TR, BR).
10149- PARTAB_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
10225+ PARTAB_HITS.with(|c| c.set(c.get() + 1));
1015010226 decode_inter_block(
1015110227 &mut dec,
1015210228 &mut cdfs,