Performance of recursive function

I rewrote the function to fixed depth with only for loops:
-it is slighly faster
-allocations do not disappear
-though execution time varies a lot.

btime GAME.perf_loop($game,4,$moves)
  7.826 ms (162661 allocations: 7.45 MiB)
 ────────────────────────────────────────────────────────────────────
                            Time                    Allocations      
                   ───────────────────────   ────────────────────────
 Tot / % measured:      5.01s /   0.2%           4.15GiB /   0.2%    

 Section   ncalls     time    %tot     avg     alloc    %tot      avg
 ────────────────────────────────────────────────────────────────────
 1              1   8.40ms  100.0%  8.40ms   7.45MiB  100.0%  7.45MiB
   2           16   8.39ms   99.9%   525μs   7.44MiB  100.0%   476KiB
     3        256   8.33ms   99.2%  32.5μs   7.43MiB   99.8%  29.7KiB
       4    6.46k   7.03ms   83.7%  1.09μs   7.14MiB   95.8%  1.13KiB
 ───────────────────────────────────────────────────────────────────

1 is te most outer loop and 4 the most inner.
It seems that the function play is doing one allocation … I had changed Game struct to mutable, when it was intended not to be. Now almost all allocations disappeared.
Sorry for your time

1 Like