Comments (5)
ah, i missed that. thanks a lot :)
from moka.
Hi. Thank you for using Moka.
It would not be necessary to expose these APIs. You can write your oun hasher, which simply pass-through the pre-calculated hash value. And then you can give the hasher to the cache. Here is an example:
use std::{
hash::{BuildHasherDefault, Hash, Hasher},
sync::Arc,
};
//
// Define a struct for the key, which holds the key and its hash value.
//
#[derive(Clone, PartialEq, Eq)]
pub struct MyKey {
_key: Arc<Box<[u8]>>,
hash_value: u64,
}
impl MyKey {
pub fn new(key: Box<[u8]>, hash_value: u64) -> Self {
Self {
_key: Arc::new(key),
hash_value,
}
}
}
// Implement Hash. We will only use the pre-calculated hash value.
impl Hash for MyKey {
fn hash<H: Hasher>(&self, state: &mut H) {
self.hash_value.hash(state);
}
}
//
// Create a Hasher, which just uses a single u64 value as the final hash.
//
#[derive(Default)]
struct PassthroughHasher(u64);
impl Hasher for PassthroughHasher {
fn write_u64(&mut self, i: u64) {
self.0 = i;
}
fn write(&mut self, _bytes: &[u8]) {
unimplemented!()
}
fn finish(&self) -> u64 {
self.0
}
}
// Define a BuildHasher using our Hasher. We will give this to our cache.
type PassthroughBuildHasher = BuildHasherDefault<PassthroughHasher>;
fn main() {
let cache = moka::sync::Cache::builder()
.max_capacity(100)
// Give our build hasher to this cache.
.build_with_hasher(PassthroughBuildHasher::default());
let key1 = MyKey::new(b"key1".to_vec().into_boxed_slice(), 0);
let key2 = MyKey::new(b"key2".to_vec().into_boxed_slice(), 1);
cache.insert(key1.clone(), "value1");
cache.insert(key2.clone(), "value2");
assert_eq!(cache.get(&key1), Some("value1"));
assert_eq!(cache.get(&key2), Some("value2"));
}
Will this trick work for you?
from moka.
it would, but i would think it is perhaps more ergonomic to provide something similar to the raw_entry API from hashbrown https://docs.rs/hashbrown/latest/hashbrown/raw/struct.RawTable.html#method.insert, etc, especially if these functions are already implemented.
its a bit a question of API, i guess.
from moka.
Unfortunately, exposing sync::Cache
's get_with_hash
and insert_with_hash
does not solve your problem.
Like many other hash tables, Moka cache's internal hash table re-calculates hash values of existing keys when it resizes (reallocates) the storage area for growth. This operation is called rehashing. So even though, you use the pre-calculated hash when inserting and retrieving an entry, the cache will still re-calculate hashes from the keys when the cache grows. If you use a different hash function for pre-calculating the hash to the hash function that the cache is given at the build time, it will break; get_with_hash
will no longer be able to locate the key after growing.
On the other hand, the example code above with PassthroughHasher
ensures that the hash is never re-calculated from the key, at insertion, retrieval and rehashing times.
from moka.
Here is the rehash
method in Moka's internal hash table:
Line 396 in 915d9b0
and this is the line in rehash
that re-calculates hash from key.
Line 439 in 915d9b0
from moka.
Related Issues (20)
- Statistics for metrics HOT 3
- `get_with` deadlock HOT 13
- `.blocking().invalidate(key)` doesn't trigger eviction listener HOT 1
- RUSTSEC-2020-0168: mach is unmaintained HOT 2
- Can the eviction strategy be configured? HOT 8
- Upgrade the cache policy from TinyLFU to W-TinyLFU
- Bump the MSRV to 1.60
- optionally_try_get_with in moka::future::Cache HOT 1
- feature request: remove method HOT 2
- Use `std::thread::available_parallelism()` instead of `num_cpus` dependency
- Eviction listener not always called. HOT 2
- Cache housekeeper can panic (seen after 50M+ insertions) HOT 4
- CI: Miri test causes a complie error with Rust nightly-2023-05-27 HOT 2
- Implement Serialize trait for debugging webserver? HOT 2
- Data race found by `miri test` HOT 9
- CI: Enable Miri tests on `moka::cht::*` modules
- Memory corruption observed when using Moka v0.9.6 HOT 15
- unbounded capacity? HOT 2
- Possibility of using async runtime tasks instead of thread pools HOT 10
- oom caused after use #234's statistics record code HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moka.