-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dmitry Dygalo <[email protected]>
- Loading branch information
1 parent
e1fb6fe
commit baf1796
Showing
16 changed files
with
546 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use core::hash::{BuildHasherDefault, Hash, Hasher}; | ||
use std::{ | ||
collections::{hash_map::Entry, HashMap}, | ||
sync::Arc, | ||
}; | ||
|
||
use ahash::AHasher; | ||
use fluent_uri::Uri; | ||
use parking_lot::RwLock; | ||
|
||
use crate::{hasher::BuildNoHashHasher, uri, Error}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub(crate) struct UriCache { | ||
cache: HashMap<u64, Arc<Uri<String>>, BuildNoHashHasher>, | ||
} | ||
|
||
impl UriCache { | ||
pub(crate) fn new() -> Self { | ||
Self { | ||
cache: HashMap::with_hasher(BuildHasherDefault::default()), | ||
} | ||
} | ||
|
||
pub(crate) fn with_capacity(capacity: usize) -> Self { | ||
Self { | ||
cache: HashMap::with_capacity_and_hasher(capacity, BuildHasherDefault::default()), | ||
} | ||
} | ||
|
||
pub(crate) fn resolve_against( | ||
&mut self, | ||
base: &Uri<&str>, | ||
uri: impl AsRef<str>, | ||
) -> Result<Arc<Uri<String>>, Error> { | ||
let mut hasher = AHasher::default(); | ||
(base.as_str(), uri.as_ref()).hash(&mut hasher); | ||
let hash = hasher.finish(); | ||
|
||
Ok(match self.cache.entry(hash) { | ||
Entry::Occupied(entry) => Arc::clone(entry.get()), | ||
Entry::Vacant(entry) => { | ||
let new = Arc::new(uri::resolve_against(base, uri.as_ref())?); | ||
Arc::clone(entry.insert(new)) | ||
} | ||
}) | ||
} | ||
|
||
pub(crate) fn into_shared(self) -> SharedUriCache { | ||
SharedUriCache { | ||
cache: RwLock::new(self.cache), | ||
} | ||
} | ||
} | ||
|
||
/// A dedicated type for URI resolution caching. | ||
#[derive(Debug)] | ||
pub(crate) struct SharedUriCache { | ||
cache: RwLock<HashMap<u64, Arc<Uri<String>>, BuildNoHashHasher>>, | ||
} | ||
|
||
impl Clone for SharedUriCache { | ||
fn clone(&self) -> Self { | ||
Self { | ||
cache: RwLock::new( | ||
self.cache | ||
.read() | ||
.iter() | ||
.map(|(k, v)| (*k, Arc::clone(v))) | ||
.collect(), | ||
), | ||
} | ||
} | ||
} | ||
|
||
impl SharedUriCache { | ||
pub(crate) fn resolve_against( | ||
&self, | ||
base: &Uri<&str>, | ||
uri: impl AsRef<str>, | ||
) -> Result<Arc<Uri<String>>, Error> { | ||
let mut hasher = AHasher::default(); | ||
(base.as_str(), uri.as_ref()).hash(&mut hasher); | ||
let hash = hasher.finish(); | ||
|
||
if let Some(cached) = self.cache.read().get(&hash).cloned() { | ||
return Ok(cached); | ||
} | ||
|
||
let new = Arc::new(uri::resolve_against(base, uri.as_ref())?); | ||
self.cache.write().insert(hash, Arc::clone(&new)); | ||
Ok(new) | ||
} | ||
|
||
pub(crate) fn into_local(self) -> UriCache { | ||
UriCache { | ||
cache: self.cache.into_inner(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use core::hash::{BuildHasherDefault, Hasher}; | ||
|
||
pub(crate) type BuildNoHashHasher = BuildHasherDefault<NoHashHasher>; | ||
|
||
#[derive(Default)] | ||
pub(crate) struct NoHashHasher(u64); | ||
|
||
impl Hasher for NoHashHasher { | ||
fn finish(&self) -> u64 { | ||
self.0 | ||
} | ||
fn write(&mut self, _: &[u8]) { | ||
unreachable!("Should not be used") | ||
} | ||
fn write_u8(&mut self, _: u8) { | ||
unreachable!("Should not be used") | ||
} | ||
fn write_u16(&mut self, _: u16) { | ||
unreachable!("Should not be used") | ||
} | ||
fn write_u32(&mut self, _: u32) { | ||
unreachable!("Should not be used") | ||
} | ||
fn write_u64(&mut self, n: u64) { | ||
self.0 = n; | ||
} | ||
fn write_usize(&mut self, _: usize) { | ||
unreachable!("Should not be used") | ||
} | ||
fn write_i8(&mut self, _: i8) { | ||
unreachable!("Should not be used") | ||
} | ||
fn write_i16(&mut self, _: i16) { | ||
unreachable!("Should not be used") | ||
} | ||
fn write_i32(&mut self, _: i32) { | ||
unreachable!("Should not be used") | ||
} | ||
fn write_i64(&mut self, _: i64) { | ||
unreachable!("Should not be used") | ||
} | ||
fn write_isize(&mut self, _: isize) { | ||
unreachable!("Should not be used") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.