chained/
chained.rs

1use stalloc::{AllocChain, SyncStalloc};
2
3use std::{alloc::System, hint::black_box, time::Instant};
4
5// Create a global allocator with 1024 blocks of stack memory,
6// but fall back to the system allocator if we ever OOM.
7// Note: changing this to `UnsafeStalloc` almost doubles speed...
8#[global_allocator]
9static GLOBAL: AllocChain<SyncStalloc<1024, 8>, System> = SyncStalloc::new().chain(&System);
10
11fn main() {
12	let start = Instant::now();
13
14	let mut big_strings = vec![];
15
16	// Now create lots of small strings
17	for i in 0..100_000_000 {
18		black_box(String::from("hello!"));
19
20		// Every once in a while, create and store a really big string
21		if i % 10000 == 0 {
22			big_strings.push("x".repeat(100_000));
23		}
24	}
25
26	for s in big_strings {
27		black_box(s);
28	}
29
30	println!("Elapsed: {}ms", start.elapsed().as_millis());
31}