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

refactor(deno/core): Move buffer_ops to deno core & merge logic with json_ops #9457

Merged
merged 7 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
rename 'buffer op' to 'bin op'
  • Loading branch information
inteon committed Mar 20, 2021
commit 489d341bcb9a73920cf2e4b952178f4136a796a7
17 changes: 8 additions & 9 deletions cli/bench/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ pub(crate) fn benchmark(
let hyper_hello_exe = target_path.join("test_server");
let hyper_hello_exe = hyper_hello_exe.to_str().unwrap();

let core_http_buffer_ops_exe =
target_path.join("examples/http_bench_buffer_ops");
let core_http_buffer_ops_exe = core_http_buffer_ops_exe.to_str().unwrap();
let core_http_bin_ops_exe = target_path.join("examples/http_bench_bin_ops");
let core_http_bin_ops_exe = core_http_bin_ops_exe.to_str().unwrap();

let core_http_json_ops_exe = target_path.join("examples/http_bench_json_ops");
let core_http_json_ops_exe = core_http_json_ops_exe.to_str().unwrap();
Expand All @@ -42,11 +41,11 @@ pub(crate) fn benchmark(
"deno_proxy_tcp".to_string(),
deno_tcp_proxy(deno_exe, hyper_hello_exe)?,
);
// "core_http_buffer_ops" was once called "deno_core_single"
// "core_http_buffer_ops" was once called "deno_core_http_bench"
// "core_http_bin_ops" was once called "deno_core_single"
// "core_http_bin_ops" was once called "deno_core_http_bench"
res.insert(
"core_http_buffer_ops".to_string(),
core_http_buffer_ops(core_http_buffer_ops_exe)?,
"core_http_bin_ops".to_string(),
core_http_bin_ops(core_http_bin_ops_exe)?,
);
res.insert(
"core_http_json_ops".to_string(),
Expand Down Expand Up @@ -227,8 +226,8 @@ fn deno_http_proxy(
)
}

fn core_http_buffer_ops(exe: &str) -> Result<HttpBenchmarkResult> {
println!("http_benchmark testing CORE http_bench_buffer_ops");
fn core_http_bin_ops(exe: &str) -> Result<HttpBenchmarkResult> {
println!("http_benchmark testing CORE http_bench_bin_ops");
run(&[exe], 4544, None, None)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
const readErrorStackPattern = new RegExp(
`^.*
at handleError \\(.*core\\.js:.*\\)
at bufferOpParseResult \\(.*core\\.js:.*\\)
at binOpParseResult \\(.*core\\.js:.*\\)
at asyncHandle \\(.*core\\.js:.*\\).*$`,
"ms",
);
Expand All @@ -33,7 +33,7 @@ declare global {
}
}

unitTest(function bufferOpsHeaderTooShort(): void {
unitTest(function binOpsHeaderTooShort(): void {
for (const op of ["op_read_sync", "op_read_async"]) {
const readOpId = Deno.core.ops()[op];
const res = Deno.core.send(
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/unit/unit_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import "./console_test.ts";
import "./copy_file_test.ts";
import "./custom_event_test.ts";
import "./dir_test.ts";
import "./dispatch_buffer_test.ts";
import "./dispatch_bin_test.ts";
import "./dispatch_json_test.ts";
import "./error_stack_test.ts";
import "./event_test.ts";
Expand Down
4 changes: 2 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ smallvec = "1.6.1"
url = { version = "2.2.0", features = ["serde"] }

[[example]]
name = "http_bench_buffer_ops"
path = "examples/http_bench_buffer_ops.rs"
name = "http_bench_bin_ops"
path = "examples/http_bench_bin_ops.rs"

[[example]]
name = "http_bench_json_ops"
Expand Down
2 changes: 1 addition & 1 deletion core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function to trigger the "dispatch" callback in Rust. The user is responsible for
encoding both the request and response into a Uint8Array.

Documentation for this crate is thin at the moment. Please see
[http_bench_buffer_ops.rs](https://github.com/denoland/deno/blob/main/core/examples/http_bench_buffer_ops.rs)
[http_bench_bin_ops.rs](https://github.com/denoland/deno/blob/main/core/examples/http_bench_bin_ops.rs)
and
[http_bench_json_ops.rs](https://github.com/denoland/deno/blob/main/core/examples/http_bench_json_ops.rs)
as a simple example of usage.
Expand Down
28 changes: 14 additions & 14 deletions core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,21 +265,21 @@ SharedQueue Binary Layout
}

////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// Buffer ops handling ///////////////////////////////////
///////////////////////////////////// Bin ops handling /////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////

const bufferRequestHeaderByteLength = 8 + 4;
const scratchBuffer = new ArrayBuffer(bufferRequestHeaderByteLength);
const binRequestHeaderByteLength = 8 + 4;
const scratchBuffer = new ArrayBuffer(binRequestHeaderByteLength);
const scratchView = new DataView(scratchBuffer);

function bufferOpBuildRequest(requestId, argument, zeroCopy) {
function binOpBuildRequest(requestId, argument, zeroCopy) {
scratchView.setBigUint64(0, BigInt(requestId), true);
scratchView.setUint32(8, argument, true);
return [scratchView, ...zeroCopy];
}

function bufferOpParseResult(u8Array, isCopyNeeded) {
// Decode header value from ui8 buffer
function binOpParseResult(u8Array, isCopyNeeded) {
// Decode header value from u8Array
const headerByteLength = 8 + 2 * 4;
assert(u8Array.byteLength >= headerByteLength);
assert(u8Array.byteLength % 4 == 0);
Expand Down Expand Up @@ -319,19 +319,19 @@ SharedQueue Binary Layout
return [requestId, respBuffer, null];
}

function bufferOpAsync(opName, argument = 0, ...zeroCopy) {
function binOpAsync(opName, argument = 0, ...zeroCopy) {
return opAsync(
opName,
(requestId) => bufferOpBuildRequest(requestId, argument, zeroCopy),
bufferOpParseResult,
(requestId) => binOpBuildRequest(requestId, argument, zeroCopy),
binOpParseResult,
);
}

function bufferOpSync(opName, argument = 0, ...zeroCopy) {
function binOpSync(opName, argument = 0, ...zeroCopy) {
return opSync(
opName,
() => bufferOpBuildRequest(0, argument, zeroCopy),
bufferOpParseResult,
() => binOpBuildRequest(0, argument, zeroCopy),
binOpParseResult,
);
}

Expand Down Expand Up @@ -389,8 +389,8 @@ SharedQueue Binary Layout
Object.assign(window.Deno.core, {
jsonOpAsync,
jsonOpSync,
bufferOpAsync,
bufferOpSync,
binOpAsync,
binOpSync,
dispatch,
dispatchByName,
ops,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ const responseBuf = new Uint8Array(

/** Listens on 0.0.0.0:4500, returns rid. */
function listen() {
return Deno.core.bufferOpSync("listen");
return Deno.core.binOpSync("listen");
}

/** Accepts a connection, returns rid. */
function accept(rid) {
return Deno.core.bufferOpAsync("accept", rid);
return Deno.core.binOpAsync("accept", rid);
}

/**
* Reads a packet from the rid, presumably an http request. data is ignored.
* Returns bytes read.
*/
function read(rid, data) {
return Deno.core.bufferOpAsync("read", rid, data);
return Deno.core.binOpAsync("read", rid, data);
}

/** Writes a fixed HTTP response to the socket rid. Returns bytes written. */
function write(rid, data) {
return Deno.core.bufferOpAsync("write", rid, data);
return Deno.core.binOpAsync("write", rid, data);
}

function close(rid) {
Deno.core.bufferOpSync("close", rid);
Deno.core.binOpSync("close", rid);
}

async function serve(rid) {
Expand All @@ -57,7 +57,7 @@ async function main() {

const listenerRid = listen();
Deno.core.print(
`http_bench_buffer_ops listening on https://127.0.0.1:4544/\n`,
`http_bench_bin_ops listening on https://127.0.0.1:4544/\n`,
);

for (;;) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ impl From<tokio::net::TcpStream> for TcpStream {

fn create_js_runtime() -> JsRuntime {
let mut runtime = JsRuntime::new(Default::default());
runtime.register_op("listen", deno_core::buffer_op_sync(op_listen));
runtime.register_op("close", deno_core::buffer_op_sync(op_close));
runtime.register_op("accept", deno_core::buffer_op_async(op_accept));
runtime.register_op("read", deno_core::buffer_op_async(op_read));
runtime.register_op("write", deno_core::buffer_op_async(op_write));
runtime.register_op("listen", deno_core::bin_op_sync(op_listen));
runtime.register_op("close", deno_core::bin_op_sync(op_close));
runtime.register_op("accept", deno_core::bin_op_async(op_accept));
runtime.register_op("read", deno_core::bin_op_async(op_read));
runtime.register_op("write", deno_core::bin_op_async(op_write));
runtime
}

Expand Down Expand Up @@ -222,8 +222,8 @@ fn main() {
let future = async move {
js_runtime
.execute(
"http_bench_buffer_ops.js",
include_str!("http_bench_buffer_ops.js"),
"http_bench_bin_ops.js",
include_str!("http_bench_bin_ops.js"),
)
.unwrap();
js_runtime.run_event_loop().await
Expand Down
8 changes: 4 additions & 4 deletions core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod module_specifier;
mod modules;
mod normalize_path;
mod ops;
mod ops_buffer;
mod ops_bin;
mod ops_json;
pub mod plugin_api;
mod resources;
Expand Down Expand Up @@ -68,9 +68,9 @@ pub use crate::ops::OpFn;
pub use crate::ops::OpId;
pub use crate::ops::OpState;
pub use crate::ops::OpTable;
pub use crate::ops_buffer::buffer_op_async;
pub use crate::ops_buffer::buffer_op_sync;
pub use crate::ops_buffer::ValueOrVector;
pub use crate::ops_bin::bin_op_async;
pub use crate::ops_bin::bin_op_sync;
pub use crate::ops_bin::ValueOrVector;
pub use crate::ops_json::json_op_async;
pub use crate::ops_json::json_op_sync;
pub use crate::resources::Resource;
Expand Down
8 changes: 4 additions & 4 deletions core/ops_buffer.rs → core/ops_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,18 @@ fn gen_padding_32bit(len: usize) -> &'static [u8] {
/// When registering an op like this...
/// ```ignore
/// let mut runtime = JsRuntime::new(...);
/// runtime.register_op("hello", deno_core::buffer_op_sync(Self::hello_op));
/// runtime.register_op("hello", deno_core::bin_op_sync(Self::hello_op));
/// ```
///
/// ...it can be invoked from JS using the provided name, for example:
/// ```js
/// Deno.core.ops();
/// let result = Deno.core.bufferOpSync("function_name", args);
/// let result = Deno.core.binOpSync("function_name", args);
/// ```
///
/// The `Deno.core.ops()` statement is needed once before any op calls, for initialization.
/// A more complete example is available in the examples directory.
pub fn buffer_op_sync<F, R>(op_fn: F) -> Box<OpFn>
pub fn bin_op_sync<F, R>(op_fn: F) -> Box<OpFn>
where
F: Fn(&mut OpState, u32, &mut [ZeroCopyBuf]) -> Result<R, AnyError> + 'static,
R: ValueOrVector,
Expand Down Expand Up @@ -202,7 +202,7 @@ where
///
/// The `Deno.core.ops()` statement is needed once before any op calls, for initialization.
/// A more complete example is available in the examples directory.
pub fn buffer_op_async<F, R, RV>(op_fn: F) -> Box<OpFn>
pub fn bin_op_async<F, R, RV>(op_fn: F) -> Box<OpFn>
where
F: Fn(Rc<RefCell<OpState>>, u32, BufVec) -> R + 'static,
R: Future<Output = Result<RV, AnyError>> + 'static,
Expand Down
2 changes: 1 addition & 1 deletion runtime/js/11_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

const nowBytes = new Uint8Array(8);
function opNow() {
core.bufferOpSync("op_now", 0, nowBytes);
core.binOpSync("op_now", 0, nowBytes);
return new DataView(nowBytes.buffer).getFloat64();
}

Expand Down
8 changes: 4 additions & 4 deletions runtime/js/12_io.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
return 0;
}

const nread = core.bufferOpSync("op_read_sync", rid, buffer);
const nread = core.binOpSync("op_read_sync", rid, buffer);
if (nread < 0) {
throw new Error("read error");
}
Expand All @@ -97,7 +97,7 @@
return 0;
}

const nread = await core.bufferOpAsync("op_read_async", rid, buffer);
const nread = await core.binOpAsync("op_read_async", rid, buffer);
if (nread < 0) {
throw new Error("read error");
}
Expand All @@ -106,7 +106,7 @@
}

function writeSync(rid, data) {
const result = core.bufferOpSync("op_write_sync", rid, data);
const result = core.binOpSync("op_write_sync", rid, data);
if (result < 0) {
throw new Error("write error");
}
Expand All @@ -115,7 +115,7 @@
}

async function write(rid, data) {
const result = await core.bufferOpAsync("op_write_async", rid, data);
const result = await core.binOpAsync("op_write_async", rid, data);
if (result < 0) {
throw new Error("write error");
}
Expand Down
8 changes: 4 additions & 4 deletions runtime/ops/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ lazy_static! {
}

pub fn init(rt: &mut JsRuntime) {
super::reg_buffer_async(rt, "op_read_async", op_read_async);
super::reg_buffer_async(rt, "op_write_async", op_write_async);
super::reg_bin_async(rt, "op_read_async", op_read_async);
super::reg_bin_async(rt, "op_write_async", op_write_async);

super::reg_buffer_sync(rt, "op_read_sync", op_read_sync);
super::reg_buffer_sync(rt, "op_write_sync", op_write_sync);
super::reg_bin_sync(rt, "op_read_sync", op_read_sync);
super::reg_bin_sync(rt, "op_write_sync", op_write_sync);

super::reg_json_async(rt, "op_shutdown", op_shutdown);
}
Expand Down
17 changes: 7 additions & 10 deletions runtime/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub mod websocket;
pub mod worker_host;

use crate::metrics::metrics_op;
use deno_core::buffer_op_async;
use deno_core::buffer_op_sync;
use deno_core::bin_op_async;
use deno_core::bin_op_sync;
use deno_core::error::AnyError;
use deno_core::json_op_async;
use deno_core::json_op_sync;
Expand Down Expand Up @@ -62,24 +62,21 @@ where
rt.register_op(name, metrics_op(name, json_op_sync(op_fn)));
}

pub fn reg_buffer_async<F, R, RV>(
rt: &mut JsRuntime,
name: &'static str,
op_fn: F,
) where
pub fn reg_bin_async<F, R, RV>(rt: &mut JsRuntime, name: &'static str, op_fn: F)
where
F: Fn(Rc<RefCell<OpState>>, u32, BufVec) -> R + 'static,
R: Future<Output = Result<RV, AnyError>> + 'static,
RV: ValueOrVector,
{
rt.register_op(name, metrics_op(name, buffer_op_async(op_fn)));
rt.register_op(name, metrics_op(name, bin_op_async(op_fn)));
}

pub fn reg_buffer_sync<F, R>(rt: &mut JsRuntime, name: &'static str, op_fn: F)
pub fn reg_bin_sync<F, R>(rt: &mut JsRuntime, name: &'static str, op_fn: F)
where
F: Fn(&mut OpState, u32, &mut [ZeroCopyBuf]) -> Result<R, AnyError> + 'static,
R: ValueOrVector,
{
rt.register_op(name, metrics_op(name, buffer_op_sync(op_fn)));
rt.register_op(name, metrics_op(name, bin_op_sync(op_fn)));
}

/// `UnstableChecker` is a struct so it can be placed inside `GothamState`;
Expand Down
2 changes: 1 addition & 1 deletion runtime/ops/timers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn init(rt: &mut deno_core::JsRuntime) {
super::reg_json_sync(rt, "op_global_timer_stop", op_global_timer_stop);
super::reg_json_sync(rt, "op_global_timer_start", op_global_timer_start);
super::reg_json_async(rt, "op_global_timer", op_global_timer);
super::reg_buffer_sync(rt, "op_now", op_now);
super::reg_bin_sync(rt, "op_now", op_now);
super::reg_json_sync(rt, "op_sleep_sync", op_sleep_sync);
}

Expand Down