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

perf(ext/web): optimize transferArrayBuffer #16294

Merged
merged 8 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions ext/web/06_streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
WeakMapPrototypeSet,
} = globalThis.__bootstrap.primordials;
const consoleInternal = window.__bootstrap.console;
const ops = core.ops;
const { AssertionError, assert } = window.__bootstrap.infra;

/** @template T */
Expand Down Expand Up @@ -218,15 +219,8 @@
* @returns {ArrayBufferLike}
*/
function transferArrayBuffer(O) {
assert(!isDetachedBuffer(O));
const transferredIshVersion = O.slice(0);
ObjectDefineProperty(O, "byteLength", {
get() {
return 0;
},
});
O[isFakeDetached] = true;
return transferredIshVersion;
const v = ops.op_transfer_arraybuffer(O);
v[isFakeDetached] = true;
Copy link
Contributor

@marcosc90 marcosc90 Oct 15, 2022

Choose a reason for hiding this comment

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

Suggested change
v[isFakeDetached] = true;
v[isFakeDetached] = true;
return v;

CI is failing because v is not being returned.

Copy link
Contributor

@marcosc90 marcosc90 Oct 15, 2022

Choose a reason for hiding this comment

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

deno/ext/web/06_streams.js

Lines 196 to 198 in 2300a70

function isDetachedBuffer(O) {
return ReflectHas(O, isFakeDetached);
}

And maybe we can now change that method to:

function isDetachedBuffer(O) { 
  return O.byteLength === 0;
} 

Although it may trigger some false positives.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm keeping isFakeDetached to not touch a lot of code.

Copy link
Member

Choose a reason for hiding this comment

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

I'm keeping isFakeDetached to not touch a lot of code.

Could you fix this properly rather than half fixing it just enough so that WPT passes? 😉

Copy link
Member Author

Choose a reason for hiding this comment

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

@lucacasonato #16307 @marcosc90 opened a PR to do that as a follow up to this one.

}

/**
Expand Down Expand Up @@ -4572,26 +4566,30 @@
* @param {UnderlyingSource<R>=} underlyingSource
* @param {QueuingStrategy<R>=} strategy
*/
constructor(underlyingSource = undefined, strategy = {}) {
constructor(underlyingSource = undefined, strategy = undefined) {
const prefix = "Failed to construct 'ReadableStream'";
if (underlyingSource !== undefined) {
underlyingSource = webidl.converters.object(underlyingSource, {
prefix,
context: "Argument 1",
});
}
strategy = webidl.converters.QueuingStrategy(strategy, {
prefix,
context: "Argument 2",
});
if (strategy !== undefined) {
strategy = webidl.converters.QueuingStrategy(strategy, {
prefix,
context: "Argument 2",
});
} else {
strategy = {};
}
lucacasonato marked this conversation as resolved.
Show resolved Hide resolved
this[webidl.brand] = webidl.brand;
if (underlyingSource === undefined) {
underlyingSource = null;
let underlyingSourceDict = {};
if (underlyingSource !== undefined) {
underlyingSourceDict = webidl.converters.UnderlyingSource(
underlyingSource,
{ prefix, context: "underlyingSource" },
);
}
Copy link
Member

Choose a reason for hiding this comment

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

      if (underlyingSource === undefined) {
        underlyingSource = null;
      }

This check is now not present at all anymore. underlyingSource is passed to a child function further down.

const underlyingSourceDict = webidl.converters.UnderlyingSource(
underlyingSource,
{ prefix, context: "underlyingSource" },
);
initializeReadableStream(this);
if (underlyingSourceDict.type === "bytes") {
if (strategy.size !== undefined) {
Expand Down Expand Up @@ -4652,13 +4650,17 @@
* @param {ReadableStreamGetReaderOptions=} options
* @returns {ReadableStreamDefaultReader<R> | ReadableStreamBYOBReader}
*/
getReader(options = {}) {
getReader(options = undefined) {
webidl.assertBranded(this, ReadableStreamPrototype);
const prefix = "Failed to execute 'getReader' on 'ReadableStream'";
options = webidl.converters.ReadableStreamGetReaderOptions(options, {
prefix,
context: "Argument 1",
});
if (options !== undefined) {
options = webidl.converters.ReadableStreamGetReaderOptions(options, {
prefix,
context: "Argument 1",
});
} else {
options = {};
}
if (options.mode === undefined) {
return acquireReadableStreamDefaultReader(this);
} else {
Expand Down
18 changes: 18 additions & 0 deletions ext/web/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub fn init<P: TimersPermission + 'static>(
op_timer_handle::decl(),
op_cancel_handle::decl(),
op_sleep::decl(),
op_transfer_arraybuffer::decl(),
])
.state(move |state| {
state.put(blob_store.clone());
Expand Down Expand Up @@ -337,6 +338,23 @@ fn op_encoding_encode_into(
Ok(())
}

#[op(v8)]
fn op_transfer_arraybuffer<'a>(
scope: &mut v8::HandleScope<'a>,
input: serde_v8::Value<'a>,
) -> Result<serde_v8::Value<'a>, AnyError> {
let ab = v8::Local::<v8::ArrayBuffer>::try_from(input.v8_value)?;
if !ab.is_detachable() {
return Err(type_error("ArrayBuffer is not detachable"));
}
let bs = ab.get_backing_store();
ab.detach();
let ab = v8::ArrayBuffer::with_backing_store(scope, &bs);
Ok(serde_v8::Value {
v8_value: ab.into(),
})
}

/// Creates a [`CancelHandle`] resource that can be used to cancel invocations of certain ops.
#[op]
pub fn op_cancel_handle(state: &mut OpState) -> ResourceId {
Expand Down