From 06209f824cf398bcd3332dc37f2d22cbdd840647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 25 Jul 2023 23:43:00 +0200 Subject: perf: cache node resolution when accesing a global (#19930) Reclaims some of the performance hit introduced by https://github.com/denoland/deno/pull/19307. --- ext/fs/sync.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'ext/fs') diff --git a/ext/fs/sync.rs b/ext/fs/sync.rs index c43850c28..749e4ee02 100644 --- a/ext/fs/sync.rs +++ b/ext/fs/sync.rs @@ -5,10 +5,42 @@ pub use inner::*; #[cfg(feature = "sync_fs")] mod inner { #![allow(clippy::disallowed_types)] + + use std::ops::Deref; + use std::ops::DerefMut; pub use std::sync::Arc as MaybeArc; pub use core::marker::Send as MaybeSend; pub use core::marker::Sync as MaybeSync; + + pub struct MaybeArcMutexGuard<'lock, T>(std::sync::MutexGuard<'lock, T>); + + impl<'lock, T> Deref for MaybeArcMutexGuard<'lock, T> { + type Target = std::sync::MutexGuard<'lock, T>; + fn deref(&self) -> &std::sync::MutexGuard<'lock, T> { + &self.0 + } + } + + impl<'lock, T> DerefMut for MaybeArcMutexGuard<'lock, T> { + fn deref_mut(&mut self) -> &mut std::sync::MutexGuard<'lock, T> { + &mut self.0 + } + } + + #[derive(Debug)] + pub struct MaybeArcMutex(std::sync::Arc>); + impl MaybeArcMutex { + pub fn new(val: T) -> Self { + Self(std::sync::Arc::new(std::sync::Mutex::new(val))) + } + } + + impl<'lock, T> MaybeArcMutex { + pub fn lock(&'lock self) -> MaybeArcMutexGuard<'lock, T> { + MaybeArcMutexGuard(self.0.lock().unwrap()) + } + } } #[cfg(not(feature = "sync_fs"))] @@ -19,4 +51,33 @@ mod inner { impl MaybeSync for T where T: ?Sized {} pub trait MaybeSend {} impl MaybeSend for T where T: ?Sized {} + + pub struct MaybeArcMutexGuard<'lock, T>(std::cell::RefMut<'lock, T>); + + impl<'lock, T> Deref for MaybeArcMutexGuard<'lock, T> { + type Target = std::cell::RefMut<'lock, T>; + fn deref(&self) -> &std::cell::RefMut<'lock, T> { + &self.0 + } + } + + impl<'lock, T> DerefMut for MaybeArcMutexGuard<'lock, T> { + fn deref_mut(&mut self) -> &mut std::cell::RefMut<'lock, T> { + &mut self.0 + } + } + + #[derive(Debug)] + pub struct MaybeArcMutex(std::rc::Rc>); + impl MaybeArcMutex { + pub fn new(val: T) -> Self { + Self(std::rc::Rc::new(std::cell::RefCell::new(val))) + } + } + + impl<'lock, T> MaybeArcMutex { + pub fn lock(&'lock self) -> MaybeArcMutexGuard<'lock, T> { + MaybeArcMutexGuard(self.0.borrow_mut()) + } + } } -- cgit v1.2.3