Skip to content
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

Enable ArrayBuffer Transfer via postMessage for Improved Performance #175

Open
vs-gianni opened this issue Mar 27, 2025 · 1 comment
Open

Comments

@vs-gianni
Copy link

vs-gianni commented Mar 27, 2025

TLDR;
Currently, passing ArrayBuffer data from Devvit to a WebView requires converting it to a base64 string. This workaround significantly increases data size and reduces performance due to unnecessary cloning and data encoding.

Description:

Forwarding raw arrayBuffer received with a fetch request from devvit to the Webview is a great performance improvement, currently the only workaround is to convert data as a base 64 string, which clones and expand the data size.

Transferable objects are commonly used to share resources that can only be safely exposed to a single JavaScript thread at a time. For example, an ArrayBuffer is a transferable object that owns a block of memory. When such a buffer is transferred between threads, the associated memory resource is detached from the original buffer and attached to the buffer object created in the new thread.

The correct solution would be to forward the third argument of postMessage function:

transfer
An optional array of transferable objects to transfer ownership of. The ownership of these objects is given to the destination side and they are no longer usable on the sending side. These transferable objects should be attached to the message; otherwise they would be moved but not actually accessible on the receiving end.

Today, passing an arrayBuffer via postMessage will make it not accessible from within the Webview for the reason above.

The solution would be to change the signature of the postMessage function in UseWebViewResult from:
postMessage(message: To): void;
to:
postMessage(message: To, transfer?: Transferable[]): void;

@vs-gianni
Copy link
Author

I could make a PR myself but I'd need a nudge in the right direction to do the wiring where the postMessage emitEffect is handled. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant