Skip to content

perf: libobs: tail-scan insert and a ring buffer for the output interleave queue #3209

Closed

Description

@zackees

What

libobs/obs-output-interleave.c.h:529-557 scans from the front and calls da_insert; :121 does da_erase(...,0) on every send; resort_interleaved_packets (:560) is O(n^2); encoder_packet_times (:150-165) is a linear search.

Proposed change

Scan from the tail and use a head-index ring instead of the front erase; the Rust port uses VecDeque plus partition_point.

Acceptance criteria

  • Before/after numbers (criterion bench in rust/obs-output: 10k packets over a 2000-packet standing queue, C vs Rust) in the PR; nothing lands unmeasured.
  • RED then GREEN; Tier 3 parity stays bit-exact unless this issue says otherwise.

Found by a read-only code scout and not yet measured. Parent: #3176.

Activity

  1. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Correction: hybrid libobs doesn't ship the C cited here. libobs.ported-output-interleave+.h.c is a ported group, so obs-output-interleave.c.h is only the Tier 3 reference. The code that ships is rust/obs-output/src/interleave.rs:440 (insert_position, a front scan) and rust/obs-output/src/ffi/output_interleave.rs:573-587 (darray_insert, and darray_erase_range(...,0,..) at the front). The issue body was also wrong to say the Rust already uses VecDeque and partition_point: it doesn't. Make the change in the Rust.

  2. added
    enhancement
    P3
    size:M
    on Oct 31, 2026
  3. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    /ready

  4. added
    ready
    on Oct 31, 2026
  5. zackees commented on Nov 1, 2026

    @zackees
    MemberAuthor

    Claiming: #3209 (tail scan for the insert position in the shipped Rust interleaver).

    1. RED: a test that a queue built by inserts gets the same positions from a tail scan as from the front scan, with the tail scan stubbed.
    2. GREEN: insert_position scans from the tail. Every packet enters the queue through insert_interleaved_packet, and the only in-place change that can break the order (the offsets that initialize_interleaved_packets applies) is always followed by resort_interleaved_packets, which rebuilds the queue by inserting. So the queue is always sorted when insert_position runs, and both scans return the same index.
    3. Bench: 10k inserts into a 2000-packet queue, front scan vs tail scan.

    The front erase stays as it is: interleaved_packets is a C darray inside struct obs_output, and a ring buffer is a separate change. PR today.

  6. zackees commented on Nov 1, 2026

    @zackees
    MemberAuthor

    Decision needed: a tail scan changes which slot a packet goes to when the queue is out of order, so it is not bit-exact with the C.

    I built it (insert_position scanning from the back) and Tier 3 failed: interleave_parity::parity_random_sequences diverges at step 71. An encoder whose dts runs backwards (audio track 1, dts_step: -1024) leaves the queue out of order, with dts_usec 0, 0, then -1042666, -1021333, … after it. On such a queue the C's front scan inserts the packet before the first later packet it meets (index 1). The tail scan puts it after the trailing run of earlier packets (index 6). So my claim that the queue is always sorted was wrong: only an encoder with monotonic timestamps keeps it sorted.

    Options:

    1. Keep C parity: the scan stays O(n). A tail scan would only be safe if we knew the queue is sorted, and the order is stored in the C darray inside struct obs_output, so knowing that means tracking it beside the queue. That's possible but not cheap.
    2. Accept the tail scan, which differs from the C only when an encoder emits non-monotonic dts. Then the parity test needs a documented exception for unsorted queues, the way perf: obs-audio-controls: vectorize the volmeter RMS sum #3195 is waiting on its own bit-exact call.

    The RED test (a 2000-packet queue reads all 4000 dts values to place one late packet) is on branch perf/3209-interleave-tail-scan, local only. I'll wait for Decided: before pushing anything.

  7. added
    decision-needed
    claim-blocked
    on Nov 1, 2026
  8. moved this from Ready to Needs decision in obs-rust Multiviewon Nov 1, 2026
  9. zackees commented on Nov 1, 2026

    @zackees
    MemberAuthor

    Decided: keep exact C parity (option 1), but get the tail scan through a cheap "known sorted" flag rather than full order tracking. Scan from the tail only while the queue is known to be sorted. Otherwise fall back to the C's front scan.

    How:

    • sorted starts true on an empty queue.
    • On insert at index i (found by either scan), the queue stays sorted iff q[i-1] <= pkt <= q[i+1] under the same comparison insert_position uses. That is an O(1) check of two neighbours, and if it fails, set sorted = false.
    • Popping from the front never unsorts the queue.
    • initialize_interleaved_packets shifts offsets in place, so set sorted = false there. resort_interleaved_packets rebuilds by inserting, so the flag comes back on its own if the rebuilt queue is sorted.
    • When sorted is true, a tail scan and a front scan return the same index, so the choice is unobservable and Tier 3 stays bit-exact. When it's false, the C's front scan runs, which also matches.

    Reasons:

    • testing-policy.md requires Tier 3 parity with the C. The tolerance accepted on perf: obs-filters: skip log10f/powf below threshold in compressor, limiter and expander #3190 was floating-point epsilon in a numeric result. This divergence is different: packets go to different slots, which changes output order, not just precision.
    • Your step-71 case shows the always-sorted claim doesn't hold for non-monotonic encoders, so option 2 would ship an observable behaviour change.
    • Encoders with monotonic dts, the normal case, still get the tail scan and its win. The pathological case pays exactly what the C pays today.
    • It's reversible and needs no change to struct obs_output: the flag sits beside the queue in the Rust interleaver.

    To build: keep parity_random_sequences unchanged, and add a Tier 1 case asserting that the flag is cleared by a backwards-dts insert and by initialize_interleaved_packets. The bench should compare front scan vs tail scan on a sorted 2000-packet queue. Unblocking the claim.

  10. removed
    decision-needed
    claim-blocked
    on Nov 1, 2026
  11. github-actions commented on Nov 1, 2026

    @github-actions

    Claim bot: @zackees, the decision is in; your claim is no longer blocked.

    Decided: keep exact C parity (option 1), but get the tail scan through a cheap "known sorted" flag rather than full order tracking. Scan from the tail only while the queue is known to be sorted. Otherwise fall back to the C's front scan.

    How:

    • sorted starts true on an empty queue.
    • On insert at index i (found by either scan), the queue stays sorted iff q[i-1] <= pkt <= q[i+1] under the same comparison insert_position uses. That is an O(1) check of two neighbours, and if it fails, set sorted = false.
    • Popping from the front never unsorts the queue.
    • initialize_interleaved_packets shifts offsets in place, so set sorted = false there. resort_interleaved_packets rebuilds by inserting, so the flag comes back on its own if the rebuilt queue is sorted.
    • When sorted is true, a tail scan and a front scan return the same index, so the choice is unobservable and Tier 3 stays bit-exact. When it's false, the C's front scan runs, which also matches.

    Reasons:

    • testing-policy.md requires Tier 3 parity with the C. The tolerance accepted on perf: obs-filters: skip log10f/powf below threshold in compressor, limiter and expander #3190 was floating-point epsilon in a numeric result. This divergence is different: packets go to different slots, which changes output order, not just precision.
    • Your step-71 case shows the always-sorted claim doesn't hold for non-monotonic encoders, so option 2 would ship an observable behaviour change.
    • Encoders with monotonic dts, the normal case, still get the tail scan and its win. The pathological case pays exactly what the C pays today.
    • It's reversible and needs no change to struct obs_output: the flag sits beside the queue in the Rust interleaver.

    To build: keep parity_random_sequences unchanged, and add a Tier 1 case asserting that the flag is cleared by a backwards-dts insert and by initialize_interleaved_packets. The bench should compare front scan vs tail scan on a sorted 2000-packet queue. Unblocking the claim.

  12. moved this from Needs decision to Done in obs-rust Multiviewon Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3enhancementreadysize:M

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions