1#![feature(allocator_api)]
2#![feature(slice_ptr_get)]
3#![feature(linked_list_retain)]
4#![feature(linked_list_cursors)]
5#![feature(portable_simd)]
6#![feature(str_as_str)]
7#![feature(box_vec_non_null)]
8extern crate alloc;
9extern crate core;
10
11pub mod arena;
12pub mod buffers;
13pub mod cpu;
14pub mod mem;
15pub mod dune;
16pub mod base;
17pub mod serde;
18
19pub use arena::Arena;
20pub use mem::*;
21
22#[cfg(test)]
23mod tests {
24 use crate::buffers::{buffer_find, RUMBuffer, RUMBufferIteratorExt};
25 use crate::cpu::{cpu_find, cpu_slice_to_array_padded};
26 use crate::mem::constants::*;
27 use crate::{as_slice_mut, direct_alloc, rumtk_arena_new, Arena};
28 use std::alloc::alloc;
29 use std::collections::{HashMap, VecDeque};
30
31 macro_rules! rumtk_benchmark_snippet {
32 ( $closure:expr ) => {{
33 use std::time::Instant;
34
35 let start = Instant::now();
36 let r = $closure();
37 let end = Instant::now();
38
39 let time = end - start;
40 let micros = time.as_micros();
41
42 (r, micros)
43 }};
44 }
45
46 #[test]
47 fn test_cpu_slice_to_array_padded() {
48 let expected = b"Hello World\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
49 let result = cpu_slice_to_array_padded::<32, 0>(b"Hello World");
50 assert_eq!(&result, expected, "Stack array was not properly padded!");
51 }
52
53 #[test]
54 fn test_cpu_slice_to_array_padded_newline() {
55 let expected = b"Hello World\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
56 let result = cpu_slice_to_array_padded::<32, b'\n'>(b"Hello World");
57 assert_eq!(&result, expected, "Stack array was not properly padded!");
58 }
59
60 #[test]
61 fn test_cpu_find_simd_aligned() {
62 let input = b"Hello World\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n00000000000000000000000000000000";
63 let expected = 6;
64 let result = cpu_find(input, b'W').unwrap();
65 assert_eq!(result, expected, "Failed to find needle in haystack!");
66 }
67
68 #[test]
69 fn test_cpu_find_simd_unaligned() {
70 let input = b"Hello World\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
71 let expected = 6;
72 let result = cpu_find(input, b'W').unwrap();
73 assert_eq!(result, expected, "Failed to find needle in haystack!");
74 }
75
76 #[test]
77 fn test_cpu_find_simd_unaligned_none() {
78 let input = b"Hello World\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
79 let expected = 6;
80 let result = cpu_find(input, b'\0');
81 assert!(result.is_none() || (result.unwrap() -1) < input.len(), "Succeeded to find needle in haystack when the search character is not part of the haystack!");
82 }
83
84 #[test]
85 fn test_buffer_find() {
86 let input = b"Hello World\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
87 let expected = 9;
88 let result = buffer_find(input, b"ld\n");
89
90 assert_eq!(result, expected, "Succeeded to find needle in haystack when the search character is not part of the haystack!");
91 }
92
93 #[test]
94 fn test_buffer_find2() {
95 let input = b"Hello World\n\n\n\n\n\n\nasdjklfkasjwaoia poaw ml;,\n\n\n\n\n\n\n\n\n\n\n\n\n";
96 let expected = 40;
97 let result = buffer_find(input, b"ml;");
98
99 assert_eq!(result, expected, "Succeeded to find needle in haystack when the search character is not part of the haystack!");
100 }
101
102 #[test]
103 fn test_buffer_split() {
104 let input = RUMBuffer::from(b"Hello World\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
105 let expected = 20;
106 let mut result = 0;
107
108 for _ in input.split_fast(b'\n') {
109 result += 1;
110 }
111
112 assert_eq!(result, expected, "Succeeded to find needle in haystack when the search character is not part of the haystack!");
113 }
114
115 #[test]
116 fn test_arena_direct_allocation() {
117
118 let (r, time) = rumtk_benchmark_snippet!(|| {
119 unsafe { as_slice_mut(direct_alloc(DEFAULT_GLOBAL_MB_ALLOCATION_LAYOUT), DEFAULT_GLOBAL_MB_ALLOCATION) }
120 });
121
122 assert_eq!(r.len(), DEFAULT_GLOBAL_MB_ALLOCATION);
123 println!("Allocation took long! => {}us", time)
124
125 }
126
127 #[test]
128 fn test_arena_basic_allocation() {
129 let (r, time) = rumtk_benchmark_snippet!(|| {
130 unsafe { as_slice_mut(alloc(DEFAULT_GLOBAL_MB_ALLOCATION_LAYOUT), DEFAULT_GLOBAL_MB_ALLOCATION) }
131 });
132
133 assert_eq!(r.len(), DEFAULT_GLOBAL_MB_ALLOCATION);
134 println!("Allocation took long! => {}us", time)
135
136 }
137
138 #[test]
139 fn test_arena_allocate_and_use() {
140 let (r, time) = rumtk_benchmark_snippet!(|| {
141 let slice = unsafe { as_slice_mut(alloc(DEFAULT_GLOBAL_MB_ALLOCATION_LAYOUT), DEFAULT_GLOBAL_MB_ALLOCATION) };
142 let v = slice.to_vec();
143 let mut buffer = RUMBuffer::from(v);
144 let mut chunk = buffer.freeze();
145
146 for _ in 0..(DEFAULT_GLOBAL_MB_ALLOCATION/5) {
147 chunk.split_to(5);
148 }
149
150 chunk
151 });
152
153 println!("Allocation took {}us", time)
154
155 }
156
157 #[test]
158 fn test_arena_simple_vec_allocation() {
159 let arena = Arena::with_capacity(1024);
160 let mut v = Vec::<usize>::with_capacity(10);
161
162 v.push(10);
163 v.push(10);
164
165 assert_eq!(v, [10, 10], "Failed to allocate and fill a small vector!");
166 }
167
168 #[test]
169 fn test_arena_simple_vec_reallocation() {
170 let arena = Arena::with_capacity(1024);
171 let mut v = Vec::<usize>::with_capacity(1);
172
173 v.push(10);
174 v.push(10);
175
176 assert_eq!(v, [10, 10], "Failed to reallocate and fill a small vector!");
177 }
178
179 #[test]
180 fn test_arena_allocate_more_than_allowed() {
181 let mut arena = Arena::with_capacity(5);
182 let v = arena.commit(10);
183
184 assert!(v.is_err(), "Arena did not emit error upon allocation of byte count higher than current capacity.");
185 }
186
187 #[test]
188 fn test_arena_create_vec_with_macro() {
189 let arena = Arena::with_capacity(5);
190 let v: Vec<String> = vec![];
191
192 assert!(v.is_empty(), "Failed to create vector with arena allocation enabled.");
193 }
194
195 #[test]
196 fn test_arena_benchmark_arenavec_vs_vec() {
197 struct ptr {
198 data: usize,
199 len: usize,
200 index: usize,
201 bad: usize,
202 }
203
204 impl ptr {
205 pub fn new() -> Self {
206 Self {
207 data: 0,
208 len: 0,
209 index: 0,
210 bad: 0,
211 }
212 }
213 }
214
215 let total_items = 20000;
216
217 let (arena, arena_time) = rumtk_benchmark_snippet!(|| {
218 let total_bytes = (total_items * size_of::<ptr>()) + size_of::<Vec<ptr>>();
219 Arena::with_capacity(total_bytes)
220 });
221
222 let (arena_vec_r, arena_vec_time) = rumtk_benchmark_snippet!(|| {
223 let mut v: Vec<ptr> = vec![];
224
225 for _ in 0..total_items {
226 v.push(ptr::new());
227 }
228
229 v
230 });
231
232 let (vec_r, vec_time) = rumtk_benchmark_snippet!(|| {
233 let mut v = Vec::<ptr>::with_capacity(total_items);
234
235 for _ in 0..total_items {
236 v.push(ptr::new());
237 }
238
239 v
240 });
241
242 let total_arena_vec_time = arena_time + arena_vec_time;
243 println!("ArenaVec => {} us vs. Vec => {} us.", total_arena_vec_time, vec_time);
244
245 }
247
248 #[test]
249 fn test_arena_create_vec_with_macro_with_items() {
250 let arena = Arena::with_capacity(50);
251 let expected = &["Hello", "World", "!"];
252 let v: Vec<&str> = vec!["Hello", "World", "!"];
253
254 assert_eq!(v.as_slice(), expected, "Failed to create vector with arena allocation enabled and item slice.");
255 }
256
257 #[test]
258 fn test_arena_create_vecdeque_with_macro() {
259 let arena = Arena::with_capacity(5);
260 let v: VecDeque<String> = VecDeque::new();
261
262 assert!(v.is_empty(), "Failed to create vector with arena allocation enabled.");
263 }
264
265 #[test]
266 fn test_arena_create_vecdeque_with_macro_with_items() {
267 let arena = Arena::with_capacity(50);
268 let expected = ["Hello", "World", "!"];
269 let mut v: VecDeque<&str> = VecDeque::from(expected.clone());
270
271 assert_eq!(v.pop_front(), Some(expected[0]), "Failed to create queue with arena allocation enabled and item slice.");
272 }
273
274 #[test]
275 fn test_arena_create_hashmap_with_macro() {
276 let arena = Arena::with_capacity(5);
277 let v: HashMap<&str, &str> = HashMap::new();
278
279 assert!(v.is_empty(), "Failed to create vector with arena allocation enabled.");
280 }
281
282 #[test]
283 fn test_arena_create_hashmap_with_macro_with_items() {
284 let arena = Arena::with_capacity(120);
285 let expected = [(0, "Hello"), (1, "World"), (2, "!")];
286 let v: HashMap<usize, &str> = HashMap::from_iter(expected.clone());
287
288 assert_eq!(v[&0], expected[0].1, "Failed to create hashmap with arena allocation enabled and item slice.");
289 }
290
291 #[test]
292 fn test_arena_vec_debug_print() {
293 let arena = rumtk_arena_new!(500);
294 let mut test_vec = Vec::new();
295 let expected = ["Hello", "World", "!"];
296
297 for s in expected.iter() {
298 test_vec.push(s);
299 }
300
301 println!("{:?}", &test_vec);
302 }
303
304 #[test]
305 fn test_arena_map_debug_print() {
306 let expected = [(5, "Hello"), (1, "World"), (3, "!")];
307
308
309 let m = HashMap::<usize, &str>::from_iter(expected.clone());
310
311 for (k, v) in expected.iter() {
312 assert!(m.contains_key(k), "Key missing!");
313 assert_eq!(v, &m[k], "Contents mismatch!");
314 }
315 }
316}