-
Notifications
You must be signed in to change notification settings - Fork 319
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
Fake transfers for getMappedRange-created ArrayBuffers? #2072
Comments
In other words, postMessage creates a new ArrayBuffer with a copy of the contents, and transfers that in place of the original one, and detaches the original ArrayBuffer and frees its underlying reference to the mapped data. An alternative to this, which came up while discussing this with @pythonesque, would be to require these ArrayBuffers to be disposed somehow before .unmap() is called. Unmap would then fail (refuse to unmap) if this wasn't the case - there could be dangling ArrayBuffers somewhere out there referring to the mapped data. This could be done without making the current API worse: any ArrayBuffers that haven't been transferred away would still be on the same thread so they could be detached without problems. If we had a way to share mappings across threads (detachable SharedArrayBuffers, #747), this would also work nicely here: The SharedArrayBuffer must be returned for disposal on every thread where it exists. If detachable SharedArrayBuffers existed at all I think they would probably have to work like this - i.e. each detachable SharedArrayBuffer would be a refcounted pointer to underlying data that would be freed after all references are dropped. And in unmap we would require the refcount to be 0. A caveat of this approach is that, IIRC, when you transfer an object in JS it can actually arrive in multiple places, for reasons (relating to WebExtensions..?) that I don't completely understand. So we'd have to understand that. |
Using SharedArrayBuffers would also be a considerable hit from an optimization perspective, I suspect (whether in JavaScript or not). In Rust, it would require treating each access to an element as an atomic which disables lots of useful optimizations and is generally very unpleasant to use (for example, safe transmute operations that treat a bag of bytes as a bag of plain old data types of other sizes or structures are not generally sound for atomics). |
JavaScript provides atomics, but allows racy access to SharedArrayBuffer without them. I don't think we would change all mappings to return SharedArrayBuffer, it would probably be opt-in. |
I think that if you allow racy accesses at all, the optimizer must still treat them as something like "Relaxed" or "unordered" LLVM intrinsics in order to ensure there are no unsafe optimizations (such as assuming an in-bounds index stored and then loaded from an array slot is still in bounds). Similarly, I think if there are any APIs that allow returning non-SharedArrayBuffer mappings, then we still need one of the other proposed solutions to figure out the |
Originally posted by @pythonesque in #747 (comment)
Exactly. The only surprising thing would be that writes to the new ArrayBuffer wouldn't propagate to the GPUBuffer. Which isn't that weird, because the ArrayBuffer is no longer on the thread that issues the unmap() (which propagates the changes) anyway. |
Well, I suspect "isn't that weird if you know how unmap is implemented" doesn't mean it wouldn't be confusing to people... but at least as a stopgap solution it seems fine, and it's certainly better than crashing the process. |
Instead of doing this silently, could you print a one-time warning to the console when a transfer does a copy instead? This seems like something you'd implement to reduce copies, but then you'd end up copying things anyway without knowing. |
Yes, it could definitely come with a console warning (advising you to make a copy to get rid of the warning). |
Editors agreed to go with this direction. |
See gpuweb/gpuweb#2072 (comment) Passes updated tests in gpuweb/cts#1027 Bug: 1243842 Change-Id: I3024b0185b4bdff3daf2ed32ab0f7286a4dbd1ff Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3496685 Reviewed-by: Kentaro Hara <haraken@chromium.org> Reviewed-by: Yuki Shiino <yukishiino@chromium.org> Reviewed-by: Kai Ninomiya <kainino@chromium.org> Commit-Queue: Austin Eng <enga@chromium.org> Cr-Commit-Position: refs/heads/main@{#976902}
This reverts commit 2d525d2. Reason for revert: WebGPUExpectations update needs to re-run generator. Original change's description: > Implement copy-on-transfer semantics for GPUMappedArrayBuffer > > See gpuweb/gpuweb#2072 (comment) > Passes updated tests in gpuweb/cts#1027 > > Bug: 1243842 > Change-Id: I3024b0185b4bdff3daf2ed32ab0f7286a4dbd1ff > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3496685 > Reviewed-by: Kentaro Hara <haraken@chromium.org> > Reviewed-by: Yuki Shiino <yukishiino@chromium.org> > Reviewed-by: Kai Ninomiya <kainino@chromium.org> > Commit-Queue: Austin Eng <enga@chromium.org> > Cr-Commit-Position: refs/heads/main@{#976902} Bug: 1243842 Change-Id: I20459fce5efe4987040f5e3dd9cc1b4338acd27f No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3501104 Auto-Submit: Austin Eng <enga@chromium.org> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> Commit-Queue: Nidhi Jaju <nidhijaju@chromium.org> Owners-Override: Nidhi Jaju <nidhijaju@chromium.org> Cr-Commit-Position: refs/heads/main@{#976943}
This reverts commit 7146831. Reason for revert: Fixed instead by https://chromium-review.googlesource.com/c/chromium/src/+/3499755 Original change's description: > Revert "Implement copy-on-transfer semantics for GPUMappedArrayBuffer" > > This reverts commit 2d525d2. > > Reason for revert: WebGPUExpectations update needs to re-run generator. > > Original change's description: > > Implement copy-on-transfer semantics for GPUMappedArrayBuffer > > > > See gpuweb/gpuweb#2072 (comment) > > Passes updated tests in gpuweb/cts#1027 > > > > Bug: 1243842 > > Change-Id: I3024b0185b4bdff3daf2ed32ab0f7286a4dbd1ff > > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3496685 > > Reviewed-by: Kentaro Hara <haraken@chromium.org> > > Reviewed-by: Yuki Shiino <yukishiino@chromium.org> > > Reviewed-by: Kai Ninomiya <kainino@chromium.org> > > Commit-Queue: Austin Eng <enga@chromium.org> > > Cr-Commit-Position: refs/heads/main@{#976902} > > Bug: 1243842 > Change-Id: I20459fce5efe4987040f5e3dd9cc1b4338acd27f > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3501104 > Auto-Submit: Austin Eng <enga@chromium.org> > Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> > Commit-Queue: Nidhi Jaju <nidhijaju@chromium.org> > Owners-Override: Nidhi Jaju <nidhijaju@chromium.org> > Cr-Commit-Position: refs/heads/main@{#976943} Bug: 1243842 Change-Id: Ie124d05f59c330f8f5a4b1ba223d9608bc786cce Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3500883 Reviewed-by: Kentaro Hara <haraken@chromium.org> Commit-Queue: Austin Eng <enga@chromium.org> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> Reviewed-by: Nidhi Jaju <nidhijaju@chromium.org> Cr-Commit-Position: refs/heads/main@{#976959}
See gpuweb/gpuweb#2072 (comment) Passes updated tests in gpuweb/cts#1027 Bug: 1243842 Change-Id: I3024b0185b4bdff3daf2ed32ab0f7286a4dbd1ff Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3496685 Reviewed-by: Kentaro Hara <haraken@chromium.org> Reviewed-by: Yuki Shiino <yukishiino@chromium.org> Reviewed-by: Kai Ninomiya <kainino@chromium.org> Commit-Queue: Austin Eng <enga@chromium.org> Cr-Commit-Position: refs/heads/main@{#976902} NOKEYCHECK=True GitOrigin-RevId: 2d525d281fa5c1a234baf36310e303cb9c54b697
This reverts commit 2d525d281fa5c1a234baf36310e303cb9c54b697. Reason for revert: WebGPUExpectations update needs to re-run generator. Original change's description: > Implement copy-on-transfer semantics for GPUMappedArrayBuffer > > See gpuweb/gpuweb#2072 (comment) > Passes updated tests in gpuweb/cts#1027 > > Bug: 1243842 > Change-Id: I3024b0185b4bdff3daf2ed32ab0f7286a4dbd1ff > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3496685 > Reviewed-by: Kentaro Hara <haraken@chromium.org> > Reviewed-by: Yuki Shiino <yukishiino@chromium.org> > Reviewed-by: Kai Ninomiya <kainino@chromium.org> > Commit-Queue: Austin Eng <enga@chromium.org> > Cr-Commit-Position: refs/heads/main@{#976902} Bug: 1243842 Change-Id: I20459fce5efe4987040f5e3dd9cc1b4338acd27f No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3501104 Auto-Submit: Austin Eng <enga@chromium.org> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> Commit-Queue: Nidhi Jaju <nidhijaju@chromium.org> Owners-Override: Nidhi Jaju <nidhijaju@chromium.org> Cr-Commit-Position: refs/heads/main@{#976943} NOKEYCHECK=True GitOrigin-RevId: 714683105559d161e173df8420a19c9cb2cecb1f
This reverts commit 714683105559d161e173df8420a19c9cb2cecb1f. Reason for revert: Fixed instead by https://chromium-review.googlesource.com/c/chromium/src/+/3499755 Original change's description: > Revert "Implement copy-on-transfer semantics for GPUMappedArrayBuffer" > > This reverts commit 2d525d281fa5c1a234baf36310e303cb9c54b697. > > Reason for revert: WebGPUExpectations update needs to re-run generator. > > Original change's description: > > Implement copy-on-transfer semantics for GPUMappedArrayBuffer > > > > See gpuweb/gpuweb#2072 (comment) > > Passes updated tests in gpuweb/cts#1027 > > > > Bug: 1243842 > > Change-Id: I3024b0185b4bdff3daf2ed32ab0f7286a4dbd1ff > > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3496685 > > Reviewed-by: Kentaro Hara <haraken@chromium.org> > > Reviewed-by: Yuki Shiino <yukishiino@chromium.org> > > Reviewed-by: Kai Ninomiya <kainino@chromium.org> > > Commit-Queue: Austin Eng <enga@chromium.org> > > Cr-Commit-Position: refs/heads/main@{#976902} > > Bug: 1243842 > Change-Id: I20459fce5efe4987040f5e3dd9cc1b4338acd27f > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3501104 > Auto-Submit: Austin Eng <enga@chromium.org> > Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> > Commit-Queue: Nidhi Jaju <nidhijaju@chromium.org> > Owners-Override: Nidhi Jaju <nidhijaju@chromium.org> > Cr-Commit-Position: refs/heads/main@{#976943} Bug: 1243842 Change-Id: Ie124d05f59c330f8f5a4b1ba223d9608bc786cce Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3500883 Reviewed-by: Kentaro Hara <haraken@chromium.org> Commit-Queue: Austin Eng <enga@chromium.org> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> Reviewed-by: Nidhi Jaju <nidhijaju@chromium.org> Cr-Commit-Position: refs/heads/main@{#976959} NOKEYCHECK=True GitOrigin-RevId: 68365a9dc0a62413d5f2c2a96f926ef540ee6ee2
Please see https://github.com/tc39/proposal-immutable-arraybuffer which proposes Immutable ArrayBuffers, is at Stage 2 of the tc39 process, has enthusiastic support from some implementations, and not (yet?) any serious objections. How applicable would this be to the concerns expressed above? |
^ I've replied on #747 (comment) (the need for read-only ArrayBuffers there is the same root issue) |
Originally posted by @kainino0x in #747 (comment)
The text was updated successfully, but these errors were encountered: