From a33ee087ce77b8eb15ae23858c6743ce12eea563 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Thu, 23 Sep 2021 00:45:58 +0200 Subject: 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 --- runtime/permissions.rs | 27 ++++++++++++++++++--------- 1 file 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 { -- cgit v1.2.3