Skip to content

perf(moq-mux): SIMD-accelerate MPEG-TS sync byte resync#1695

Merged
kixelated merged 3 commits into
mainfrom
claude/simd-optimization-search-g936s8
Jun 12, 2026
Merged

perf(moq-mux): SIMD-accelerate MPEG-TS sync byte resync#1695
kixelated merged 3 commits into
mainfrom
claude/simd-optimization-search-g936s8

Conversation

@kixelated

Copy link
Copy Markdown
Collaborator

Summary

You asked me to look for code that could be SIMD-optimized, but only where it's a simple change. After surveying the byte-scanning hot loops across rs/ and js/, there was exactly one clean win.

The MPEG-TS demuxer (rs/moq-mux/src/container/ts/import.rs) resyncs after a lost sync byte by scanning forward one byte at a time looking for the next 0x47:

if self.scratch[off] != 0x47 {
    off += 1;
    continue;
}

This now jumps straight to the next sync byte with memchr::memchr, which is SIMD-accelerated (SSE2/AVX2/NEON) and was already a dependency of the crate:

if self.scratch[off] != 0x47 {
    off = match memchr::memchr(0x47, &self.scratch[off..]) {
        Some(rel) => off + rel,
        None => self.scratch.len(),
    };
    continue;
}

The end state is identical to the old loop (advance to the next sync byte, or drop the whole buffer when none remains), so resync semantics are unchanged. This matters most when a live capture joins mid-stream or a stream loses sync, where the old path crawled byte-by-byte over the buffer.

What I deliberately left alone

  • H.264/H.265 Annex-B start-code scanning (rs/moq-mux/src/codec/annexb.rs) already uses memchr::memmem::find, which is SIMD-accelerated. No change needed.
  • There is no TypeScript packet finder to optimize. The MPEG-TS demuxer lives in Rust; the JS side doesn't scan for sync bytes / start codes.
  • Other candidates (ADTS frame parsing, ESDS descriptor scan, length-prefix decode) were either too small to matter or not a clean drop-in, so they didn't meet the "simple change" bar.

Test plan

  • cargo test -p moq-mux (all 36 container::ts tests pass, including import_resyncs_after_byte_misalignment, stays_desynced_until_next_pusi, tei_continuation_drops_partial_and_resyncs)
  • cargo clippy -p moq-mux clean

(Written by Claude)


Generated by Claude Code

The TS demuxer scanned forward one byte at a time to find the next 0x47
sync byte after a misalignment. Replace the byte-by-byte loop with
memchr::memchr, which is SIMD-accelerated and already a dependency. The
end state is identical (jump to the next sync byte, or drop the buffer if
none remains), so resync semantics are unchanged.
@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@kixelated, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 3 minutes and 52 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1e326a37-8833-4cad-97d5-0a81fb849a2f

📥 Commits

Reviewing files that changed from the base of the PR and between 916f7ae and faebdff.

📒 Files selected for processing (1)
  • rs/moq-mux/src/container/ts/import.rs

Walkthrough

This PR changes Import::decode's resynchronization: when the byte at the current offset is not 0x47, it now calls memchr to locate the next 0x47 in the remaining scratch buffer. If memchr finds no match, the offset is set to scratch.len() so the packet loop exits.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: replacing byte-by-byte MPEG-TS sync resynchronization with SIMD-accelerated memchr. It is specific, directly related to the primary improvement, and accurately reflects the changeset.
Description check ✅ Passed The description is thorough and directly related to the changeset. It explains the motivation, the old vs. new approach, reasoning for other non-changes, and includes comprehensive test results, all aligned with the actual code modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch claude/simd-optimization-search-g936s8

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
rs/moq-mux/src/container/ts/import.rs (1)

145-148: ⚡ Quick win

Use if let for memchr result unwrapping consistency.

This match is only unwrapping Option and setting fallback state; converting to if let aligns with the repo Rust style rule.

Suggested refactor
-				off = match memchr::memchr(0x47, &self.scratch[off..]) {
-					Some(rel) => off + rel,
-					None => self.scratch.len(),
-				};
+				if let Some(rel) = memchr::memchr(0x47, &self.scratch[off..]) {
+					off += rel;
+				} else {
+					off = self.scratch.len();
+				}

As per coding guidelines, "**/*.rs: Prefer if let / let ... else over an unwrapping match when the only job is to unwrap."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@rs/moq-mux/src/container/ts/import.rs` around lines 145 - 148, Replace the
match that unwraps memchr::memchr with an if let to match repo style: call
memchr::memchr(0x47, &self.scratch[off..]) and use `if let Some(rel) = ... { off
= off + rel; } else { off = self.scratch.len(); }`, preserving the exact
semantics for the `off` update; reference the `off` variable, `self.scratch`,
and the memchr::memchr call in import.rs.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@rs/moq-mux/src/container/ts/import.rs`:
- Around line 145-148: Replace the match that unwraps memchr::memchr with an if
let to match repo style: call memchr::memchr(0x47, &self.scratch[off..]) and use
`if let Some(rel) = ... { off = off + rel; } else { off = self.scratch.len();
}`, preserving the exact semantics for the `off` update; reference the `off`
variable, `self.scratch`, and the memchr::memchr call in import.rs.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 935f8402-4feb-4659-ba18-da4864e9dc55

📥 Commits

Reviewing files that changed from the base of the PR and between 789cf21 and 5527d4f.

📒 Files selected for processing (1)
  • rs/moq-mux/src/container/ts/import.rs

claude added 2 commits June 12, 2026 18:08
Match repo Rust convention preferring if let over an unwrapping match.
@kixelated
kixelated enabled auto-merge (squash) June 12, 2026 18:18
@kixelated
kixelated disabled auto-merge June 12, 2026 18:18
@kixelated
kixelated enabled auto-merge (squash) June 12, 2026 18:30
@kixelated
kixelated merged commit 4d09d42 into main Jun 12, 2026
1 check passed
@kixelated
kixelated deleted the claude/simd-optimization-search-g936s8 branch June 12, 2026 18:32
@moq-bot moq-bot Bot mentioned this pull request Jun 12, 2026
@moq-bot moq-bot Bot mentioned this pull request Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants