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(web): optimize encodeInto() #15922

Merged
merged 10 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions cli/bench/encode_into.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
const queueMicrotask = globalThis.queueMicrotask || process.nextTick;
let [total, count] = typeof Deno !== "undefined"
? Deno.args
: [process.argv[2], process.argv[3]];

total = total ? parseInt(total, 0) : 50;
count = count ? parseInt(count, 10) : 1000000;

function bench(fun) {
const start = Date.now();
for (let i = 0; i < count; i++) fun();
const elapsed = Date.now() - start;
const rate = Math.floor(count / (elapsed / 1000));
console.log(`time ${elapsed} ms rate ${rate}`);
if (--total) queueMicrotask(() => bench(fun));
}

const encoder = new TextEncoder();
const data = "hello world";
const out = new Uint8Array(100);

bench(() => encoder.encodeInto(data, out));
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ serde_json = { version = "1.0.79", features = ["preserve_order"] }
serde_v8 = { version = "0.61.0", path = "../serde_v8" }
sourcemap = "=6.0.1"
url = { version = "2.3.1", features = ["serde", "expose_internals"] }
v8 = { version = "0.49.0", default-features = false }
v8 = { version = "0.49.0", default-features = false, path = "../../rusty_v8" }

[[example]]
name = "http_bench_json_ops"
Expand Down
2 changes: 1 addition & 1 deletion core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ impl JsRuntime {
{
let scope = &mut self.handle_scope();
let o = Self::grab_global::<v8::Object>(scope, "Deno.core.ops").unwrap();
let names = o.get_own_property_names(scope).unwrap();
let names = o.get_own_property_names(scope, Default::default()).unwrap();
for i in 0..names.length() {
let key = names.get_index(scope, i).unwrap();
o.delete(scope, key);
Expand Down
8 changes: 7 additions & 1 deletion ext/web/08_text_encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,16 @@
context: "Argument 2",
allowShared: true,
});
return ops.op_encoding_encode_into(source, destination);
ops.op_encoding_encode_into(source, destination, encodeIntoBuf);
return {
read: encodeIntoBuf[0],
written: encodeIntoBuf[1],
};
}
}

const encodeIntoBuf = new Uint32Array(2);

webidl.configurePrototype(TextEncoder);
const TextEncoderPrototype = TextEncoder.prototype;

Expand Down
61 changes: 23 additions & 38 deletions ext/web/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::include_js_files;
use deno_core::op;
use deno_core::serde_v8;
use deno_core::url::Url;
use deno_core::v8;
use deno_core::ByteString;
use deno_core::CancelHandle;
use deno_core::Extension;
Expand All @@ -19,11 +21,11 @@ use deno_core::Resource;
use deno_core::ResourceId;
use deno_core::U16String;
use deno_core::ZeroCopyBuf;

use encoding_rs::CoderResult;
use encoding_rs::Decoder;
use encoding_rs::DecoderResult;
use encoding_rs::Encoding;
use serde::Serialize;
use std::borrow::Cow;
use std::cell::RefCell;
use std::fmt;
Expand Down Expand Up @@ -314,46 +316,29 @@ impl Resource for TextDecoderResource {
}
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct EncodeIntoResult {
read: usize,
written: usize,
}
littledivy marked this conversation as resolved.
Show resolved Hide resolved

#[op]
#[op(v8)]
fn op_encoding_encode_into(
input: String,
scope: &mut v8::HandleScope,
input: serde_v8::Value,
buffer: &mut [u8],
) -> EncodeIntoResult {
// Since `input` is already UTF-8, we can simply find the last UTF-8 code
// point boundary from input that fits in `buffer`, and copy the bytes up to
// that point.
let boundary = if buffer.len() >= input.len() {
input.len()
} else {
let mut boundary = buffer.len();

// The maximum length of a UTF-8 code point is 4 bytes.
for _ in 0..4 {
if input.is_char_boundary(boundary) {
break;
}
debug_assert!(boundary > 0);
boundary -= 1;
}

debug_assert!(input.is_char_boundary(boundary));
boundary
out_buf: &mut [u8],
) {
let s = v8::Local::<v8::String>::try_from(input.v8_value).unwrap();
littledivy marked this conversation as resolved.
Show resolved Hide resolved
assert!(out_buf.len() % 4 == 0);
// SAFETY: `out_buf` is guaranteed to be aligned to 4 bytes.
littledivy marked this conversation as resolved.
Show resolved Hide resolved
let out_buf: &mut [u32] = unsafe {
std::slice::from_raw_parts_mut(
out_buf.as_mut_ptr() as *mut u32,
out_buf.len() / std::mem::size_of::<u32>(),
)
littledivy marked this conversation as resolved.
Show resolved Hide resolved
};

buffer[..boundary].copy_from_slice(input[..boundary].as_bytes());

EncodeIntoResult {
// The `read` output parameter is measured in UTF-16 code units.
read: input[..boundary].encode_utf16().count(),
written: boundary,
}
out_buf[1] = s.write_utf8(
scope,
buffer,
Some(&mut (out_buf[0] as usize)),
littledivy marked this conversation as resolved.
Show resolved Hide resolved
v8::WriteOptions::NO_NULL_TERMINATION
| v8::WriteOptions::REPLACE_INVALID_UTF8,
) as u32;
}

/// Creates a [`CancelHandle`] resource that can be used to cancel invocations of certain ops.
Expand Down
41 changes: 18 additions & 23 deletions ops/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,31 +729,26 @@ fn codegen_arg(
return quote! {
let #ident = {
let value = args.get(#idx as i32);
if let Ok(view) = #core::v8::Local::<#core::v8::ArrayBufferView>::try_from(value) {
let (offset, len) = (view.byte_offset(), view.byte_length());
let buffer = match view.buffer(scope) {
Some(v) => v,
None => {
return #core::_ops::throw_type_error(scope, format!("Expected ArrayBufferView at position {}", #idx));
}
};
let store = buffer.get_backing_store();
if store.is_shared() {
return #core::_ops::throw_type_error(scope, format!("Expected non-shared ArrayBufferView at position {}", #idx));
}
unsafe { #mutability *(&store[offset..offset + len] as *const _ as *mut #ptr_ty) }
} else {
let b: #core::v8::Local<#core::v8::ArrayBuffer> = match value.try_into() {
Ok(v) => v,
Err(_) => {
return #core::_ops::throw_type_error(scope, format!("Expected ArrayBuffer at position {}", #idx));
match #core::v8::Local::<#core::v8::ArrayBuffer>::try_from(value) {
Ok(b) => {
let store = b.data() as *mut u8;
unsafe { ::std::slice::from_raw_parts_mut(store, b.byte_length()) }
},
Err(_) => {
if let Ok(view) = #core::v8::Local::<#core::v8::ArrayBufferView>::try_from(value) {
let (offset, len) = (view.byte_offset(), view.byte_length());
let buffer = match view.buffer(scope) {
Some(v) => v,
None => {
return #core::_ops::throw_type_error(scope, format!("Expected ArrayBufferView at position {}", #idx));
}
};
let store = buffer.data() as *mut u8;
unsafe { ::std::slice::from_raw_parts_mut(store.add(offset), len) }
} else {
return #core::_ops::throw_type_error(scope, format!("Expected ArrayBufferView at position {}", #idx));
}
};
let store = b.get_backing_store();
if store.is_shared() {
return #core::_ops::throw_type_error(scope, format!("Expected non-shared ArrayBufferView at position {}", #idx));
}
unsafe { #mutability *(&store[0..b.byte_length()] as *const _ as *mut #ptr_ty) }
}
};
};
Expand Down
2 changes: 1 addition & 1 deletion serde_v8/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ derive_more = "0.99.17"
serde = { version = "1.0.136", features = ["derive"] }
serde_bytes = "0.11"
smallvec = { version = "1.8", features = ["union"] }
v8 = { version = "0.49.0", default-features = false }
v8 = { version = "0.49.0", default-features = false, path = "../../rusty_v8" }

[dev-dependencies]
bencher = "0.1"
Expand Down
6 changes: 4 additions & 2 deletions serde_v8/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ impl<'de, 'a, 'b, 's, 'x> de::Deserializer<'de>
};
visitor.visit_map(map)
} else {
let prop_names = obj.get_own_property_names(self.scope);
let prop_names =
obj.get_own_property_names(self.scope, Default::default());
let keys: Vec<magic::Value> = match prop_names {
Some(names) => from_v8(self.scope, names.into()).unwrap(),
None => vec![],
Expand Down Expand Up @@ -410,7 +411,8 @@ impl<'de, 'a, 'b, 's, 'x> de::Deserializer<'de>
let obj = v8::Local::<v8::Object>::try_from(self.input).unwrap();
// Unpack single-key
let tag = {
let prop_names = obj.get_own_property_names(self.scope);
let prop_names =
obj.get_own_property_names(self.scope, Default::default());
let prop_names = prop_names.ok_or(Error::ExpectedEnum)?;
if prop_names.length() != 1 {
return Err(Error::LengthMismatch);
Expand Down