diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-09-23 00:45:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-23 00:45:58 +0200 |
commit | a33ee087ce77b8eb15ae23858c6743ce12eea563 (patch) | |
tree | a8c428caa18bf0bc644c7f7c928ddad8699ac824 /runtime | |
parent | 845a27e748c26bd75752bfe19082226c4e43266b (diff) |
perf(ops): optimize permission check (#11800)
* perf(ops): optimize permission check
Removes the overhead of permission check on access granted (should be common case):
Delta measured on `perf_now` from `deno_common` bench:
- before: `528ns/op
- after: `166ns/op`
So ~3x faster
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/permissions.rs | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/runtime/permissions.rs b/runtime/permissions.rs index c6fcfc58c..4b92f7835 100644 --- a/runtime/permissions.rs +++ b/runtime/permissions.rs @@ -12,7 +12,7 @@ use deno_core::serde::Serialize; use deno_core::url; use deno_core::ModuleSpecifier; use deno_core::OpState; -use log::debug; +use log; use std::collections::HashSet; use std::fmt; use std::hash::Hash; @@ -24,6 +24,10 @@ use std::sync::atomic::Ordering; const PERMISSION_EMOJI: &str = "⚠️"; +lazy_static::lazy_static! { + static ref DEBUG_LOG_ENABLED: bool = log::log_enabled!(log::Level::Debug); +} + /// Tri-state value for storing permission state #[derive(PartialEq, Debug, Clone, Copy, Deserialize, PartialOrd)] pub enum PermissionState { @@ -35,14 +39,19 @@ pub enum PermissionState { impl PermissionState { #[inline(always)] fn log_perm_access(name: &str, info: Option<&str>) { - debug!( - "{}", - colors::bold(&format!( - "{}️ Granted {}", - PERMISSION_EMOJI, - Self::fmt_access(name, info) - )) - ); + // Eliminates log overhead (when logging is disabled), + // log_enabled!(Debug) check in a hot path still has overhead + // TODO(AaronO): generalize or upstream this optimization + if *DEBUG_LOG_ENABLED { + log::debug!( + "{}", + colors::bold(&format!( + "{}️ Granted {}", + PERMISSION_EMOJI, + Self::fmt_access(name, info) + )) + ); + } } fn fmt_access(name: &str, info: Option<&str>) -> String { |