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: use fast api for io read/write sync #15863

Merged
merged 11 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions cli/bench/stdio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## `stdio` benchmarks

Compile the C baseline and run the benchmark:

```bash
cc stdio.c -o stdio -O3
time dd if=/dev/zero bs=65536 count=500000 | ./stdio
time dd if=/dev/zero bs=65536 count=500000 | deno run stdio.js
```
29 changes: 29 additions & 0 deletions cli/bench/stdio/stdio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// From https://github.com/just-js/benchmarks/tree/main/01-stdio
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main(int argc, char *argv[]) {
unsigned int blocksize = 65536;
if (argc == 2) {
blocksize = atoi(argv[1]);
}
char buf[blocksize];
unsigned long size = 0;
unsigned int reads = 0;
int n = read(STDIN_FILENO, buf, blocksize);
while (n > 0) {
reads++;
size += n;
n = read(STDIN_FILENO, buf, blocksize);
}
if (n < 0) {
fprintf(stderr, "read: %s (%i)\n", strerror(errno), errno);
exit(1);
}
fprintf(stdout, "size %lu reads %u blocksize %u\n", size, reads, blocksize);
}
16 changes: 16 additions & 0 deletions cli/bench/stdio/stdio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// From https://github.com/just-js/benchmarks/tree/main/01-stdio
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

const blocksize = parseInt(Deno.args[0] || 65536);
const buf = new Uint8Array(blocksize);
let size = 0;
let reads = 0;

let n = Deno.stdin.readSync(buf);
while (n > 0) {
reads++;
size += n;
n = Deno.stdin.readSync(buf);
}
if (n < 0) throw new Error("Bad Read");
console.log(`size ${size} reads ${reads} blocksize ${blocksize}`);
17 changes: 8 additions & 9 deletions runtime/ops/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use deno_core::OpState;
use deno_core::RcRef;
use deno_core::Resource;
use deno_core::ResourceId;
use deno_core::ZeroCopyBuf;
use once_cell::sync::Lazy;
use std::borrow::Cow;
use std::cell::RefCell;
Expand Down Expand Up @@ -683,32 +682,32 @@ pub fn op_print(
})
}

#[op]
#[op(fast)]
fn op_read_sync(
state: &mut OpState,
rid: ResourceId,
mut buf: ZeroCopyBuf,
rid: u32,
buf: &mut [u8],
) -> Result<u32, AnyError> {
StdFileResource::with_resource(state, rid, move |resource| {
resource.with_inner_and_metadata(|inner, _| {
inner
.read(&mut buf)
.read(buf)
.map(|n: usize| n as u32)
.map_err(AnyError::from)
})
})
}

#[op]
#[op(fast)]
fn op_write_sync(
state: &mut OpState,
rid: ResourceId,
buf: ZeroCopyBuf,
rid: u32,
buf: &mut [u8],
) -> Result<u32, AnyError> {
StdFileResource::with_resource(state, rid, move |resource| {
resource.with_inner_and_metadata(|inner, _| {
inner
.write_and_maybe_flush(&buf)
.write_and_maybe_flush(buf)
.map(|nwritten: usize| nwritten as u32)
.map_err(AnyError::from)
})
Expand Down