Reblessive
A heap allocated runtime for deeply recursive algorithms.
Turn your cursed recursive algorithm into a blessed heap allocated structure which won't
overflow the stack, regardless of depth.
Example
use std::{
mem::MaybeUninit,
time::{Duration, Instant},
};
use reblessive::{Ctx, Stack};
async fn heavy_fibbo(mut ctx: Ctx<'_>, n: usize) -> usize {
let mut ballast: MaybeUninit<[u8; 1024 * 1024]> = std::mem::MaybeUninit::uninit();
std::hint::black_box(&mut ballast);
match n {
0 => 1,
1 => 1,
x => {
ctx.run(move |ctx| heavy_fibbo(ctx, x - 1)).await
+ ctx.run(move |ctx| heavy_fibbo(ctx, x - 2)).await
}
}
}
fn main() {
let mut stack = Stack::new();
let res = stack.run(|ctx| heavy_fibbo(ctx, 20)).finish();
println!("result: {res}");
assert_eq!(res, 10946);
let mut runner = stack.run(|ctx| heavy_fibbo(ctx, 60));
let start = Instant::now();
loop {
if let Some(x) = runner.step() {
println!("finished: {x}")
}
if start.elapsed() > Duration::from_secs(3) {
println!("Timed out!");
break;
}
}
}