Tune compute_avg_stack function