Skip to content

Commit

Permalink
Remove RefCell.
Browse files Browse the repository at this point in the history
  • Loading branch information
cryscan committed Aug 27, 2023
1 parent c12549d commit 9caf708
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "web-rwkv"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
authors = ["Zhenyuan Zhang <[email protected]>"]
license = "MIT OR Apache-2.0"
Expand Down
9 changes: 3 additions & 6 deletions src/tensor/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
cell::RefCell,
collections::HashMap,
hash::Hash,
sync::{Arc, RwLock},
Expand All @@ -9,7 +8,7 @@ use std::{
#[derive(Debug)]
pub struct ResourceCache<K, V> {
max_count: usize,
map: RefCell<RwLock<HashMap<K, (Arc<V>, usize)>>>,
map: RwLock<HashMap<K, (Arc<V>, usize)>>,
}

impl<K, V> Default for ResourceCache<K, V> {
Expand All @@ -26,16 +25,14 @@ where
K: PartialEq + Eq + Hash,
{
pub fn query(&self, key: &K) -> Option<Arc<V>> {
let map = self.map.borrow();
let map = map.read().unwrap();
let map = self.map.read().unwrap();
map.get(key).cloned().map(|(v, _)| v)
}

pub fn request(&self, key: K, op: impl FnOnce() -> V) -> Arc<V> {
let buffer = self.query(&key).unwrap_or_else(|| Arc::new(op()));

let map = self.map.borrow_mut();
let mut map = map.write().unwrap();
let mut map = self.map.write().unwrap();
map.insert(key, (buffer.clone(), 0));

map.retain(|_, (_, count)| {
Expand Down

0 comments on commit 9caf708

Please sign in to comment.