fast_vectors/
fast_vectors.rs

1use stalloc::UnsafeStalloc;
2use std::{mem, time::Instant};
3
4// Create a global allocator with 1000 blocks, each 4 bytes in length.
5// SAFETY: The program is single-threaded.
6#[global_allocator]
7static GLOBAL: UnsafeStalloc<1000, 4> = unsafe { UnsafeStalloc::new() };
8
9fn main() {
10	let start = Instant::now();
11	for _ in 0..10_000_000 {
12		let mut a = vec![];
13		let mut b = vec![];
14		for i in 0..10 {
15			a.push(i);
16			b.push(i);
17		}
18
19		mem::forget(a);
20		mem::forget(b);
21
22		// By clearing the global allocator, we can quickly drop both vectors together.
23		// SAFETY: There are no more active allocations into `GLOBAL`.
24		unsafe {
25			GLOBAL.clear();
26		}
27	}
28
29	println!("Elapsed: {}ms", start.elapsed().as_millis());
30}