got a 4x speedup by memoizing heuristic