efficient incremental memoization without space leaks