From 4bb6c5e90b84131d50c251456c34aff23c38f67d Mon Sep 17 00:00:00 2001 From: neon_arch Date: Thu, 5 Sep 2024 21:57:18 +0530 Subject: [PATCH] :zap: perf: initialize vectors with capacity by default & use `Arc` to partially clone memory cache struct (#603) --- src/cache/cacher.rs | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/cache/cacher.rs b/src/cache/cacher.rs index f027aff..604d527 100644 --- a/src/cache/cacher.rs +++ b/src/cache/cacher.rs @@ -2,10 +2,9 @@ //! from the upstream search engines in a json format. use error_stack::Report; +use futures::future::join_all; #[cfg(feature = "memory-cache")] -use mini_moka::sync::Cache as MokaCache; -#[cfg(feature = "memory-cache")] -use mini_moka::sync::ConcurrentCacheExt; +use moka::future::Cache as MokaCache; #[cfg(feature = "memory-cache")] use std::time::Duration; @@ -376,13 +375,13 @@ impl Cacher for RedisCache { } } /// TryInto implementation for SearchResults from Vec -use std::convert::TryInto; +use std::{convert::TryInto, sync::Arc}; impl TryInto for Vec { type Error = CacheError; fn try_into(self) -> Result { - serde_json::from_slice(&self).map_err(|_| CacheError::SerializationError) + bincode::deserialize_from(self.as_slice()).map_err(|_| CacheError::SerializationError) } } @@ -390,7 +389,7 @@ impl TryInto> for &SearchResults { type Error = CacheError; fn try_into(self) -> Result, Self::Error> { - serde_json::to_vec(self).map_err(|_| CacheError::SerializationError) + bincode::serialize(self).map_err(|_| CacheError::SerializationError) } } @@ -398,7 +397,16 @@ impl TryInto> for &SearchResults { #[cfg(feature = "memory-cache")] pub struct InMemoryCache { /// The backend cache which stores data. - cache: MokaCache>, + cache: Arc>>, +} + +#[cfg(feature = "memory-cache")] +impl Clone for InMemoryCache { + fn clone(&self) -> Self { + Self { + cache: self.cache.clone(), + } + } } #[cfg(feature = "memory-cache")] @@ -408,15 +416,17 @@ impl Cacher for InMemoryCache { log::info!("Initialising in-memory cache"); InMemoryCache { - cache: MokaCache::builder() - .time_to_live(Duration::from_secs(config.cache_expiry_time.into())) - .build(), + cache: Arc::new( + MokaCache::builder() + .time_to_live(Duration::from_secs(config.cache_expiry_time.into())) + .build(), + ), } } async fn cached_results(&mut self, url: &str) -> Result> { let hashed_url_string = self.hash_url(url); - match self.cache.get(&hashed_url_string) { + match self.cache.get(&hashed_url_string).await { Some(res) => self.post_process_search_results(res).await, None => Err(Report::new(CacheError::MissingValue)), } @@ -427,13 +437,18 @@ impl Cacher for InMemoryCache { search_results: &[SearchResults], urls: &[String], ) -> Result<(), Report> { + let mut tasks: Vec<_> = Vec::with_capacity(urls.len()); for (url, search_result) in urls.iter().zip(search_results.iter()) { let hashed_url_string = self.hash_url(url); let bytes = self.pre_process_search_results(search_result).await?; - self.cache.insert(hashed_url_string, bytes); + let new_self = self.clone(); + tasks.push(tokio::spawn(async move { + new_self.cache.insert(hashed_url_string, bytes).await + })); } - self.cache.sync(); + join_all(tasks).await; + Ok(()) } }