Use native base64-bytes conversion utils when they exist#1786
Open
peaBerberian wants to merge 1 commit intodevfrom
Open
Use native base64-bytes conversion utils when they exist#1786peaBerberian wants to merge 1 commit intodevfrom
peaBerberian wants to merge 1 commit intodevfrom
Conversation
To convert between bytes and base64, we previously had a handmade (forked) util because there was no native way of doing it (conversion from and into base64 in the web is done through the HTML API `btoa` and `atob` which relies on a JS string for the raw bytes instead of an `Uint8Array` - probably because those former API predate the latter). But there's now [native API planned (stage 4) in the future ECMAScript 2026](https://github.com/tc39/proposal-arraybuffer-base64) version which does just that, already implemented in most browsers. Those are operations we may do often: - Manifests are text files which most often rely on base64 to include binary data - such as embedded PSSH - When persisting persistent license data, if the backed storage is text-based, we may also serialize a [lot of binary data through that util](https://github.com/canalplus/rx-player/blob/63be19736deafb7c59c8ca671e35c698e77b7c11/src/main_thread/decrypt/utils/serializable_bytes.ts#L36-L45) As such we could profit in terms of performance from relying on native API instead of our JS ones, though the actual impact is difficult to know.
|
✅ Automated performance checks have passed on commit DetailsPerformance tests 1st run outputNo significative change in performance for tests:
|
0142e34 to
1fd9df3
Compare
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.
To convert between bytes and base64, we previously had a handmade (forked) util because there was no native way of doing it (conversion from and into base64 in the web is done through the HTML API
btoaandatobwhich relies on a JS string for the raw bytes instead of anUint8Array- probably because those former API predate the latter).But there's now native API planned (stage 4) in the future ECMAScript 2026 version which does just that, already implemented in most browsers.
Those are operations we may do often:
Manifests are text files which most often rely on base64 to include binary data - such as embedded PSSH
When persisting persistent license data, if the used storage is text-based (e.g.
localStorage), we may also serialize/deserialize a lot of binary data through that utilAs such we could profit in terms of performance from relying on native API instead of our JS ones, though the actual impact is difficult to know.
PS: Because this is interesting, I looked at how browsers implemented it:
Chrome/v8's implem is here: https://source.chromium.org/chromium/chromium/src/+/bbb19280f88d291ded7b1536f5c8ae5fa59b4e45:v8/src/builtins/builtins-typed-array.cc;l=748
It calls into a lib called
simdutfhere. Interestingly this is the lib proposed in the ECMAScript proposalTo transform into base64, they make heavy usage of SIMD extensions. Their code is per-arch/generation and seems to me completely unreadable to mere mortals :D: e.g. arm's, icelake's
Firefox/Spidermonkey's here: https://searchfox.org/firefox-main/rev/93aad2a6615f670b1279c229dd37f7397236131a/js/src/vm/TypedArrayObject.cpp#5517
They have their own implem in there. Much more readable, still much more optimized than our previous simple implem
Safari/JavaScriptCore:
They call a fantastic function named
base64EncodeToStringReturnNullIfOverflowbut they then make heavy use of C++ templates (generics) to then rely on the same base64 util than everything else.Then that util either call simdutf or does its own thing based on the size of each char in the output (e.g. UTF-16, ASCII...): https://github.com/WebKit/WebKit/blob/4eaf8042011babf1855953554288c14813aa91e4/Source/WTF/wtf/text/Base64.cpp#L117
Diving deep our specific case here seems to be 8-bit and as such it also goes through simdutf