1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

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
This commit is contained in:
Aaron O'Mullan 2021-09-23 00:45:58 +02:00 committed by GitHub
parent 845a27e748
commit a33ee087ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 {