A show_existing_frame output no longer stalls the parsing thread
Browse files at this commitfada71e306c05c584aa083d2d106ea963a86a90eParent: fcddd9c- perf
- a show_existing_frame output no longer stalls the parsing thread
1 changed file +109 -25
- crates/ec-av1/src/stream.rs +109 -25
crates/ec-av1/src/stream.rsmodified+109 -25
| @@ -422,25 +422,33 @@ impl SlotPromise { | |||
| 422 | } | 422 | } |
| 423 | } | 423 | } |
| 424 | 424 | ||
| 425 | -/// One finished leaf frame from a worker: its output position, and either the | 425 | +/// One finished frame from a worker: its output position, whether the job was |
| 426 | -/// (grain-synthesised) picture with the decode index to report or the error | 426 | +/// a frame DECODE (as opposed to a `show_existing_frame` output, which only |
| 427 | -/// that aborts the stream. | 427 | +/// waits for a slot and synthesises grain -- lane-thread3 counts the two |
| 428 | -type LeafResult = (usize, Result<(Picture, usize, bool)>); | 428 | +/// against separate in-flight bounds, since a waiting output job costs no |
| 429 | - | 429 | +/// CPU), and either the (grain-synthesised) picture with the decode index to |
| 430 | -/// Waits for one in-flight leaf frame and emits whatever that unblocks. | 430 | +/// report or the error that aborts the stream. |
| 431 | +type LeafResult = (usize, bool, Result<(Picture, usize, bool)>); | ||
| 432 | + | ||
| 433 | +/// Waits for one in-flight job and emits whatever that unblocks. | ||
| 431 | fn collect_one( | 434 | fn collect_one( |
| 432 | rx: &std::sync::mpsc::Receiver<LeafResult>, | 435 | rx: &std::sync::mpsc::Receiver<LeafResult>, |
| 433 | - in_flight: &mut usize, | 436 | + decodes: &mut usize, |
| 437 | + shows: &mut usize, | ||
| 434 | reorder: &mut Reorder, | 438 | reorder: &mut Reorder, |
| 435 | sink: &mut impl FnMut(&Picture, usize, bool) -> Result<()>, | 439 | sink: &mut impl FnMut(&Picture, usize, bool) -> Result<()>, |
| 436 | ) -> Result<()> { | 440 | ) -> Result<()> { |
| 437 | // Every dispatched worker answers exactly once, panic included (it is | 441 | // Every dispatched worker answers exactly once, panic included (it is |
| 438 | // caught and turned into an error below), so the sender count cannot | 442 | // caught and turned into an error below), so the sender count cannot |
| 439 | // reach zero while `in_flight > 0`. | 443 | // reach zero while `in_flight > 0`. |
| 440 | - let (seq, result) = rx | 444 | + let (seq, decode_job, result) = rx |
| 441 | .recv() | 445 | .recv() |
| 442 | .map_err(|_| Error::corrupt("AV1 decode_stream: a worker thread vanished"))?; | 446 | .map_err(|_| Error::corrupt("AV1 decode_stream: a worker thread vanished"))?; |
| 443 | - *in_flight -= 1; | 447 | + if decode_job { |
| 448 | + *decodes -= 1; | ||
| 449 | + } else { | ||
| 450 | + *shows -= 1; | ||
| 451 | + } | ||
| 444 | let (picture, decode_idx, shown) = result?; | 452 | let (picture, decode_idx, shown) = result?; |
| 445 | reorder.push(seq, picture, decode_idx, shown); | 453 | reorder.push(seq, picture, decode_idx, shown); |
| 446 | reorder.drain(sink) | 454 | reorder.drain(sink) |
| @@ -513,7 +521,12 @@ pub fn decode_stream_with_threads( | |||
| 513 | // pushed). | 521 | // pushed). |
| 514 | let mut emit_seq = 0usize; | 522 | let mut emit_seq = 0usize; |
| 515 | let mut reorder = Reorder::default(); | 523 | let mut reorder = Reorder::default(); |
| 516 | - let mut in_flight = 0usize; | 524 | + // lane-thread3: frame decodes and `show_existing_frame` outputs are |
| 525 | + // bounded separately -- an output job is a promise wait plus a memcpy, so | ||
| 526 | + // letting it eat a decode's budget just idles a core (measured: sharing | ||
| 527 | + // one bound cost 4K 18.7 s at 244% CPU against 17.7 s at 287%). | ||
| 528 | + let mut in_flight_decodes = 0usize; | ||
| 529 | + let mut in_flight_shows = 0usize; | ||
| 517 | let in_flight_cap = threads + 2; | 530 | let in_flight_cap = threads + 2; |
| 518 | let (tx, rx) = std::sync::mpsc::channel::<LeafResult>(); | 531 | let (tx, rx) = std::sync::mpsc::channel::<LeafResult>(); |
| 519 | // lane-thread3: at `threads > 1` THIS is the reference bank -- one | 532 | // lane-thread3: at `threads > 1` THIS is the reference bank -- one |
| @@ -569,20 +582,75 @@ pub fn decode_stream_with_threads( | |||
| 569 | "a show_existing_frame header naming an empty reference slot", | 582 | "a show_existing_frame header naming an empty reference slot", |
| 570 | ) | 583 | ) |
| 571 | }; | 584 | }; |
| 572 | - let picture = if threads > 1 { | 585 | + if threads > 1 { |
| 573 | // lane-thread3: the named slot may still be decoding on a | 586 | // lane-thread3: the named slot may still be decoding on a |
| 574 | - // worker. This is the parsing thread's ONLY promise wait, and | 587 | + // worker, and the copy into the slots this header refreshes |
| 575 | - // it points at an earlier coded frame (see [`SlotPromise`]); | 588 | + // is then just the promise -- no value needed. The OUTPUT |
| 576 | - // the copy into the refreshed slots is then just the promise. | 589 | + // does need the picture, so it is produced on a worker too: |
| 590 | + // waiting for it here would stall the parsing thread, and a | ||
| 591 | + // stalled parser dispatches nothing (measured before this | ||
| 592 | + // arm existed: 7.9 s of an 8.8 s 1080p decode, 18.9 s of | ||
| 593 | + // 23.3 s at 4K, spent in this one wait). The wait still | ||
| 594 | + // points at an earlier coded frame, so the argument in | ||
| 595 | + // [`SlotPromise`] is unchanged. | ||
| 577 | let promise = slot_promises[slot].clone().ok_or_else(empty_slot)?; | 596 | let promise = slot_promises[slot].clone().ok_or_else(empty_slot)?; |
| 578 | - let value = promise.wait()?; | ||
| 579 | for i in 0..NUM_REF_FRAMES { | 597 | for i in 0..NUM_REF_FRAMES { |
| 580 | if header.refresh_frame_flags & (1 << i) != 0 { | 598 | if header.refresh_frame_flags & (1 << i) != 0 { |
| 581 | slot_promises[i] = Some(promise.clone()); | 599 | slot_promises[i] = Some(promise.clone()); |
| 582 | } | 600 | } |
| 583 | } | 601 | } |
| 584 | - value.picture.as_ref().clone() | 602 | + while in_flight_shows >= in_flight_cap { |
| 585 | - } else { | 603 | + collect_one( |
| 604 | + &rx, | ||
| 605 | + &mut in_flight_decodes, | ||
| 606 | + &mut in_flight_shows, | ||
| 607 | + &mut reorder, | ||
| 608 | + &mut sink, | ||
| 609 | + )?; | ||
| 610 | + } | ||
| 611 | + let (job_seq, decode_idx) = (emit_seq, pictures_decoded); | ||
| 612 | + emit_seq += 1; | ||
| 613 | + pictures_shown += 1; | ||
| 614 | + let grain = header.film_grain; | ||
| 615 | + let seq = SeqFlags::read(&parser); | ||
| 616 | + let tx = tx.clone(); | ||
| 617 | + scope.spawn(move || { | ||
| 618 | + let reply = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { | ||
| 619 | + let value = promise.wait()?; | ||
| 620 | + let output = if grain.apply_grain && !no_grain() { | ||
| 621 | + crate::film_grain::apply_grain( | ||
| 622 | + &value.picture, | ||
| 623 | + &grain, | ||
| 624 | + seq.mc_identity, | ||
| 625 | + u32::from(seq.bit_depth), | ||
| 626 | + &crate::decode::FrameCtx::new(), | ||
| 627 | + ) | ||
| 628 | + } else { | ||
| 629 | + value.picture.as_ref().clone() | ||
| 630 | + }; | ||
| 631 | + Ok((output, decode_idx, true)) | ||
| 632 | + })) | ||
| 633 | + .unwrap_or_else(|_| { | ||
| 634 | + Err(Error::corrupt( | ||
| 635 | + "AV1 decode_stream: a worker thread panicked on a show_existing_frame output", | ||
| 636 | + )) | ||
| 637 | + }); | ||
| 638 | + let _ = tx.send((job_seq, false, reply)); | ||
| 639 | + }); | ||
| 640 | + in_flight_shows += 1; | ||
| 641 | + while let Ok((seq, decode_job, result)) = rx.try_recv() { | ||
| 642 | + if decode_job { | ||
| 643 | + in_flight_decodes -= 1; | ||
| 644 | + } else { | ||
| 645 | + in_flight_shows -= 1; | ||
| 646 | + } | ||
| 647 | + let (picture, decode_idx, shown) = result?; | ||
| 648 | + reorder.push(seq, picture, decode_idx, shown); | ||
| 649 | + reorder.drain(&mut sink)?; | ||
| 650 | + } | ||
| 651 | + continue; | ||
| 652 | + } | ||
| 653 | + let picture = { | ||
| 586 | let picture = ref_slots[slot].clone().ok_or_else(empty_slot)?; | 654 | let picture = ref_slots[slot].clone().ok_or_else(empty_slot)?; |
| 587 | for i in 0..NUM_REF_FRAMES { | 655 | for i in 0..NUM_REF_FRAMES { |
| 588 | if header.refresh_frame_flags & (1 << i) != 0 { | 656 | if header.refresh_frame_flags & (1 << i) != 0 { |
| @@ -730,8 +798,14 @@ pub fn decode_stream_with_threads( | |||
| 730 | // deadlock argument). A key frame stays inline: it resets the whole | 798 | // deadlock argument). A key frame stays inline: it resets the whole |
| 731 | // bank and reads nothing, so there is nothing to gain. | 799 | // bank and reads nothing, so there is nothing to gain. |
| 732 | if threads > 1 && header.frame_type != FrameType::Key && !header.show_existing_frame { | 800 | if threads > 1 && header.frame_type != FrameType::Key && !header.show_existing_frame { |
| 733 | - while in_flight >= in_flight_cap { | 801 | + while in_flight_decodes >= in_flight_cap { |
| 734 | - collect_one(&rx, &mut in_flight, &mut reorder, &mut sink)?; | 802 | + collect_one( |
| 803 | + &rx, | ||
| 804 | + &mut in_flight_decodes, | ||
| 805 | + &mut in_flight_shows, | ||
| 806 | + &mut reorder, | ||
| 807 | + &mut sink, | ||
| 808 | + )?; | ||
| 735 | } | 809 | } |
| 736 | // Exactly the slots this frame reads: `ref_frame_idx` names its | 810 | // Exactly the slots this frame reads: `ref_frame_idx` names its |
| 737 | // references, and its `primary_ref_frame` CDFs/segment map, its | 811 | // references, and its `primary_ref_frame` CDFs/segment map, its |
| @@ -843,13 +917,17 @@ pub fn decode_stream_with_threads( | |||
| 843 | { | 917 | { |
| 844 | promise.fail(); | 918 | promise.fail(); |
| 845 | } | 919 | } |
| 846 | - let _ = tx.send((job_seq, reply)); | 920 | + let _ = tx.send((job_seq, true, reply)); |
| 847 | }); | 921 | }); |
| 848 | - in_flight += 1; | 922 | + in_flight_decodes += 1; |
| 849 | FRAMES_DISPATCHED.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 923 | FRAMES_DISPATCHED.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 850 | // Anything that finished while this frame was being set up. | 924 | // Anything that finished while this frame was being set up. |
| 851 | - while let Ok((seq, result)) = rx.try_recv() { | 925 | + while let Ok((seq, decode_job, result)) = rx.try_recv() { |
| 852 | - in_flight -= 1; | 926 | + if decode_job { |
| 927 | + in_flight_decodes -= 1; | ||
| 928 | + } else { | ||
| 929 | + in_flight_shows -= 1; | ||
| 930 | + } | ||
| 853 | let (picture, decode_idx, shown) = result?; | 931 | let (picture, decode_idx, shown) = result?; |
| 854 | reorder.push(seq, picture, decode_idx, shown); | 932 | reorder.push(seq, picture, decode_idx, shown); |
| 855 | reorder.drain(&mut sink)?; | 933 | reorder.drain(&mut sink)?; |
| @@ -949,8 +1027,14 @@ pub fn decode_stream_with_threads( | |||
| 949 | reorder.drain(&mut sink)?; | 1027 | reorder.drain(&mut sink)?; |
| 950 | } | 1028 | } |
| 951 | } | 1029 | } |
| 952 | - while in_flight > 0 { | 1030 | + while in_flight_decodes + in_flight_shows > 0 { |
| 953 | - collect_one(&rx, &mut in_flight, &mut reorder, &mut sink)?; | 1031 | + collect_one( |
| 1032 | + &rx, | ||
| 1033 | + &mut in_flight_decodes, | ||
| 1034 | + &mut in_flight_shows, | ||
| 1035 | + &mut reorder, | ||
| 1036 | + &mut sink, | ||
| 1037 | + )?; | ||
| 954 | } | 1038 | } |
| 955 | debug_assert!(reorder.pending.is_empty(), "a frame was never emitted"); | 1039 | debug_assert!(reorder.pending.is_empty(), "a frame was never emitted"); |
| 956 | Ok(()) | 1040 | Ok(()) |