1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

perf: use fast api for io read/write sync (#15863)

```
$ dd if=/dev/zero bs=65536 count=500000 | ./stdio # C baseline

500000+0 records in
500000+0 records out
32768000000 bytes transferred in 4.126087 secs (7941664827 bytes/sec)
c: size 32768000000 reads 500000 blocksize 65536
```

```
$ dd if=/dev/zero bs=65536 count=500000 | deno run stdio.js # Deno

500000+0 records in
500000+0 records out
32768000000 bytes transferred in 4.279032 secs (7657806719 bytes/sec)
deno: size 32768000000 reads 500000 blocksize 65536
```
This commit is contained in:
Divy Srivastava 2022-12-01 22:05:18 -08:00 committed by GitHub
parent 9b2b8df927
commit 6982c74e11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 9 deletions

View file

@ -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
cli/bench/stdio/stdio.c Normal file
View file

@ -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
cli/bench/stdio/stdio.js Normal file
View file

@ -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}`);

View file

@ -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;
@ -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)
})