use std::time::Instant;
use gladiator::{trace::Trace, Arena, Gc};
struct GcTest<'arena> {
id: usize,
other: Option<Gc<'arena, Self>>,
}
impl<'arena> GcTest<'arena> {
pub fn new(id: usize) -> Self {
println!("Gc#{} created", id);
Self { id, other: None }
}
pub fn new_ref(id: usize, other: Gc<'arena, Self>) -> Self {
println!("Gc#{} created", id);
Self { id, other: Some(other) }
}
}
unsafe impl<'arena> Trace<'arena, Self> for GcTest<'arena> {
unsafe fn send(&self, signal: &gladiator::trace::Signal<'arena, Self>) {
match &self.other {
Some(other) => other.send(signal),
_ => {},
}
}
unsafe fn finalize(&mut self) {
println!("GcTest#{} finalized", self.id);
}
}
impl<'arena> Drop for GcTest<'arena> {
fn drop(&mut self) {
println!("GcTest#{} dropped", self.id);
}
}
fn main() {
let start = Instant::now();
let arena = Arena::new();
let mut last = arena.host(GcTest::new(0));
for i in 1..=2000 {
last = arena.host(GcTest::new_ref(i, last.as_weak()));
}
arena.collect();
let end = start.elapsed().as_nanos() as f64 / 1_000_000f64;
println!("{}ms", end);
}