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::cell::RefCell;
use std::cmp;
use std::mem;
const INITIAL_SIZE: usize = 1024;
const MIN_CAPACITY: usize = 1;
pub struct Arena<T> {
chunks: RefCell<ChunkList<T>>,
}
struct ChunkList<T> {
current: Vec<T>,
rest: Vec<Vec<T>>,
}
impl<T> Arena<T> {
pub fn new() -> Arena<T> {
let size = cmp::max(1, mem::size_of::<T>());
Arena::with_capacity(INITIAL_SIZE / size)
}
pub fn with_capacity(n: usize) -> Arena<T> {
let n = cmp::max(MIN_CAPACITY, n);
Arena {
chunks: RefCell::new(ChunkList {
current: Vec::with_capacity(n),
rest: vec![]
}),
}
}
pub fn alloc(&self, value: T) -> &mut T {
let mut chunks = self.chunks.borrow_mut();
let next_item_index = chunks.current.len();
chunks.current.push(value);
let new_item_ref = {
let new_item_ref = &mut chunks.current[next_item_index];
unsafe { mem::transmute::<&mut T, &mut T>(new_item_ref) }
};
if chunks.current.len() == chunks.current.capacity() {
chunks.grow();
}
new_item_ref
}
pub fn into_vec(self) -> Vec<T> {
let chunks = self.chunks.into_inner();
let mut result = chunks.current;
for mut vec in chunks.rest {
result.append(&mut vec);
}
result
}
}
impl<T> ChunkList<T> {
#[inline(never)]
#[cold]
fn grow(&mut self) {
let new_capacity = self.current.capacity().checked_mul(2).unwrap();
let chunk = mem::replace(&mut self.current, Vec::with_capacity(new_capacity));
self.rest.push(chunk);
}
}
#[test]
fn it_works() {
use std::cell::Cell;
struct DropTracker<'a>(&'a Cell<u32>);
impl<'a> Drop for DropTracker<'a> {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
}
struct Node<'a, 'b: 'a>(Option<&'a Node<'a, 'b>>, u32, DropTracker<'b>);
let drop_counter = Cell::new(0);
{
let arena = Arena::with_capacity(2);
let mut node: &Node = arena.alloc(Node(None, 1, DropTracker(&drop_counter)));
assert_eq!(arena.chunks.borrow().rest.len(), 0);
node = arena.alloc(Node(Some(node), 2, DropTracker(&drop_counter)));
assert_eq!(arena.chunks.borrow().rest.len(), 1);
node = arena.alloc(Node(Some(node), 3, DropTracker(&drop_counter)));
assert_eq!(arena.chunks.borrow().rest.len(), 1);
node = arena.alloc(Node(Some(node), 4, DropTracker(&drop_counter)));
assert_eq!(arena.chunks.borrow().rest.len(), 1);
assert_eq!(node.1, 4);
assert_eq!(node.0.unwrap().1, 3);
assert_eq!(node.0.unwrap().0.unwrap().1, 2);
assert_eq!(node.0.unwrap().0.unwrap().0.unwrap().1, 1);
assert !(node.0.unwrap().0.unwrap().0.unwrap().0.is_none());
mem::drop(node);
assert_eq!(drop_counter.get(), 0);
let mut node: &Node = arena.alloc(Node(None, 5, DropTracker(&drop_counter)));
assert_eq!(arena.chunks.borrow().rest.len(), 1);
node = arena.alloc(Node(Some(node), 6, DropTracker(&drop_counter)));
assert_eq!(arena.chunks.borrow().rest.len(), 2);
node = arena.alloc(Node(Some(node), 7, DropTracker(&drop_counter)));
assert_eq!(arena.chunks.borrow().rest.len(), 2);
assert_eq!(drop_counter.get(), 0);
assert_eq!(node.1, 7);
assert_eq!(node.0.unwrap().1, 6);
assert_eq!(node.0.unwrap().0.unwrap().1, 5);
assert !(node.0.unwrap().0.unwrap().0.is_none());
assert_eq!(drop_counter.get(), 0);
}
assert_eq!(drop_counter.get(), 7);
let arena = Arena::with_capacity(1);
for &s in &["t", "e", "s", "t"] {
arena.alloc(String::from(s));
}
let vec = arena.into_vec();
assert_eq!(vec.len(), 4);
assert_eq!(vec.iter().filter(|el| *el == "t").count(), 2);
assert_eq!(vec.iter().filter(|el| *el == "e").count(), 1);
assert_eq!(vec.iter().filter(|el| *el == "s").count(), 1);
}