Faster IMAP UID generation
This commit is contained in:
@@ -40,6 +40,7 @@ futures = "0.3"
|
||||
proxy-header = { version = "0.1.0", features = ["tokio"] }
|
||||
regex = "1.7.0"
|
||||
blake3 = "1.3.3"
|
||||
lru-cache = "0.1.2"
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
privdrop = "0.5.3"
|
||||
|
||||
@@ -21,11 +21,7 @@
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{atomic::AtomicU64, Arc},
|
||||
time::SystemTime,
|
||||
};
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use config::Config;
|
||||
|
||||
@@ -35,6 +31,7 @@ pub mod config;
|
||||
pub mod expr;
|
||||
pub mod ipc;
|
||||
pub mod listener;
|
||||
pub mod lru_cache;
|
||||
pub mod map;
|
||||
pub mod snowflake;
|
||||
pub mod suffixlist;
|
||||
@@ -115,40 +112,6 @@ impl AsMut<[u8]> for BlobHash {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CachedItem<T> {
|
||||
last_access: Arc<AtomicU64>,
|
||||
item: Arc<tokio::sync::Mutex<T>>,
|
||||
}
|
||||
|
||||
impl<T> CachedItem<T> {
|
||||
pub fn new(item: T) -> Self {
|
||||
Self {
|
||||
last_access: Arc::new(AtomicU64::new(
|
||||
SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.map_or(0, |d| d.as_secs()),
|
||||
)),
|
||||
item: Arc::new(tokio::sync::Mutex::new(item)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get(&self) -> tokio::sync::MutexGuard<'_, T> {
|
||||
let lock = self.item.lock().await;
|
||||
self.last_access.store(
|
||||
SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.map_or(0, |d| d.as_secs()),
|
||||
std::sync::atomic::Ordering::Relaxed,
|
||||
);
|
||||
lock
|
||||
}
|
||||
|
||||
pub fn last_access(&self) -> u64 {
|
||||
self.last_access.load(std::sync::atomic::Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait UnwrapFailure<T> {
|
||||
fn failed(self, action: &str) -> T;
|
||||
}
|
||||
|
||||
58
crates/utils/src/lru_cache.rs
Normal file
58
crates/utils/src/lru_cache.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Stalwart Labs Ltd.
|
||||
*
|
||||
* This file is part of Stalwart Mail Server.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
* in the LICENSE file at the top-level directory of this distribution.
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* You can be released from the requirements of the AGPLv3 license by
|
||||
* purchasing a commercial license. Please contact licensing@stalw.art
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
use std::{borrow::Borrow, hash::Hash};
|
||||
|
||||
use parking_lot::Mutex;
|
||||
|
||||
pub type LruCache<K, V> = Mutex<lru_cache::LruCache<K, V, ahash::RandomState>>;
|
||||
|
||||
pub trait LruCached<K, V>: Sized {
|
||||
fn with_capacity(capacity: usize) -> Self;
|
||||
fn get<Q: ?Sized>(&self, name: &Q) -> Option<V>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Hash + Eq;
|
||||
fn insert(&self, name: K, value: V) -> Option<V>;
|
||||
}
|
||||
|
||||
impl<K: Hash + Eq, V: Clone> LruCached<K, V> for LruCache<K, V> {
|
||||
fn with_capacity(capacity: usize) -> Self {
|
||||
Mutex::new(lru_cache::LruCache::with_hasher(
|
||||
capacity,
|
||||
ahash::RandomState::new(),
|
||||
))
|
||||
}
|
||||
|
||||
fn get<Q: ?Sized>(&self, name: &Q) -> Option<V>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Hash + Eq,
|
||||
{
|
||||
self.lock().get_mut(name).map(|entry| entry.clone())
|
||||
}
|
||||
|
||||
fn insert(&self, name: K, item: V) -> Option<V> {
|
||||
self.lock().insert(name, item)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user