Repository navigation
Skip the copy in writeRbsp when a frame needs no escaping - #2184
Merged
Merged
Conversation
Find the escape points first, return the input when there are none, and otherwise copy the runs between them with Uint8Array.set. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 75af414 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
size-limit report 📦
|
1egoman
approved these changes
Oct 25, 2026
| * Returns `data_in` itself when nothing needs escaping, which is the case for almost | ||
| * every encrypted frame. Callers must not mutate the result in place. | ||
| */ | ||
| export function writeRbsp(data_in: NonSharedUint8Array): NonSharedUint8Array { |
Contributor
There was a problem hiding this comment.
nitpick: data_in should probably be dataIn?
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The E2EE worker calls
writeRbspon every encrypted H.264 and H.265 frame on the sender. It adds emulation prevention bytes so that the ciphertext never contains a start code. Today the function pushes every byte into a JS array and then copies that array into aUint8Array. On a 500 KB keyframe this takes about 2.2 ms, but AES-GCM takes about 0.09 ms. We found this while working on #2181.Change
writeRbspscans the input once and records each index that needs an emulation prevention byte. If there are none, it returns the input unchanged. This is the case for almost every encrypted frame, because random ciphertext rarely has two zeros followed by a byte<= 3. If there are escapes, it allocates the output once and copies the runs between the escapes withUint8Array.set. The output and the wire format do not change.parseRbspdoes not change, because the receiver calls it only afterneedsRbspUnescapingfinds an escape.Verified
npx vitest run src/e2eepasses. The newsrc/e2ee/utils.test.tschecks known escaped sequences: no escape, one escape, runs of zeros, and trailing zeros. It also checks that the function returns the same instance when nothing needs escaping.🤖 Generated with Claude Code