diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-03-17 16:15:27 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-17 22:15:27 +0000 |
commit | 3487fde236d0852a8b0672c293fa41a741f471e8 (patch) | |
tree | af466368147a08b787080446319a3a46a60ee37d /ext/io/lib.rs | |
parent | e55b448730160a6e4df9815a268d4049ac89deab (diff) |
perf(core) Reduce copying and cloning in extension initialization (#18252)
Follow-up to #18210:
* we are passing the generated `cfg` object into the state function
rather than passing individual config fields
* reduce cloning dramatically by making the state_fn `FnOnce`
* `take` for `ExtensionBuilder` to avoid more unnecessary copies
* renamed `config` to `options`
Diffstat (limited to 'ext/io/lib.rs')
-rw-r--r-- | ext/io/lib.rs | 80 |
1 files changed, 39 insertions, 41 deletions
diff --git a/ext/io/lib.rs b/ext/io/lib.rs index bfbb1d94f..5bb526d4a 100644 --- a/ext/io/lib.rs +++ b/ext/io/lib.rs @@ -80,55 +80,53 @@ deno_core::extension!(deno_io, deps = [ deno_web ], ops = [op_read_sync, op_write_sync], esm = [ "12_io.js" ], - config = { - stdio: Rc<RefCell<Option<Stdio>>>, + options = { + stdio: Option<Stdio>, }, middleware = |op| match op.name { "op_print" => op_print::decl(), _ => op, }, - state = |state, stdio| { - let stdio = stdio - .borrow_mut() - .take() - .expect("Extension only supports being used once."); - let t = &mut state.resource_table; - - let rid = t.add(StdFileResource::stdio( - match stdio.stdin { - StdioPipe::Inherit => StdFileResourceInner { - kind: StdFileResourceKind::Stdin, - file: STDIN_HANDLE.try_clone().unwrap(), + state = |state, options| { + if let Some(stdio) = options.stdio { + let t = &mut state.resource_table; + + let rid = t.add(StdFileResource::stdio( + match stdio.stdin { + StdioPipe::Inherit => StdFileResourceInner { + kind: StdFileResourceKind::Stdin, + file: STDIN_HANDLE.try_clone().unwrap(), + }, + StdioPipe::File(pipe) => StdFileResourceInner::file(pipe), }, - StdioPipe::File(pipe) => StdFileResourceInner::file(pipe), - }, - "stdin", - )); - assert_eq!(rid, 0, "stdin must have ResourceId 0"); - - let rid = t.add(StdFileResource::stdio( - match stdio.stdout { - StdioPipe::Inherit => StdFileResourceInner { - kind: StdFileResourceKind::Stdout, - file: STDOUT_HANDLE.try_clone().unwrap(), + "stdin", + )); + assert_eq!(rid, 0, "stdin must have ResourceId 0"); + + let rid = t.add(StdFileResource::stdio( + match stdio.stdout { + StdioPipe::Inherit => StdFileResourceInner { + kind: StdFileResourceKind::Stdout, + file: STDOUT_HANDLE.try_clone().unwrap(), + }, + StdioPipe::File(pipe) => StdFileResourceInner::file(pipe), }, - StdioPipe::File(pipe) => StdFileResourceInner::file(pipe), - }, - "stdout", - )); - assert_eq!(rid, 1, "stdout must have ResourceId 1"); - - let rid = t.add(StdFileResource::stdio( - match stdio.stderr { - StdioPipe::Inherit => StdFileResourceInner { - kind: StdFileResourceKind::Stderr, - file: STDERR_HANDLE.try_clone().unwrap(), + "stdout", + )); + assert_eq!(rid, 1, "stdout must have ResourceId 1"); + + let rid = t.add(StdFileResource::stdio( + match stdio.stderr { + StdioPipe::Inherit => StdFileResourceInner { + kind: StdFileResourceKind::Stderr, + file: STDERR_HANDLE.try_clone().unwrap(), + }, + StdioPipe::File(pipe) => StdFileResourceInner::file(pipe), }, - StdioPipe::File(pipe) => StdFileResourceInner::file(pipe), - }, - "stderr", - )); - assert_eq!(rid, 2, "stderr must have ResourceId 2"); + "stderr", + )); + assert_eq!(rid, 2, "stderr must have ResourceId 2"); + } }, ); |