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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use std::rc::{Rc, Weak};
use super::cell_impl::VirtualCellWrapper;
use super::rc::{RcCell, RcCellFamily};
use super::{Cell, CellDescriptor, CellHash};
#[cfg(feature = "stats")]
use super::CellTreeStats;
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum UsageTreeMode {
OnLoad,
OnDataAccess,
}
pub struct RcUsageTree {
state: Rc<RcUsageTreeState>,
}
impl RcUsageTree {
pub fn new(mode: UsageTreeMode) -> Self {
RcUsageTree {
state: Rc::new(RcUsageTreeState {
mode,
visited: Default::default(),
}),
}
}
pub fn track(&self, cell: &RcCell) -> RcCell {
self.state.insert(cell, UsageTreeMode::OnLoad);
Rc::new(RcUsageCell {
cell: cell.clone(),
usage_tree: Rc::downgrade(&self.state),
children: Default::default(),
})
}
pub fn contains(&self, repr_hash: &CellHash) -> bool {
if let Some(cell) = self.state.visited.borrow().get(repr_hash) {
cell.include
} else {
false
}
}
}
struct RcUsageTreeState {
mode: UsageTreeMode,
visited: std::cell::RefCell<ahash::HashMap<CellHash, VisitedCell>>,
}
impl RcUsageTreeState {
fn insert(self: &Rc<Self>, cell: &RcCell, ctx: UsageTreeMode) {
let repr_hash = cell.repr_hash();
let mut visited = self.visited.borrow_mut();
if let Some(visited) = visited.get_mut(repr_hash) {
visited.include |= self.mode == ctx;
} else {
visited.insert(
*repr_hash,
VisitedCell {
include: self.mode == ctx,
_cell: cell.clone(),
},
);
}
}
}
struct VisitedCell {
include: bool,
_cell: RcCell,
}
struct RcUsageCell {
cell: RcCell,
usage_tree: Weak<RcUsageTreeState>,
children: std::cell::UnsafeCell<[Option<Rc<RcUsageCell>>; 4]>,
}
impl RcUsageCell {
fn load_reference(&self, index: u8) -> Option<&Rc<RcUsageCell>> {
if index < 4 {
let children = unsafe { &mut *self.children.get() };
Some(match &mut children[index as usize] {
Some(value) => value,
slot @ None => {
let child = self.cell.as_ref().reference_cloned(index)?;
if let Some(usage_tree) = self.usage_tree.upgrade() {
usage_tree.insert(&child, UsageTreeMode::OnLoad);
}
slot.insert(Rc::new(RcUsageCell {
cell: child.clone(),
usage_tree: self.usage_tree.clone(),
children: Default::default(),
}))
}
})
} else {
None
}
}
}
impl Cell<RcCellFamily> for RcUsageCell {
fn descriptor(&self) -> CellDescriptor {
self.cell.as_ref().descriptor()
}
fn data(&self) -> &[u8] {
if let Some(usage_tree) = self.usage_tree.upgrade() {
usage_tree.insert(&self.cell, UsageTreeMode::OnDataAccess);
}
self.cell.as_ref().data()
}
fn bit_len(&self) -> u16 {
self.cell.as_ref().bit_len()
}
fn reference(&self, index: u8) -> Option<&dyn Cell<RcCellFamily>> {
Some(self.load_reference(index)?.as_ref())
}
fn reference_cloned(&self, index: u8) -> Option<RcCell> {
Some(self.load_reference(index)?.clone())
}
fn virtualize(&self) -> &dyn Cell<RcCellFamily> {
VirtualCellWrapper::wrap(self)
}
fn hash(&self, level: u8) -> &CellHash {
self.cell.as_ref().hash(level)
}
fn depth(&self, level: u8) -> u16 {
self.cell.as_ref().depth(level)
}
#[cfg(feature = "stats")]
fn stats(&self) -> CellTreeStats {
self.cell.as_ref().stats()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
println!("SIZE: {}", std::mem::size_of::<RcUsageCell>());
}
}