forked from ijl/orjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.rs
More file actions
45 lines (36 loc) · 1.05 KB
/
Copy pathcache.rs
File metadata and controls
45 lines (36 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
use crate::typeref::*;
use ahash::CallHasher;
use associative_cache::replacement::RoundRobinReplacement;
use associative_cache::*;
use once_cell::unsync::OnceCell;
use std::os::raw::c_void;
#[repr(transparent)]
pub struct CachedKey {
ptr: *mut c_void,
}
unsafe impl Send for CachedKey {}
unsafe impl Sync for CachedKey {}
impl CachedKey {
pub fn new(ptr: *mut pyo3_ffi::PyObject) -> CachedKey {
CachedKey {
ptr: ptr as *mut c_void,
}
}
pub fn get(&mut self) -> *mut pyo3_ffi::PyObject {
let ptr = self.ptr as *mut pyo3_ffi::PyObject;
ffi!(Py_INCREF(ptr));
ptr
}
}
impl Drop for CachedKey {
fn drop(&mut self) {
ffi!(Py_DECREF(self.ptr as *mut pyo3_ffi::PyObject));
}
}
pub type KeyMap =
AssociativeCache<u64, CachedKey, Capacity1024, HashDirectMapped, RoundRobinReplacement>;
pub static mut KEY_MAP: OnceCell<KeyMap> = OnceCell::new();
pub fn cache_hash(key: &[u8]) -> u64 {
<[u8]>::get_hash(&key, unsafe { &*HASH_BUILDER })
}