0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-10 14:17:49 -04:00
deno/runtime/snapshot_info.rs
Nathan Whitaker 69b59b2296
perf(http): instantiate generic functions in deno_http, increase opt-level for some more hyper deps (#28317)
results on my machine (Macbook pro w/ M3 Max)

canary (hello world):
```
❯ oha -c 125 -n 4000000 --no-tui --disable-compression http://localhost:8000
Summary:
  Success rate: 100.00%
  Total:        31.0160 secs
  Slowest:      0.0083 secs
  Fastest:      0.0005 secs
  Average:      0.0010 secs
  Requests/sec: 128965.6454

  Total data:   49.59 MiB
  Size/request: 13 B
  Size/sec:     1.60 MiB
```

this PR (hello world):
```
❯ oha -c 125 -n 4000000 --no-tui --disable-compression http://localhost:8000
Summary:
  Success rate: 100.00%
  Total:        28.4050 secs
  Slowest:      0.0085 secs
  Fastest:      0.0001 secs
  Average:      0.0009 secs
  Requests/sec: 140820.2060

  Total data:   49.59 MiB
  Size/request: 13 B
  Size/sec:     1.75 MiB
```

---

Two changes here:
- use `opt-level` 3 for some of hyper's deps, since profile overrides
are not transitive
- As noted in the [cargo
reference](https://doc.rust-lang.org/cargo/reference/profiles.html#overrides-and-generics)
generic functions _may_ be optimized at the opt-level of the
_instantiating_ crate, rather than the defining crate. So currently it's
possible that some of the functions in `deno_http` are being compiled at
a lower optimization level. This PR ensures the generics are
instantiated in `deno_http`, which theoretically should guarantee they
actually use opt-level 3.

To allow embedders to still provide a custom property extractor, I put
this behind a feature flag.
2025-02-27 10:07:11 -08:00

319 lines
8 KiB
Rust

// Copyright 2018-2025 the Deno authors. MIT license.
use std::borrow::Cow;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use deno_core::Extension;
use deno_io::fs::FsError;
use deno_permissions::PermissionCheckError;
use deno_resolver::npm::DenoInNpmPackageChecker;
use deno_resolver::npm::NpmResolver;
use crate::ops;
use crate::shared::runtime;
#[derive(Clone)]
pub struct Permissions;
impl deno_websocket::WebSocketPermissions for Permissions {
fn check_net_url(
&mut self,
_url: &deno_core::url::Url,
_api_name: &str,
) -> Result<(), PermissionCheckError> {
unreachable!("snapshotting!")
}
}
impl deno_web::TimersPermission for Permissions {
fn allow_hrtime(&mut self) -> bool {
unreachable!("snapshotting!")
}
}
impl deno_fetch::FetchPermissions for Permissions {
fn check_net_url(
&mut self,
_url: &deno_core::url::Url,
_api_name: &str,
) -> Result<(), PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_read<'a>(
&mut self,
_resolved: bool,
_p: &'a Path,
_api_name: &str,
) -> Result<Cow<'a, Path>, FsError> {
unreachable!("snapshotting!")
}
}
impl deno_ffi::FfiPermissions for Permissions {
fn check_partial_no_path(&mut self) -> Result<(), PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_partial_with_path(
&mut self,
_path: &str,
) -> Result<PathBuf, PermissionCheckError> {
unreachable!("snapshotting!")
}
}
impl deno_napi::NapiPermissions for Permissions {
fn check(&mut self, _path: &str) -> Result<PathBuf, PermissionCheckError> {
unreachable!("snapshotting!")
}
}
impl deno_node::NodePermissions for Permissions {
fn check_net_url(
&mut self,
_url: &deno_core::url::Url,
_api_name: &str,
) -> Result<(), PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_net(
&mut self,
_host: (&str, Option<u16>),
_api_name: &str,
) -> Result<(), PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_read_path<'a>(
&mut self,
_path: &'a Path,
) -> Result<Cow<'a, Path>, PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_read_with_api_name(
&mut self,
_p: &str,
_api_name: Option<&str>,
) -> Result<PathBuf, PermissionCheckError> {
unreachable!("snapshotting!")
}
fn query_read_all(&mut self) -> bool {
unreachable!("snapshotting!")
}
fn check_write_with_api_name(
&mut self,
_p: &str,
_api_name: Option<&str>,
) -> Result<PathBuf, PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_sys(
&mut self,
_kind: &str,
_api_name: &str,
) -> Result<(), PermissionCheckError> {
unreachable!("snapshotting!")
}
}
impl deno_net::NetPermissions for Permissions {
fn check_net<T: AsRef<str>>(
&mut self,
_host: &(T, Option<u16>),
_api_name: &str,
) -> Result<(), PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_read(
&mut self,
_p: &str,
_api_name: &str,
) -> Result<PathBuf, PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_write(
&mut self,
_p: &str,
_api_name: &str,
) -> Result<PathBuf, PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_write_path<'a>(
&mut self,
_p: &'a Path,
_api_name: &str,
) -> Result<Cow<'a, Path>, PermissionCheckError> {
unreachable!("snapshotting!")
}
}
impl deno_fs::FsPermissions for Permissions {
fn check_open<'a>(
&mut self,
_resolved: bool,
_read: bool,
_write: bool,
_path: &'a Path,
_api_name: &str,
) -> Result<Cow<'a, Path>, FsError> {
unreachable!("snapshotting!")
}
fn check_read(
&mut self,
_path: &str,
_api_name: &str,
) -> Result<PathBuf, PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_read_all(
&mut self,
_api_name: &str,
) -> Result<(), PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_read_blind(
&mut self,
_path: &Path,
_display: &str,
_api_name: &str,
) -> Result<(), PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_write(
&mut self,
_path: &str,
_api_name: &str,
) -> Result<PathBuf, PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_write_partial(
&mut self,
_path: &str,
_api_name: &str,
) -> Result<PathBuf, PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_write_all(
&mut self,
_api_name: &str,
) -> Result<(), PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_write_blind(
&mut self,
_path: &Path,
_display: &str,
_api_name: &str,
) -> Result<(), PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_read_path<'a>(
&mut self,
_path: &'a Path,
_api_name: &str,
) -> Result<Cow<'a, Path>, PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_write_path<'a>(
&mut self,
_path: &'a Path,
_api_name: &str,
) -> Result<Cow<'a, Path>, PermissionCheckError> {
unreachable!("snapshotting!")
}
}
impl deno_kv::sqlite::SqliteDbHandlerPermissions for Permissions {
fn check_read(
&mut self,
_path: &str,
_api_name: &str,
) -> Result<PathBuf, PermissionCheckError> {
unreachable!("snapshotting!")
}
fn check_write<'a>(
&mut self,
_path: &'a Path,
_api_name: &str,
) -> Result<Cow<'a, Path>, PermissionCheckError> {
unreachable!("snapshotting!")
}
}
pub fn get_extensions_in_snapshot() -> Vec<Extension> {
// NOTE(bartlomieju): ordering is important here, keep it in sync with
// `runtime/worker.rs`, `runtime/web_worker.rs`, `runtime/snapshot_info.rs`
// and `runtime/snapshot.rs`!
let fs = std::sync::Arc::new(deno_fs::RealFs);
vec![
deno_telemetry::deno_telemetry::init_ops(),
deno_webidl::deno_webidl::init_ops(),
deno_console::deno_console::init_ops(),
deno_url::deno_url::init_ops(),
deno_web::deno_web::init_ops::<Permissions>(
Default::default(),
Default::default(),
),
deno_webgpu::deno_webgpu::init_ops(),
deno_canvas::deno_canvas::init_ops(),
deno_fetch::deno_fetch::init_ops::<Permissions>(Default::default()),
deno_cache::deno_cache::init_ops(None),
deno_websocket::deno_websocket::init_ops::<Permissions>(
"".to_owned(),
None,
None,
),
deno_webstorage::deno_webstorage::init_ops(None),
deno_crypto::deno_crypto::init_ops(None),
deno_broadcast_channel::deno_broadcast_channel::init_ops(
deno_broadcast_channel::InMemoryBroadcastChannel::default(),
),
deno_ffi::deno_ffi::init_ops::<Permissions>(),
deno_net::deno_net::init_ops::<Permissions>(None, None),
deno_tls::deno_tls::init_ops(),
deno_kv::deno_kv::init_ops(
deno_kv::sqlite::SqliteDbHandler::<Permissions>::new(None, None),
deno_kv::KvConfig::builder().build(),
),
deno_cron::deno_cron::init_ops(deno_cron::local::LocalCronHandler::new()),
deno_napi::deno_napi::init_ops::<Permissions>(),
deno_http::deno_http::init_ops(deno_http::Options::default()),
deno_io::deno_io::init_ops(Some(Default::default())),
deno_fs::deno_fs::init_ops::<Permissions>(fs.clone()),
deno_os::deno_os::init_ops(Default::default()),
deno_process::deno_process::init_ops(Default::default()),
deno_node::deno_node::init_ops::<
Permissions,
DenoInNpmPackageChecker,
NpmResolver<sys_traits::impls::RealSys>,
sys_traits::impls::RealSys,
>(None, fs.clone()),
runtime::init_ops(),
ops::runtime::deno_runtime::init_ops("deno:runtime".parse().unwrap()),
ops::worker_host::deno_worker_host::init_ops(
Arc::new(|_| unreachable!("not used in snapshot.")),
None,
),
ops::fs_events::deno_fs_events::init_ops(),
ops::permissions::deno_permissions::init_ops(),
ops::tty::deno_tty::init_ops(),
ops::http::deno_http_runtime::init_ops(),
ops::bootstrap::deno_bootstrap::init_ops(None),
ops::web_worker::deno_web_worker::init_ops(),
]
}