You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf: libobs: tail-scan insert and a ring buffer for the output interleave queue #3209
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.
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.
Claiming: #3209 (tail scan for the insert position in the shipped Rust interleaver).
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.
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.
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.
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:
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.
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.
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.
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.
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.
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.
Activity
zackees commentedon Oct 31, 2026
Correction: hybrid libobs doesn't ship the C cited here.
libobs.ported-output-interleave+.h.cis a ported group, soobs-output-interleave.c.his only the Tier 3 reference. The code that ships isrust/obs-output/src/interleave.rs:440(insert_position, a front scan) andrust/obs-output/src/ffi/output_interleave.rs:573-587(darray_insert, anddarray_erase_range(...,0,..)at the front). The issue body was also wrong to say the Rust already uses VecDeque andpartition_point: it doesn't. Make the change in the Rust.zackees commentedon Oct 31, 2026
/ready
zackees commentedon Nov 1, 2026
Claiming: #3209 (tail scan for the insert position in the shipped Rust interleaver).
insert_positionscans from the tail. Every packet enters the queue throughinsert_interleaved_packet, and the only in-place change that can break the order (the offsets thatinitialize_interleaved_packetsapplies) is always followed byresort_interleaved_packets, which rebuilds the queue by inserting. So the queue is always sorted wheninsert_positionruns, and both scans return the same index.The front erase stays as it is:
interleaved_packetsis a C darray insidestruct obs_output, and a ring buffer is a separate change. PR today.zackees commentedon Nov 1, 2026
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_positionscanning from the back) and Tier 3 failed:interleave_parity::parity_random_sequencesdiverges 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:
struct obs_output, so knowing that means tracking it beside the queue. That's possible but not cheap.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 forDecided:before pushing anything.zackees commentedon Nov 1, 2026
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:
sortedstartstrueon an empty queue.i(found by either scan), the queue stays sorted iffq[i-1] <= pkt <= q[i+1]under the same comparisoninsert_positionuses. That is an O(1) check of two neighbours, and if it fails, setsorted = false.initialize_interleaved_packetsshifts offsets in place, so setsorted = falsethere.resort_interleaved_packetsrebuilds by inserting, so the flag comes back on its own if the rebuilt queue is sorted.sortedis 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:
struct obs_output: the flag sits beside the queue in the Rust interleaver.To build: keep
parity_random_sequencesunchanged, and add a Tier 1 case asserting that the flag is cleared by a backwards-dts insert and byinitialize_interleaved_packets. The bench should compare front scan vs tail scan on a sorted 2000-packet queue. Unblocking the claim.github-actions commentedon Nov 1, 2026
Claim bot: @zackees, the decision is in; your claim is no longer blocked.
perf(obs-output): tail-scan the interleave queue while it is known so…