Note sure if this is really a problem because it should not be called from anywhere else but already optimized memory pooling (board data creation). However, it is top 1.
func NewLegalMoves (indexSize int) chesstypes.LegalMoves {
ret := make(chesstypes.LegalMoves, indexSize)
for i := range ret {
ret[i] = make(chesstypes.IndexList, 0, indexSize)
}
return ret[:0]
}
This allocates a lot (top 1):
$ go tool pprof -alloc_objects mem.prof
Showing top 10 nodes out of 20
flat flat% sum% cum cum%
133124 94.46% 94.46% 133124 94.46% chess/internal/chessboard.NewLegalMoves (inline)
6554 4.65% 99.11% 6554 4.65% chess/internal/chessai/advancedchesscomputer.NewMinimaxJob (inline)
1024 0.73% 99.84% 134148 95.18% chess/internal/chessboard.NewBulkGameBoard
The loop is the problem:
(pprof) list NewLegalMoves
Total: 140934
ROUTINE ======================== chess/internal/chessboard.NewLegalMoves in /Users/jhh/git/hangovergames/chess/internal/chessboard/gameboard.go
133124 133124 (flat, cum) 94.46% of Total
. . 84:func NewLegalMoves (indexSize int) chesstypes.LegalMoves {
2051 2051 85: ret := make(chesstypes.LegalMoves, indexSize)
. . 86: for i := range ret {
131073 131073 87: ret[i] = make(chesstypes.IndexList, 0, indexSize)
. . 88: }
. . 89: return ret[:0]
. . 90:}
. . 91:
. . 92:func NewBulkGameBoard(width, height chesstypes.BoardIndex) *GameBoard {
Showing top 10 nodes out of 26
flat flat% sum% cum cum%
11782.63kB 84.72% 84.72% 11782.63kB 84.72% chess/internal/chessboard.NewLegalMoves (inline)
587.37kB 4.22% 88.95% 587.37kB 4.22% github.com/hyperifyio/statelessdb/pkg/logs.NewLogger
513.12kB 3.69% 92.63% 513.12kB 3.69% chess/internal/chessai/advancedchesscomputer.filterAvailableMoves
512.25kB 3.68% 96.32% 12294.88kB 88.41% chess/internal/chessboard.NewBulkGameBoard
512.04kB 3.68% 100% 512.04kB 3.68% chess/internal/chessai/advancedchesscomputer.NewMinimaxJob (inline)
(pprof) list NewLegalMoves
Total: 13.58MB
ROUTINE ======================== chess/internal/chessboard.NewLegalMoves in /Users/jhh/git/hangovergames/chess/internal/chessboard/gameboard.go
11.51MB 11.51MB (flat, cum) 84.72% of Total
. . 84:func NewLegalMoves (indexSize int) chesstypes.LegalMoves {
3.51MB 3.51MB 85: ret := make(chesstypes.LegalMoves, indexSize)
. . 86: for i := range ret {
8MB 8MB 87: ret[i] = make(chesstypes.IndexList, 0, indexSize)
. . 88: }
. . 89: return ret[:0]
. . 90:}
. . 91:
. . 92:func NewBulkGameBoard(width, height chesstypes.BoardIndex) *GameBoard {
Note sure if this is really a problem because it should not be called from anywhere else but already optimized memory pooling (board data creation). However, it is top 1.
This allocates a lot (top 1):
The loop is the problem: