summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-09-23 09:35:45 +0530
committerGitHub <noreply@github.com>2022-09-23 09:35:45 +0530
commit72af1496d9bc180b49d42976a31b331d0be1b975 (patch)
treee0cc2fba674ccf3a20e662a44263eb61b21b8c17
parentb5dfcbbcbe6be8ac0a54e14eb8aeb0557b58f55d (diff)
perf: use fast ops for tty (#15976)
-rw-r--r--cli/bench/tty.js21
-rw-r--r--ops/lib.rs4
-rw-r--r--runtime/js/40_tty.js20
-rw-r--r--runtime/ops/fs.rs1
-rw-r--r--runtime/ops/tty.rs88
5 files changed, 72 insertions, 62 deletions
diff --git a/cli/bench/tty.js b/cli/bench/tty.js
new file mode 100644
index 000000000..f86bcfd82
--- /dev/null
+++ b/cli/bench/tty.js
@@ -0,0 +1,21 @@
+// 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) : 100000;
+
+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));
+}
+
+bench(() => {
+ Deno.consoleSize(0);
+});
diff --git a/ops/lib.rs b/ops/lib.rs
index c0552fe5c..c6b7816e9 100644
--- a/ops/lib.rs
+++ b/ops/lib.rs
@@ -629,6 +629,10 @@ fn can_be_fast_api(core: &TokenStream2, f: &syn::ItemFn) -> Option<FastApiSyn> {
args.push(arg);
}
None => match is_ref_slice(&ty) {
+ Some(SliceType::U32Mut) => {
+ args.push(quote! { #core::v8::fast_api::Type::TypedArray(#core::v8::fast_api::CType::Uint32) });
+ slices.insert(pos, quote!(u32));
+ }
Some(_) => {
args.push(quote! { #core::v8::fast_api::Type::TypedArray(#core::v8::fast_api::CType::Uint8) });
slices.insert(pos, quote!(u8));
diff --git a/runtime/js/40_tty.js b/runtime/js/40_tty.js
index 9cf351cc0..f43859ed7 100644
--- a/runtime/js/40_tty.js
+++ b/runtime/js/40_tty.js
@@ -2,24 +2,28 @@
"use strict";
((window) => {
+ const {
+ Uint32Array,
+ Uint8Array,
+ } = window.__bootstrap.primordials;
const core = window.Deno.core;
const ops = core.ops;
+ const size = new Uint32Array(2);
function consoleSize(rid) {
- return ops.op_console_size(rid);
+ ops.op_console_size(rid, size);
+ return { columns: size[0], rows: size[1] };
}
+ const isattyBuffer = new Uint8Array(1);
function isatty(rid) {
- return ops.op_isatty(rid);
+ ops.op_isatty(rid, isattyBuffer);
+ return !!isattyBuffer[0];
}
- const DEFAULT_SET_RAW_OPTIONS = {
- cbreak: false,
- };
-
+ const DEFAULT_CBREAK = false;
function setRaw(rid, mode, options = {}) {
- const rOptions = { ...DEFAULT_SET_RAW_OPTIONS, ...options };
- ops.op_set_raw({ rid, mode, options: rOptions });
+ ops.op_set_raw(rid, mode, options.cbreak || DEFAULT_CBREAK);
}
window.__bootstrap.tty = {
diff --git a/runtime/ops/fs.rs b/runtime/ops/fs.rs
index 7c3bb3df6..f5e96e7e2 100644
--- a/runtime/ops/fs.rs
+++ b/runtime/ops/fs.rs
@@ -837,7 +837,6 @@ fn op_copy_file_sync(
#[cfg(target_os = "macos")]
{
- use libc::chmod;
use libc::clonefile;
use libc::stat;
use libc::unlink;
diff --git a/runtime/ops/tty.rs b/runtime/ops/tty.rs
index 2e6d1ea7c..e267fc07f 100644
--- a/runtime/ops/tty.rs
+++ b/runtime/ops/tty.rs
@@ -5,9 +5,6 @@ use deno_core::error::AnyError;
use deno_core::op;
use deno_core::Extension;
use deno_core::OpState;
-use deno_core::ResourceId;
-use serde::Deserialize;
-use serde::Serialize;
use std::io::Error;
#[cfg(unix)]
@@ -50,27 +47,15 @@ pub fn init() -> Extension {
.build()
}
-#[derive(Deserialize)]
-#[serde(rename_all = "camelCase")]
-pub struct SetRawOptions {
+#[op(fast)]
+fn op_set_raw(
+ state: &mut OpState,
+ rid: u32,
+ is_raw: bool,
cbreak: bool,
-}
-
-#[derive(Deserialize)]
-pub struct SetRawArgs {
- rid: ResourceId,
- mode: bool,
- options: SetRawOptions,
-}
-
-#[op]
-fn op_set_raw(state: &mut OpState, args: SetRawArgs) -> Result<(), AnyError> {
+) -> Result<(), AnyError> {
super::check_unstable(state, "Deno.setRaw");
- let rid = args.rid;
- let is_raw = args.mode;
- let cbreak = args.options.cbreak;
-
// From https://github.com/kkawakam/rustyline/blob/master/src/tty/windows.rs
// and https://github.com/kkawakam/rustyline/blob/master/src/tty/unix.rs
// and https://github.com/crossterm-rs/crossterm/blob/e35d4d2c1cc4c919e36d242e014af75f6127ab50/src/terminal/sys/windows.rs
@@ -168,9 +153,13 @@ fn op_set_raw(state: &mut OpState, args: SetRawArgs) -> Result<(), AnyError> {
}
}
-#[op]
-fn op_isatty(state: &mut OpState, rid: ResourceId) -> Result<bool, AnyError> {
- let isatty: bool = StdFileResource::with_file(state, rid, move |std_file| {
+#[op(fast)]
+fn op_isatty(
+ state: &mut OpState,
+ rid: u32,
+ out: &mut [u8],
+) -> Result<(), AnyError> {
+ StdFileResource::with_file(state, rid, move |std_file| {
#[cfg(windows)]
{
use winapi::shared::minwindef::FALSE;
@@ -181,7 +170,11 @@ fn op_isatty(state: &mut OpState, rid: ResourceId) -> Result<bool, AnyError> {
// If I cannot get mode out of console, it is not a console.
// TODO(bartlomieju):
#[allow(clippy::undocumented_unsafe_blocks)]
- Ok(unsafe { consoleapi::GetConsoleMode(handle, &mut test_mode) != FALSE })
+ {
+ out[0] = unsafe {
+ consoleapi::GetConsoleMode(handle, &mut test_mode) != FALSE
+ } as u8;
+ }
}
#[cfg(unix)]
{
@@ -189,26 +182,22 @@ fn op_isatty(state: &mut OpState, rid: ResourceId) -> Result<bool, AnyError> {
let raw_fd = std_file.as_raw_fd();
// TODO(bartlomieju):
#[allow(clippy::undocumented_unsafe_blocks)]
- Ok(unsafe { libc::isatty(raw_fd as libc::c_int) == 1 })
+ {
+ out[0] = unsafe { libc::isatty(raw_fd as libc::c_int) == 1 } as u8;
+ }
}
- })?;
- Ok(isatty)
-}
-
-#[derive(Serialize)]
-struct ConsoleSize {
- columns: u32,
- rows: u32,
+ Ok(())
+ })
}
-#[op]
+#[op(fast)]
fn op_console_size(
state: &mut OpState,
- rid: ResourceId,
-) -> Result<ConsoleSize, AnyError> {
+ rid: u32,
+ result: &mut [u32],
+) -> Result<(), AnyError> {
super::check_unstable(state, "Deno.consoleSize");
-
- let size = StdFileResource::with_file(state, rid, move |std_file| {
+ StdFileResource::with_file(state, rid, move |std_file| {
#[cfg(windows)]
{
use std::os::windows::io::AsRawHandle;
@@ -224,11 +213,9 @@ fn op_console_size(
{
return Err(Error::last_os_error().into());
}
-
- Ok(ConsoleSize {
- columns: bufinfo.dwSize.X as u32,
- rows: bufinfo.dwSize.Y as u32,
- })
+ result[0] = bufinfo.dwSize.X as u32;
+ result[1] = bufinfo.dwSize.Y as u32;
+ Ok(())
}
}
@@ -244,15 +231,10 @@ fn op_console_size(
if libc::ioctl(fd, libc::TIOCGWINSZ, &mut size as *mut _) != 0 {
return Err(Error::last_os_error().into());
}
-
- // TODO (caspervonb) return a tuple instead
- Ok(ConsoleSize {
- columns: size.ws_col as u32,
- rows: size.ws_row as u32,
- })
+ result[0] = size.ws_col as u32;
+ result[1] = size.ws_row as u32;
+ Ok(())
}
}
- })?;
-
- Ok(size)
+ })
}