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 | |
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')
-rw-r--r-- | ext/broadcast_channel/lib.rs | 8 | ||||
-rw-r--r-- | ext/cache/lib.rs | 6 | ||||
-rw-r--r-- | ext/crypto/lib.rs | 6 | ||||
-rw-r--r-- | ext/fetch/lib.rs | 14 | ||||
-rw-r--r-- | ext/ffi/lib.rs | 6 | ||||
-rw-r--r-- | ext/flash/lib.rs | 6 | ||||
-rw-r--r-- | ext/fs/lib.rs | 6 | ||||
-rw-r--r-- | ext/io/lib.rs | 80 | ||||
-rw-r--r-- | ext/net/lib.rs | 10 | ||||
-rw-r--r-- | ext/node/lib.rs | 6 | ||||
-rw-r--r-- | ext/web/lib.rs | 8 | ||||
-rw-r--r-- | ext/websocket/lib.rs | 10 | ||||
-rw-r--r-- | ext/webstorage/lib.rs | 6 |
13 files changed, 85 insertions, 87 deletions
diff --git a/ext/broadcast_channel/lib.rs b/ext/broadcast_channel/lib.rs index 3df11566f..5b38e70f8 100644 --- a/ext/broadcast_channel/lib.rs +++ b/ext/broadcast_channel/lib.rs @@ -114,13 +114,13 @@ deno_core::extension!(deno_broadcast_channel, op_broadcast_recv<BC>, ], esm = [ "01_broadcast_channel.js" ], - config = { + options = { bc: BC, unstable: bool, }, - state = |state, bc, unstable| { - state.put(bc); - state.put(Unstable(unstable)); + state = |state, options| { + state.put(options.bc); + state.put(Unstable(options.unstable)); }, ); diff --git a/ext/cache/lib.rs b/ext/cache/lib.rs index eaf56c8b7..b5a29e905 100644 --- a/ext/cache/lib.rs +++ b/ext/cache/lib.rs @@ -32,11 +32,11 @@ deno_core::extension!(deno_cache, op_cache_delete<CA>, ], esm = [ "01_cache.js" ], - config = { + options = { maybe_create_cache: Option<CreateCache<CA>>, }, - state = |state, maybe_create_cache| { - if let Some(create_cache) = maybe_create_cache { + state = |state, options| { + if let Some(create_cache) = options.maybe_create_cache { state.put(create_cache); } }, diff --git a/ext/crypto/lib.rs b/ext/crypto/lib.rs index 48c5acf4a..6056b02a4 100644 --- a/ext/crypto/lib.rs +++ b/ext/crypto/lib.rs @@ -104,11 +104,11 @@ deno_core::extension!(deno_crypto, x25519::op_export_pkcs8_x25519, ], esm = [ "00_crypto.js", "01_webidl.js" ], - config = { + options = { maybe_seed: Option<u64>, }, - state = |state, maybe_seed| { - if let Some(seed) = maybe_seed { + state = |state, options| { + if let Some(seed) = options.maybe_seed { state.put(StdRng::seed_from_u64(seed)); } }, diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index 4cd5d68c8..9ed73161d 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -107,19 +107,19 @@ deno_core::extension!(deno_fetch, "23_response.js", "26_fetch.js" ], - config = { + options = { options: Options, }, state = |state, options| { - state.put::<Options>(options.clone()); + state.put::<Options>(options.options.clone()); state.put::<reqwest::Client>({ create_http_client( - options.user_agent, - options.root_cert_store, + options.options.user_agent, + options.options.root_cert_store, vec![], - options.proxy, - options.unsafely_ignore_certificate_errors, - options.client_cert_chain_and_key + options.options.proxy, + options.options.unsafely_ignore_certificate_errors, + options.options.client_cert_chain_and_key ) .unwrap() }); diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index 1fd01c9d2..c11f08dd8 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -112,12 +112,12 @@ deno_core::extension!(deno_ffi, op_ffi_unsafe_callback_ref, ], esm = [ "00_ffi.js" ], - config = { + options = { unstable: bool, }, - state = |state, unstable| { + state = |state, options| { // Stolen from deno_webgpu, is there a better option? - state.put(Unstable(unstable)); + state.put(Unstable(options.unstable)); let (async_work_sender, async_work_receiver) = mpsc::unbounded::<PendingFfiAsyncWork>(); diff --git a/ext/flash/lib.rs b/ext/flash/lib.rs index 6f11e14af..b6a586d1f 100644 --- a/ext/flash/lib.rs +++ b/ext/flash/lib.rs @@ -1559,11 +1559,11 @@ deno_core::extension!(deno_flash, op_try_flash_respond_chunked, ], esm = [ "01_http.js" ], - config = { + options = { unstable: bool, }, - state = |state, unstable| { - state.put(Unstable(unstable)); + state = |state, options| { + state.put(Unstable(options.unstable)); state.put(FlashContext { next_server_id: 0, join_handles: HashMap::default(), diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index 48b0e3495..386d143d2 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -180,11 +180,11 @@ deno_core::extension!(deno_fs, op_readfile_text_async<P>, ], esm = [ "30_fs.js" ], - config = { + options = { unstable: bool }, - state = |state, unstable| { - state.put(UnstableChecker { unstable }); + state = |state, options| { + state.put(UnstableChecker { unstable: options.unstable }); }, ); 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"); + } }, ); diff --git a/ext/net/lib.rs b/ext/net/lib.rs index 76ed02706..00833b53c 100644 --- a/ext/net/lib.rs +++ b/ext/net/lib.rs @@ -105,18 +105,18 @@ deno_core::extension!(deno_net, #[cfg(unix)] ops_unix::op_net_send_unixpacket<P>, ], esm = [ "01_net.js", "02_tls.js" ], - config = { + options = { root_cert_store: Option<RootCertStore>, unstable: bool, unsafely_ignore_certificate_errors: Option<Vec<String>>, }, - state = |state, root_cert_store, unstable, unsafely_ignore_certificate_errors| { + state = |state, options| { state.put(DefaultTlsOptions { - root_cert_store, + root_cert_store: options.root_cert_store, }); - state.put(UnstableChecker { unstable }); + state.put(UnstableChecker { unstable: options.unstable }); state.put(UnsafelyIgnoreCertificateErrors( - unsafely_ignore_certificate_errors, + options.unsafely_ignore_certificate_errors, )); }, ); diff --git a/ext/node/lib.rs b/ext/node/lib.rs index 06138cf4c..23a003bc8 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -370,11 +370,11 @@ deno_core::extension!(deno_node_loading, ops::op_require_break_on_next_statement, ], esm = ["01_node.js", "02_require.js", "module_es_shim.js"], - config = { + options = { maybe_npm_resolver: Option<Rc<dyn RequireNpmResolver>>, }, - state = |state, maybe_npm_resolver| { - if let Some(npm_resolver) = maybe_npm_resolver.clone() { + state = |state, options| { + if let Some(npm_resolver) = options.maybe_npm_resolver { state.put(npm_resolver); } }, diff --git a/ext/web/lib.rs b/ext/web/lib.rs index f3a22d623..b0dc0d56d 100644 --- a/ext/web/lib.rs +++ b/ext/web/lib.rs @@ -109,13 +109,13 @@ deno_core::extension!(deno_web, "14_compression.js", "15_performance.js", ], - config = { + options = { blob_store: BlobStore, maybe_location: Option<Url>, }, - state = |state, blob_store, maybe_location| { - state.put(blob_store); - if let Some(location) = maybe_location { + state = |state, options| { + state.put(options.blob_store); + if let Some(location) = options.maybe_location { state.put(Location(location)); } state.put(StartTime::now()); diff --git a/ext/websocket/lib.rs b/ext/websocket/lib.rs index e480d7f4c..24a290b4b 100644 --- a/ext/websocket/lib.rs +++ b/ext/websocket/lib.rs @@ -506,17 +506,17 @@ deno_core::extension!(deno_websocket, op_ws_next_event, ], esm = [ "01_websocket.js", "02_websocketstream.js" ], - config = { + options = { user_agent: String, root_cert_store: Option<RootCertStore>, unsafely_ignore_certificate_errors: Option<Vec<String>> }, - state = |state, user_agent, root_cert_store, unsafely_ignore_certificate_errors| { - state.put::<WsUserAgent>(WsUserAgent(user_agent)); + state = |state, options| { + state.put::<WsUserAgent>(WsUserAgent(options.user_agent)); state.put(UnsafelyIgnoreCertificateErrors( - unsafely_ignore_certificate_errors, + options.unsafely_ignore_certificate_errors, )); - state.put::<WsRootStore>(WsRootStore(root_cert_store)); + state.put::<WsRootStore>(WsRootStore(options.root_cert_store)); }, ); diff --git a/ext/webstorage/lib.rs b/ext/webstorage/lib.rs index 6cdc7bbff..10be072e8 100644 --- a/ext/webstorage/lib.rs +++ b/ext/webstorage/lib.rs @@ -31,11 +31,11 @@ deno_core::extension!(deno_webstorage, op_webstorage_iterate_keys, ], esm = [ "01_webstorage.js" ], - config = { + options = { origin_storage_dir: Option<PathBuf> }, - state = |state, origin_storage_dir| { - if let Some(origin_storage_dir) = origin_storage_dir { + state = |state, options| { + if let Some(origin_storage_dir) = options.origin_storage_dir { state.put(OriginStorageDir(origin_storage_dir)); } }, |