I tried only some minimal cleanup of my toy chess game...
I have only replaced a while loop by a for loop, and executable size went from 126640 bytes to 130864 bytes. I would not be surprised by a few hundred bytes, but that much. I think I must be doing something wrong for this case, but have no idea currently
#[
proc walkKing(kk: KK; s: var KKS) =
var i: int
var kk = kk
while true:
kk.di = king_path[kk.si][i].pos
if kk.di == -1: break
kk.df = board[kk.di]
if kk.df == 0 or capture(kk):
s.add kk
inc i
]#
proc walkKing(kk: KK; s: var KKS) =
var kk = kk
for i in 0..7:
kk.di = king_path[kk.si][i].pos
if kk.di < 0: break
kk.df = board[kk.di]
if kk.df == 0 or capture(kk):
s.add kk
Also surprising is, that inserting a plain let statement, without really using it, saves 600 bytes:
proc initPawn(color: Color) =
let colorcol_idx = color.col_idx # funny, this saves a few hundred bytes in executable, gcc -O3
for src in PosRange:
var i = 0
for d in PawnDirsWhite:
pawn_path[color.col_idx][src][i].pos =
if pawn_move_is_valid(color, src, src + d * color.int): src + d * color.int else: -1
pawn_path[color.col_idx][src][i].nxt_dir_idx = i + 1 # not really needed
inc i
pawn_path[color.col_idx][src][i].pos = -1
All tested with Nim 0.13, gcc 5.3 with -d:release , which is -O3 for gcc.
https://github.com/StefanSalewski/nim-chess
[EDIT]
really no idea. In the C files there seems to be nearly no difference. Is executable size a random value?
The actual size difference is in .o file:
lt nimcache/engine.o nimcache126/engine.o -rw-r--r-- 1 stefan stefan 50728 Feb 4 10:24 nimcache/engine.o -rw-r--r-- 1 stefan stefan 44392 Feb 4 10:21 nimcache126/engine.o
I would understand that for the first for loop in a module the size may increase by a larger amount, because code for the iterator is added to the executable. But there are already many for loops in that module.
size board board126 text data bss dec hex filename 108856 728 409224 518808 7ea98 board 104632 728 409224 514584 7da18 board126 stefan@AMD64X2 ~/verti $ size nimcache/cairo_engine.o nimcache126/cairo_engine.o text data bss dec hex filename 29289 0 328 29617 73b1 nimcache/cairo_engine.o 25065 0 328 25393 6331 nimcache126/cairo_engine.o
I think text section is the actual code, so it was really a great guess of Mr OderWat. ( I append the 126 for the one without the "for i in 0..7" loop.)