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

Introduce a "transferable" algorithm for BufferSource #1419

Merged
merged 2 commits into from
Jul 19, 2024

Conversation

inexorabletash
Copy link
Member

@inexorabletash inexorabletash commented Jul 16, 2024

The new "transferable" algorithm allows an invoker to determine if a BufferSource can be detached without errors before invoking transfer. This is particularly helpful if several buffers are processed in a batch, as the validity of the operation can be determined up front, rather than during processing which could leave the buffers in a mix of states.

See discussion in [1]. A local copy of the algorithm was landed as [2] (as "detachable") but this seems like a useful addition to WebIDL.

1: webmachinelearning/webnn#351
2: https://webmachinelearning.github.io/webnn/#buffersource-detachable

Eliding the checklist since this is a helper algorithm not new functionality, but please ask if more details are desired


Preview | Diff

The new "detachable" algorithm allows an invoker to determine if a BufferSource is detachable
without errors before invoking transfer. This is particularly helpful if several buffers are
processed in a batch, as the validity of the operation can be determined up front, rather than
during processing which could leave the buffers in a mix of states.

See discussion in [1]. A local copy of the algorithm was landed as [2] but this seems like a useful
addition to WebIDL.

1: webmachinelearning/webnn#351
2: https://webmachinelearning.github.io/webnn/#buffersource-detachable
@inexorabletash
Copy link
Member Author

@domenic - could you please take a look? Low priority.

Also, feedback on whether the checklist (re: implementation commitments, etc) is needed in this case would helpful.

Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, happy to have this here. Just one question.

I don't think the PR template is necessary for this kind of definition.

1. If |jsArrayBuffer| has a \[[ViewedArrayBuffer]] internal slot, then set |jsArrayBuffer| to
|jsArrayBuffer|.\[[ViewedArrayBuffer]].
1. If [$IsSharedArrayBuffer$](|jsArrayBuffer|) is true, then return false.
1. If [$IsDetachedBuffer$](|jsArrayBuffer|) is true, then return false.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this step is a bit surprising. Is it helpful for your use case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully I'm not misunderstanding your question. The use case is over at https://webmachinelearning.github.io/webnn/#mlnamedarraybufferviews-transfer

Shorter version for explanatory purposes:

  1. For each buffer of buffers:
    1. If buffer is not detachable, then throw a TypeError.
  2. For each buffer of buffers:
    1. Let xfer be the result of transferring buffer's underlying buffer.

The "IsDetachedBuffer" check is to ensure that transferring wouldn't assert ("Assert: IsDetachedBuffer(jsArrayBuffer) is false").

This could alternately be done by the caller, e.g.:

  1. For each buffer of buffers:
    1. If buffer has a [[ViewedArrayBuffer]] internal slot, then set buffer to buffer.[[ViewedArrayBuffer]].
    2. If IsDetachedBuffer(buffer) is true, then throw a TypeError.
    3. If buffer is not detachable, then throw a TypeError.
  2. For each buffer of buffers:
    1. Let xfer be the result of transferring buffer's underlying buffer.

... but it seems we'd want to keep this logic together in WebIDL. Would it be clearer if we named the new algorithm transferable instead?

(But maybe I'm answering the wrong question?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, sorry for being brief.

What I mean is that even if IsDetachedBuffer() returns true, DetachArrayBuffer() will still work. (It will just be a no-op.) So it's a bit surprising to include this step, since if it was missing, the algorithm would still give a correct answer to "is this detachable?"

And yeah, I think changing it to "transferable", while keeping the check, would be perfect. That reflects the actual use case.

(Also, it's strange that we assert instead of throw for detached array buffers in "transfer". I just checked all usages and it seems like it'd be safe, and perhaps an improvement, to update the algorithm to throw instead. But we can do that separately.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay - updated algorithm name to "transferable".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and updated PR title and description

@inexorabletash inexorabletash changed the title Introduce a "detachable" algorithm for BufferSource Introduce a "transferable" algorithm for BufferSource Jul 18, 2024
@domenic domenic merged commit d6927d5 into whatwg:main Jul 19, 2024
2 checks passed
@inexorabletash inexorabletash deleted the buffersource-detachable branch July 19, 2024 01:26
inexorabletash added a commit to inexorabletash/webnn that referenced this pull request Jul 19, 2024
As promised, the local algorithm "detachable" migrated to WebIDL[1],
so we can drop our copy of the algorithm and reference WebIDL's
version instead, which is identical except for a name change[3].

(If you're building the spec locally and run into errors, force
a Bikeshed update[2] to ensure you have the latest indexes.)

1: whatwg/webidl@d6927d5
2: https://speced.github.io/bikeshed/#updating-bikeshed
3: whatwg/webidl#1419 (comment)

Ref: webmachinelearning#351
fdwr pushed a commit to webmachinelearning/webnn that referenced this pull request Jul 19, 2024
As promised, the local algorithm "detachable" migrated to WebIDL[1],
so we can drop our copy of the algorithm and reference WebIDL's
version instead, which is identical except for a name change[3].

(If you're building the spec locally and run into errors, force
a Bikeshed update[2] to ensure you have the latest indexes.)

1: whatwg/webidl@d6927d5
2: https://speced.github.io/bikeshed/#updating-bikeshed
3: whatwg/webidl#1419 (comment)

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

Successfully merging this pull request may close these issues.

None yet

2 participants