-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TLDR: We request the support for TextEncoder in FLEDGE bidding worklet. #961
Comments
If you're using Protobufs (a binary format) and WASM (which takes binary data in formats like ArrayBuffer, where Uint8Array is a view into that ArrayBuffer), and are running into the lack of TextEncoder, it sounds like you're looking for ways to get strings (I assume encoding the binary data in things like base64) from JavaScript into WASM. In that case, TextEncoder might be the wrong API, as it takes UTF-16 code units from an input string and converts that into a UTF-8 Uint8Array -- IIUC, it can’t correctly round trip arbitrary byte sequences that aren’t valid UTF-16. Instead, a library that does base64 -> ArrayBuffer is what you probably want instead -- I wrote a pure-JS library that seems to perform well (comparable to TextEncoder), code below. The base64 format is described in https://datatracker.ietf.org/doc/html/rfc4648. Or is the protobuf parsing happening in a JavaScript library that itself invokes the TextEncoder library? Here’s the pure JavaScript code I wrote that does UTF-8 -> ArrayBuffer conversion: const b64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const lookup = new Uint8Array(256);
for (let i = 0; i < b64Chars.length; i++) {
lookup[b64Chars.charCodeAt(i)] = i;
}
function b64ToArrayBuf(b64String) {
const outputBuf = allocateBufFor(b64String);
return b64ToArrayBufInto(b64String, outputBuf);
}
function allocateBufFor(b64String) {
if (b64String.length % 4 !== 0) {
throw "b64ToArrayBuf(): Expected input size divisible by 4; actual size " +
b64String.length;
}
if (b64String.length === 0) {
return new ArrayBuffer(0);
}
let outputLength = b64String.length / 4 * 3;
if (b64String[b64String.length - 1] === "=") {
outputLength--;
if (b64String[b64String.length - 2] === "=") {
outputLength--;
}
}
return new ArrayBuffer(outputLength);
}
function b64ToArrayBufInto(b64String, outputBuf) {
const outputUint8Array = new Uint8Array(outputBuf);
let outPtr = 0;
for (let i = 0; i < b64String.length; i += 4) {
const part0 = lookup[b64String.charCodeAt(i)];
const part1 = lookup[b64String.charCodeAt(i + 1)];
const part2 = lookup[b64String.charCodeAt(i + 2)];
const part3 = lookup[b64String.charCodeAt(i + 3)];
outputUint8Array[outPtr++] = (part0 << 2) | (part1 >> 4);
outputUint8Array[outPtr++] = ((part1 & 0x0F) << 4) | (part2 >> 2);
outputUint8Array[outPtr++] = ((part2 & 0x03) << 6) | part3;
}
return outputBuf;
} |
Thanks for the suggestion! We're testing your implementation and will report back on the efficiency.
Unfortunately that's the case. There is a function serializeBinary (which returns Uinit8Array) in the protobuf library (JS) that is calls TextEncoder underneath, and hence your suggested implementation won't be sufficient. Would appreciate if you have any more suggestions. |
We're running into a similar issue with |
@droundy, would you please add your affiliation to your GitHub profile? |
I've added my affiliation with NextRoll to my profile. You can build (once you put them in the right directories with
which results in a I'm sure my example could be more minimal, and I haven't actually tested this minimal example in a browser to confirm that it uses those bindings, because I don't know of a convenient way to trigger an auction without messing with our staging deployment. |
This allows use from content/services/auction_worklet and continued use from services/accessibility/features. This is the first step in addressing WICG/turtledove#961 AX-Relnotes: n/a Change-Id: I72d6e3ba799a683d3d7113ae5abc4b4bc0af37f7 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6181149 Commit-Queue: Paul Jensen <pauljensen@chromium.org> Reviewed-by: Colin Blundell <blundell@chromium.org> Cr-Commit-Position: refs/heads/main@{#1410488}
So I've been assigned to look into supporting TextEncoder, but looks like there is some interest in TextDecoder as well? I'd like to share some thoughts in case people have feedback.
|
From the Google Ads side,
For us, having TextEncoder (or equivalent) supported is a higher priority than TextDecoder. |
Do you need this outside generateBid? (WIP at https://chromium-review.googlesource.com/c/chromium/src/+/6269638 FWIW, missing the flag to turn it off/on, though, and some test) |
We'd be good with just decoding utf8.
David Roundy
…On Thu, Feb 20, 2025, 8:13 AM Maks Orlovich ***@***.***> wrote:
Do you need this outside generateBid?
(WIP at https://chromium-review.googlesource.com/c/chromium/src/+/6269638
FWIW, missing the flag to turn it off/on, though, and some test)
—
Reply to this email directly, view it on GitHub
<#961 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABBSKN3FYC6HEE7FM6OUP32QX5KRAVCNFSM6AAAAABWRKFTSGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNZRHE3TAOBXGE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
[image: morlovich]*morlovich* left a comment (WICG/turtledove#961)
<#961 (comment)>
Do you need this outside generateBid?
(WIP at https://chromium-review.googlesource.com/c/chromium/src/+/6269638
FWIW, missing the flag to turn it off/on, though, and some test)
—
Reply to this email directly, view it on GitHub
<#961 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABBSKN3FYC6HEE7FM6OUP32QX5KRAVCNFSM6AAAAABWRKFTSGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMNZRHE3TAOBXGE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
We might need it in reportWin (not sure if the current WIP covers that already). It seems like having scoreAd/reportResult would make sense for consistency, but I can't speak for if sellers have a need for it. |
In place of TextEncoder/TextEncoder which we don't have at hand and which are more complex than what's actually needed. (Requested in WICG/turtledove#961) Bug: 397936915 Change-Id: Iaa9018e05119a4dc0ef1579134cb50347fb57c30
In place of TextEncoder/TextEncoder which we don't have at hand and which are more complex than what's actually needed. (Requested in WICG/turtledove#961) Bug: 397936915 Change-Id: Iaa9018e05119a4dc0ef1579134cb50347fb57c30 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6269638 Reviewed-by: Russ Hamilton <behamilton@google.com> Commit-Queue: Maks Orlovich <morlovich@chromium.org> Cr-Commit-Position: refs/heads/main@{#1425747}
In place of TextEncoder/TextEncoder which we don't have at hand and which are more complex than what's actually needed. (Requested in WICG/turtledove#961) Bug: 397936915 Change-Id: Iaa9018e05119a4dc0ef1579134cb50347fb57c30 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6269638 Reviewed-by: Russ Hamilton <behamilton@google.com> Commit-Queue: Maks Orlovich <morlovich@chromium.org> Cr-Commit-Position: refs/heads/main@{#1425747}
In place of TextEncoder/TextEncoder which we don't have at hand and which are more complex than what's actually needed. (Requested in WICG/turtledove#961) Bug: 397936915 Change-Id: Iaa9018e05119a4dc0ef1579134cb50347fb57c30 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6269638 Reviewed-by: Russ Hamilton <behamilton@google.com> Commit-Queue: Maks Orlovich <morlovich@chromium.org> Cr-Commit-Position: refs/heads/main@{#1425747}
…de,Decode} to worklets, a=testonly Automatic update from web-platform-tests FLEDGE: Provide browserSignals.utf8{Encode,Decode} to worklets In place of TextEncoder/TextEncoder which we don't have at hand and which are more complex than what's actually needed. (Requested in WICG/turtledove#961) Bug: 397936915 Change-Id: Iaa9018e05119a4dc0ef1579134cb50347fb57c30 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6269638 Reviewed-by: Russ Hamilton <behamilton@google.com> Commit-Queue: Maks Orlovich <morlovich@chromium.org> Cr-Commit-Position: refs/heads/main@{#1425747} -- wpt-commits: e57faa381e135a2e09d2d825b534efcde22fed9c wpt-pr: 50979
…de,Decode} to worklets, a=testonly Automatic update from web-platform-tests FLEDGE: Provide browserSignals.utf8{Encode,Decode} to worklets In place of TextEncoder/TextEncoder which we don't have at hand and which are more complex than what's actually needed. (Requested in WICG/turtledove#961) Bug: 397936915 Change-Id: Iaa9018e05119a4dc0ef1579134cb50347fb57c30 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6269638 Reviewed-by: Russ Hamilton <behamilton@google.com> Commit-Queue: Maks Orlovich <morlovich@chromium.org> Cr-Commit-Position: refs/heads/main@{#1425747} -- wpt-commits: e57faa381e135a2e09d2d825b534efcde22fed9c wpt-pr: 50979
…de,Decode} to worklets, a=testonly Automatic update from web-platform-tests FLEDGE: Provide browserSignals.utf8{Encode,Decode} to worklets In place of TextEncoder/TextEncoder which we don't have at hand and which are more complex than what's actually needed. (Requested in WICG/turtledove#961) Bug: 397936915 Change-Id: Iaa9018e05119a4dc0ef1579134cb50347fb57c30 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6269638 Reviewed-by: Russ Hamilton <behamilton@google.com> Commit-Queue: Maks Orlovich <morlovich@chromium.org> Cr-Commit-Position: refs/heads/main@{#1425747} -- wpt-commits: e57faa381e135a2e09d2d825b534efcde22fed9c wpt-pr: 50979
This reverts commit eb03766. Reason for revert: Second usage of components/text_encoder unnecessary. Original change's description: > This allows use from content/services/auction_worklet and > continued use from services/accessibility/features. > > This is the first step in addressing > WICG/turtledove#961 AX-Relnotes: n/a Change-Id: I960a28e74aa591d07ffa152253a88a40d17ca13e Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6330176 Commit-Queue: Paul Jensen <pauljensen@chromium.org> Reviewed-by: Katie Dektar <katie@chromium.org> Cr-Commit-Position: refs/heads/main@{#1429087}
object handing of the global object, rather than browserSignals argument to functions, as design doc feedback pointed out that it's otherwise hard to use in frozen context mode to pollyfill TextEncoder/TextDecoder. (Part of WICG/turtledove#961) Bug: 397936915 Change-Id: I084b14072ae8aac8419008410d4ee713191d77e9
object handing of the global object, rather than browserSignals argument to functions, as design doc feedback pointed out that it's otherwise hard to use in frozen context mode to pollyfill TextEncoder/TextDecoder. (Part of WICG/turtledove#961) Bug: 397936915 Change-Id: I084b14072ae8aac8419008410d4ee713191d77e9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6329720 Commit-Queue: Maks Orlovich <morlovich@chromium.org> Reviewed-by: Russ Hamilton <behamilton@google.com> Cr-Commit-Position: refs/heads/main@{#1429678}
object handing of the global object, rather than browserSignals argument to functions, as design doc feedback pointed out that it's otherwise hard to use in frozen context mode to pollyfill TextEncoder/TextDecoder. (Part of WICG/turtledove#961) Bug: 397936915 Change-Id: I084b14072ae8aac8419008410d4ee713191d77e9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6329720 Commit-Queue: Maks Orlovich <morlovich@chromium.org> Reviewed-by: Russ Hamilton <behamilton@google.com> Cr-Commit-Position: refs/heads/main@{#1429678}
object handing of the global object, rather than browserSignals argument to functions, as design doc feedback pointed out that it's otherwise hard to use in frozen context mode to pollyfill TextEncoder/TextDecoder. (Part of WICG/turtledove#961) Bug: 397936915 Change-Id: I084b14072ae8aac8419008410d4ee713191d77e9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6329720 Commit-Queue: Maks Orlovich <morlovich@chromium.org> Reviewed-by: Russ Hamilton <behamilton@google.com> Cr-Commit-Position: refs/heads/main@{#1429678}
Put in some (very short) explainer text in the pull request 1409. (Not approved/reviewed/etc., yet, and I have a bunch of process stuff to do before this can be turned on even in canary/dev) |
…otectedAudience., a=testonly Automatic update from web-platform-tests FLEDGE: Move encodeUtf8/decodeUtf8 to protectedAudience. object handing of the global object, rather than browserSignals argument to functions, as design doc feedback pointed out that it's otherwise hard to use in frozen context mode to pollyfill TextEncoder/TextDecoder. (Part of WICG/turtledove#961) Bug: 397936915 Change-Id: I084b14072ae8aac8419008410d4ee713191d77e9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6329720 Commit-Queue: Maks Orlovich <morlovich@chromium.org> Reviewed-by: Russ Hamilton <behamilton@google.com> Cr-Commit-Position: refs/heads/main@{#1429678} -- wpt-commits: 56a607de4b1d7fd09314eda3f18b82ca16837fe4 wpt-pr: 51203
…otectedAudience., a=testonly Automatic update from web-platform-tests FLEDGE: Move encodeUtf8/decodeUtf8 to protectedAudience. object handing of the global object, rather than browserSignals argument to functions, as design doc feedback pointed out that it's otherwise hard to use in frozen context mode to pollyfill TextEncoder/TextDecoder. (Part of WICG/turtledove#961) Bug: 397936915 Change-Id: I084b14072ae8aac8419008410d4ee713191d77e9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6329720 Commit-Queue: Maks Orlovich <morlovich@chromium.org> Reviewed-by: Russ Hamilton <behamilton@google.com> Cr-Commit-Position: refs/heads/main@{#1429678} -- wpt-commits: 56a607de4b1d7fd09314eda3f18b82ca16837fe4 wpt-pr: 51203
Canary/Dev 50% config landed (it will take some time for it to actually reach these numbers). When it's on, you'll have a protectedAudience.encodeUtf8 (and decodeUtf8) in worklets; when it's off there isn't a protectedAudience object, so please feature detect accordingly. |
Working on the spec, I realized I implemented decoding as equivalent to ignoreBOM: true (...probably since I think the name should mean the opposite of what it does); are folks OK with this, or should I go change it? |
We don't use a Byte Order Mark (BOM) in our code, so the ignoreBOM option doesn't change anything for us. Even though the BOM is optional for UTF-8 (see https://learn.microsoft.com/en-us/globalization/encoding/unicode-standard), it can still serve as an encoding indicator. To maintain consistency with the standard TextDecoder (which defaults ignoreBOM to false), I suggest we make our decodeUtf8 function behave similarly. |
TLDR: We request the support for TextEncoder in FLEDGE bidding worklet.
We’re testing our WASM implementation of generateBid, and we found difficulties as FLEDGE worklet doesn’t support TextEncoder. This is essential as our implementation depends on Protobuf.
When we use polyfill instead of TextEncoder, the resulting bidding script is significantly slower than javascript, which is in contradict to what we observe in our simulation with TextEncoder (running under Chrome main thread).
The text was updated successfully, but these errors were encountered: