inc_complete/storage/
hashmapped.rs1use dashmap::DashMap;
2
3use crate::{Cell, storage::StorageFor};
4use std::hash::{BuildHasher, Hash};
5
6use super::OutputType;
7
8pub struct HashMapStorage<K, Hasher = rustc_hash::FxBuildHasher>
9where
10 K: OutputType + Eq + Hash,
11 Hasher: BuildHasher,
12{
13 key_to_cell: DashMap<K, Cell, Hasher>,
14 cell_to_key: DashMap<Cell, (K, Option<K::Output>), Hasher>,
15}
16
17impl<K, H> Default for HashMapStorage<K, H>
18where
19 K: OutputType + Eq + Hash,
20 H: Default + BuildHasher + Clone,
21{
22 fn default() -> Self {
23 Self {
24 key_to_cell: Default::default(),
25 cell_to_key: Default::default(),
26 }
27 }
28}
29
30impl<K, H> StorageFor<K> for HashMapStorage<K, H>
31where
32 K: Clone + Eq + Hash + OutputType,
33 K::Output: Eq + Clone,
34 H: BuildHasher + Clone,
35{
36 fn get_cell_for_computation(&self, key: &K) -> Option<Cell> {
37 self.key_to_cell.get(key).map(|value| *value)
38 }
39
40 fn insert_new_cell(&self, cell: Cell, key: K) {
41 self.key_to_cell.insert(key.clone(), cell);
42 self.cell_to_key.insert(cell, (key, None));
43 }
44
45 fn get_input(&self, cell: Cell) -> K {
46 self.cell_to_key.get(&cell).unwrap().0.clone()
47 }
48
49 fn get_output(&self, cell: Cell) -> Option<K::Output> {
50 self.cell_to_key.get(&cell).unwrap().1.clone()
51 }
52
53 fn update_output(&self, cell: Cell, new_value: K::Output) -> bool {
54 let mut previous_output = self.cell_to_key.get_mut(&cell).unwrap();
55 let changed = K::ASSUME_CHANGED
56 || previous_output
57 .1
58 .as_ref()
59 .is_none_or(|value| *value != new_value);
60 previous_output.1 = Some(new_value);
61 changed
62 }
63
64 fn gc(&mut self, used_cells: &std::collections::HashSet<Cell>) {
65 self.cell_to_key.retain(|cell, _| used_cells.contains(cell));
67 self.key_to_cell.retain(|_, cell| used_cells.contains(cell));
68 }
69}
70
71impl<K, H> serde::Serialize for HashMapStorage<K, H>
72where
73 K: serde::Serialize + OutputType + Eq + Hash + Clone,
74 K::Output: serde::Serialize + Clone,
75 H: BuildHasher + Clone,
76{
77 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
78 where
79 S: serde::Serializer,
80 {
81 let mut cell_to_key_vec: Vec<(Cell, (K, Option<K::Output>))> =
82 Vec::with_capacity(self.cell_to_key.len());
83
84 for kv in self.cell_to_key.iter() {
85 let cell = *kv.key();
86 let (key, value) = kv.value().clone();
87 cell_to_key_vec.push((cell, (key, value)));
88 }
89
90 cell_to_key_vec.serialize(serializer)
91 }
92}
93
94impl<'de, K, H> serde::Deserialize<'de> for HashMapStorage<K, H>
95where
96 K: serde::Deserialize<'de> + Hash + Eq + OutputType + Clone,
97 K::Output: serde::Deserialize<'de>,
98 H: Default + BuildHasher + Clone,
99{
100 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
101 where
102 D: serde::Deserializer<'de>,
103 {
104 let cell_to_key_vec: Vec<(Cell, (K, Option<K::Output>))> =
105 serde::Deserialize::deserialize(deserializer)?;
106
107 let key_to_cell = DashMap::default();
108 let cell_to_key = DashMap::default();
109
110 for (cell, (key, value)) in cell_to_key_vec {
111 key_to_cell.insert(key.clone(), cell);
112 cell_to_key.insert(cell, (key, value));
113 }
114
115 Ok(HashMapStorage {
116 cell_to_key,
117 key_to_cell,
118 })
119 }
120}