From 1359062cdb2a45e59ce36a3b95d358e2660da840 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sun, 11 Sep 2022 10:51:23 +0530 Subject: [PATCH 1/7] perf: use fast api for stdio read/write --- cli/bench/stdio.js | 13 +++++++++++++ runtime/js/12_io.js | 4 ++-- runtime/ops/io.rs | 24 ++++++++++++------------ stdio | Bin 0 -> 33906 bytes stdio.c | 26 ++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 cli/bench/stdio.js create mode 100755 stdio create mode 100644 stdio.c diff --git a/cli/bench/stdio.js b/cli/bench/stdio.js new file mode 100644 index 00000000000000..6827e783353b5c --- /dev/null +++ b/cli/bench/stdio.js @@ -0,0 +1,13 @@ +const blocksize = parseInt(Deno.args[0] || 65536) +const buf = new ArrayBuffer(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}`) \ No newline at end of file diff --git a/runtime/js/12_io.js b/runtime/js/12_io.js index e41657d9678c07..516401a3749f2d 100644 --- a/runtime/js/12_io.js +++ b/runtime/js/12_io.js @@ -92,7 +92,7 @@ return 0; } - const nread = ops.op_read_sync(rid, buffer); + const nread = ops.op_read_sync.fast(rid, buffer); return nread === 0 ? null : nread; } @@ -108,7 +108,7 @@ } function writeSync(rid, data) { - return ops.op_write_sync(rid, data); + return ops.op_write_sync.fast(rid, data); } function write(rid, data) { diff --git a/runtime/ops/io.rs b/runtime/ops/io.rs index ce2ed252f9bf08..641e0063458b48 100644 --- a/runtime/ops/io.rs +++ b/runtime/ops/io.rs @@ -673,34 +673,34 @@ pub fn op_print( }) } -#[op] +#[op(fast)] fn op_read_sync( state: &mut OpState, - rid: ResourceId, - mut buf: ZeroCopyBuf, -) -> Result { + rid: u32, + buf: &mut [u8], +) -> u32 { 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) }) - }) + }).unwrap() } -#[op] +#[op(fast)] fn op_write_sync( state: &mut OpState, - rid: ResourceId, - buf: ZeroCopyBuf, -) -> Result { + rid: u32, + buf: &mut [u8], +) -> u32 { 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) }) - }) + }).unwrap() } diff --git a/stdio b/stdio new file mode 100755 index 0000000000000000000000000000000000000000..60cd6908643254c30586e8fb850470c1bba67095 GIT binary patch literal 33906 zcmeI5U2IfU5XWctmeN9%1xkZ_*sD@P0VzRB4T*FMG=i9xlug(`@MPQFwwsnN-4FRH zxV8anz=zhxXo8_cgKUEd5iuAiQUG0XJU*je-y&3Sdt-1g?(GXmV+iaU(XBXW?a^y*ynwXND4(q zY#2&~!jbaONGLhq!a^&bHG=*4d}&rbFf>G|icmP{ud5pI6ZhL}<;%1B*^ig_+#FXb zQn%S(=T-i?%7Eb~<~wfX<1=DmKRJilV?OyD-i0c)yvS9gmX(&QF;By3y##0tNoq9& zSsAMXs#H}V?ie1J&yBH$IL-N%N2xGI4?Ae@zGpA5ELl~&eC6;v5=~kce3qc~LhyR5 zqc-9TiK4tdKIMKi&slyy{Q2;i&1C5O*NGlOyQ0zLF&6e}XuR-PR_=SCVE%;rbUu!l z4(0bEFA@so)%wfxyjyF%@L8rpc^$L%t?azKbMe7w;a~Y{dfHnTAf`jjJz=(%Vc5)` z=!|_fp2q$RAM>$4&cp9{yzuWn59L1IgXYb~-^g}%Em+T*t)Z}Q;* zIsYv1Nkf}!`OJ%DI+Xi+uq+IVuO*_;E1nx^+>r%^r1 zJ6(E;a{%StZKxN`)TzDfrbe@`YZE(o{Kdz~P4>d>UX5D(OQ6mn%DK8d22x>ITVv9I(!@#B{8IHLNwLqjg7)?dKC(VKza zz?z1dsO$Cxirvy^;J|3w#C@ojZbQ7~P}7Ef_S46bI~QXMZE3%GG(xp-wCoh-4Q^kG z{T9V;ul@SaX-#$W@AEonaA*OAv4*xjjXF@{+m;5u@0nAv0~$Ga zY!CK>diXXlYtfN|$Dr)E4fU@_9I4I$vS)W2_Lw+(N}T;{J7^#YQf?hM^#e_|k z8Hj{odcpxeDc=@8Xt# ztR#moLeQvZyS2SxgTv!&xb*?ToU)+fVDOy_?DJg=XxTL6Sv=!?U`gNAixHw0Dj@vY`0P=HEYfJLS8h=Usj0_Fp=G@GZxT$%j9F?eHh=z_-^cPp4e& z>Z-AqxdX`E-yfKIG&TR`t;VyyA9}Zz?CbupYDaeNOK)`cJS_iz?Z0}VV`xL{ g*Y|JK+9qtu&s*NN_uQ4nzUCi)X>b0sw>W#!KjUg54*&oF literal 0 HcmV?d00001 diff --git a/stdio.c b/stdio.c new file mode 100644 index 00000000000000..abd0646c34aa5d --- /dev/null +++ b/stdio.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +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); +} \ No newline at end of file From 9539b3ddb1dc479c289ee82fe215de3cd809b0ad Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sun, 11 Sep 2022 15:38:07 +0530 Subject: [PATCH 2/7] fixes --- cli/bench/stdio/README.md | 9 +++++++++ stdio.c => cli/bench/stdio/stdio.c | 0 cli/bench/{ => stdio}/stdio.js | 0 stdio | Bin 33906 -> 0 bytes 4 files changed, 9 insertions(+) create mode 100644 cli/bench/stdio/README.md rename stdio.c => cli/bench/stdio/stdio.c (100%) rename cli/bench/{ => stdio}/stdio.js (100%) delete mode 100755 stdio diff --git a/cli/bench/stdio/README.md b/cli/bench/stdio/README.md new file mode 100644 index 00000000000000..5d3a7137009b9a --- /dev/null +++ b/cli/bench/stdio/README.md @@ -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 +``` \ No newline at end of file diff --git a/stdio.c b/cli/bench/stdio/stdio.c similarity index 100% rename from stdio.c rename to cli/bench/stdio/stdio.c diff --git a/cli/bench/stdio.js b/cli/bench/stdio/stdio.js similarity index 100% rename from cli/bench/stdio.js rename to cli/bench/stdio/stdio.js diff --git a/stdio b/stdio deleted file mode 100755 index 60cd6908643254c30586e8fb850470c1bba67095..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 33906 zcmeI5U2IfU5XWctmeN9%1xkZ_*sD@P0VzRB4T*FMG=i9xlug(`@MPQFwwsnN-4FRH zxV8anz=zhxXo8_cgKUEd5iuAiQUG0XJU*je-y&3Sdt-1g?(GXmV+iaU(XBXW?a^y*ynwXND4(q zY#2&~!jbaONGLhq!a^&bHG=*4d}&rbFf>G|icmP{ud5pI6ZhL}<;%1B*^ig_+#FXb zQn%S(=T-i?%7Eb~<~wfX<1=DmKRJilV?OyD-i0c)yvS9gmX(&QF;By3y##0tNoq9& zSsAMXs#H}V?ie1J&yBH$IL-N%N2xGI4?Ae@zGpA5ELl~&eC6;v5=~kce3qc~LhyR5 zqc-9TiK4tdKIMKi&slyy{Q2;i&1C5O*NGlOyQ0zLF&6e}XuR-PR_=SCVE%;rbUu!l z4(0bEFA@so)%wfxyjyF%@L8rpc^$L%t?azKbMe7w;a~Y{dfHnTAf`jjJz=(%Vc5)` z=!|_fp2q$RAM>$4&cp9{yzuWn59L1IgXYb~-^g}%Em+T*t)Z}Q;* zIsYv1Nkf}!`OJ%DI+Xi+uq+IVuO*_;E1nx^+>r%^r1 zJ6(E;a{%StZKxN`)TzDfrbe@`YZE(o{Kdz~P4>d>UX5D(OQ6mn%DK8d22x>ITVv9I(!@#B{8IHLNwLqjg7)?dKC(VKza zz?z1dsO$Cxirvy^;J|3w#C@ojZbQ7~P}7Ef_S46bI~QXMZE3%GG(xp-wCoh-4Q^kG z{T9V;ul@SaX-#$W@AEonaA*OAv4*xjjXF@{+m;5u@0nAv0~$Ga zY!CK>diXXlYtfN|$Dr)E4fU@_9I4I$vS)W2_Lw+(N}T;{J7^#YQf?hM^#e_|k z8Hj{odcpxeDc=@8Xt# ztR#moLeQvZyS2SxgTv!&xb*?ToU)+fVDOy_?DJg=XxTL6Sv=!?U`gNAixHw0Dj@vY`0P=HEYfJLS8h=Usj0_Fp=G@GZxT$%j9F?eHh=z_-^cPp4e& z>Z-AqxdX`E-yfKIG&TR`t;VyyA9}Zz?CbupYDaeNOK)`cJS_iz?Z0}VV`xL{ g*Y|JK+9qtu&s*NN_uQ4nzUCi)X>b0sw>W#!KjUg54*&oF From d7f6fbea1989391782ee2086150f2d0e1cc208dd Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sun, 11 Sep 2022 15:42:24 +0530 Subject: [PATCH 3/7] fmt --- cli/bench/stdio/README.md | 2 +- cli/bench/stdio/stdio.c | 3 +++ cli/bench/stdio/stdio.js | 23 +++++++++++++---------- runtime/ops/io.rs | 18 ++++++------------ 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/cli/bench/stdio/README.md b/cli/bench/stdio/README.md index 5d3a7137009b9a..b2d4875ac769ff 100644 --- a/cli/bench/stdio/README.md +++ b/cli/bench/stdio/README.md @@ -6,4 +6,4 @@ Compile the C baseline and run the benchmark: 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 -``` \ No newline at end of file +``` diff --git a/cli/bench/stdio/stdio.c b/cli/bench/stdio/stdio.c index abd0646c34aa5d..8125a0d00f18c4 100644 --- a/cli/bench/stdio/stdio.c +++ b/cli/bench/stdio/stdio.c @@ -1,3 +1,6 @@ +// From https://github.com/just-js/benchmarks/tree/main/01-stdio +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + #include #include #include diff --git a/cli/bench/stdio/stdio.js b/cli/bench/stdio/stdio.js index 6827e783353b5c..a8deecef552e98 100644 --- a/cli/bench/stdio/stdio.js +++ b/cli/bench/stdio/stdio.js @@ -1,13 +1,16 @@ -const blocksize = parseInt(Deno.args[0] || 65536) -const buf = new ArrayBuffer(blocksize) -let size = 0 -let reads = 0 +// From https://github.com/just-js/benchmarks/tree/main/01-stdio +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. -let n = Deno.stdin.readSync(buf) +const blocksize = parseInt(Deno.args[0] || 65536); +const buf = new ArrayBuffer(blocksize); +let size = 0; +let reads = 0; + +let n = Deno.stdin.readSync(buf); while (n > 0) { - reads++ - size += n - n = Deno.stdin.readSync(buf) + reads++; + size += n; + n = Deno.stdin.readSync(buf); } -if (n < 0) throw new Error('Bad Read') -console.log(`size ${size} reads ${reads} blocksize ${blocksize}`) \ No newline at end of file +if (n < 0) throw new Error("Bad Read"); +console.log(`size ${size} reads ${reads} blocksize ${blocksize}`); diff --git a/runtime/ops/io.rs b/runtime/ops/io.rs index 641e0063458b48..30a096e9c3e0e0 100644 --- a/runtime/ops/io.rs +++ b/runtime/ops/io.rs @@ -674,11 +674,7 @@ pub fn op_print( } #[op(fast)] -fn op_read_sync( - state: &mut OpState, - rid: u32, - buf: &mut [u8], -) -> u32 { +fn op_read_sync(state: &mut OpState, rid: u32, buf: &mut [u8]) -> u32 { StdFileResource::with_resource(state, rid, move |resource| { resource.with_inner_and_metadata(|inner, _| { inner @@ -686,15 +682,12 @@ fn op_read_sync( .map(|n: usize| n as u32) .map_err(AnyError::from) }) - }).unwrap() + }) + .unwrap() } #[op(fast)] -fn op_write_sync( - state: &mut OpState, - rid: u32, - buf: &mut [u8], -) -> u32 { +fn op_write_sync(state: &mut OpState, rid: u32, buf: &mut [u8]) -> u32 { StdFileResource::with_resource(state, rid, move |resource| { resource.with_inner_and_metadata(|inner, _| { inner @@ -702,5 +695,6 @@ fn op_write_sync( .map(|nwritten: usize| nwritten as u32) .map_err(AnyError::from) }) - }).unwrap() + }) + .unwrap() } From 01c8713488ddfdfdea075147df80c89d43e9b5de Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sun, 11 Sep 2022 15:47:20 +0530 Subject: [PATCH 4/7] fix --- cli/bench/stdio/stdio.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/bench/stdio/stdio.js b/cli/bench/stdio/stdio.js index a8deecef552e98..0663d6fd7d3f62 100644 --- a/cli/bench/stdio/stdio.js +++ b/cli/bench/stdio/stdio.js @@ -2,7 +2,7 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. const blocksize = parseInt(Deno.args[0] || 65536); -const buf = new ArrayBuffer(blocksize); +const buf = new Uint8Array(blocksize); let size = 0; let reads = 0; From fd678b5866aa3f413542b86f5fe06d0815f4b59d Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 23 Sep 2022 09:01:11 +0530 Subject: [PATCH 5/7] fallible ops --- runtime/ops/io.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/runtime/ops/io.rs b/runtime/ops/io.rs index 30a096e9c3e0e0..380879c3a34426 100644 --- a/runtime/ops/io.rs +++ b/runtime/ops/io.rs @@ -674,7 +674,11 @@ pub fn op_print( } #[op(fast)] -fn op_read_sync(state: &mut OpState, rid: u32, buf: &mut [u8]) -> u32 { +fn op_read_sync( + state: &mut OpState, + rid: u32, + buf: &mut [u8], +) -> Result { StdFileResource::with_resource(state, rid, move |resource| { resource.with_inner_and_metadata(|inner, _| { inner @@ -683,11 +687,14 @@ fn op_read_sync(state: &mut OpState, rid: u32, buf: &mut [u8]) -> u32 { .map_err(AnyError::from) }) }) - .unwrap() } #[op(fast)] -fn op_write_sync(state: &mut OpState, rid: u32, buf: &mut [u8]) -> u32 { +fn op_write_sync( + state: &mut OpState, + rid: u32, + buf: &mut [u8], +) -> Result { StdFileResource::with_resource(state, rid, move |resource| { resource.with_inner_and_metadata(|inner, _| { inner @@ -696,5 +703,4 @@ fn op_write_sync(state: &mut OpState, rid: u32, buf: &mut [u8]) -> u32 { .map_err(AnyError::from) }) }) - .unwrap() } From 84d80a547546b73db09aea99cee1be127dfc95e3 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 23 Sep 2022 09:37:48 +0530 Subject: [PATCH 6/7] x --- runtime/js/12_io.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/js/12_io.js b/runtime/js/12_io.js index 55393b776dbd3a..db83343bf0850b 100644 --- a/runtime/js/12_io.js +++ b/runtime/js/12_io.js @@ -92,7 +92,7 @@ return 0; } - const nread = ops.op_read_sync.fast(rid, buffer); + const nread = ops.op_read_sync(rid, buffer); return nread === 0 ? null : nread; } @@ -108,7 +108,7 @@ } function writeSync(rid, data) { - return ops.op_write_sync.fast(rid, data); + return ops.op_write_sync(rid, data); } function write(rid, data) { From 7c78786b341b6c0d0d4c5b5f587dfaecc8bbe2ec Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 14 Nov 2022 14:49:58 -0500 Subject: [PATCH 7/7] lint --- runtime/ops/io.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/runtime/ops/io.rs b/runtime/ops/io.rs index f92ff577c7f1e2..973fb8f9352f96 100644 --- a/runtime/ops/io.rs +++ b/runtime/ops/io.rs @@ -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;