I'm a big advocate of copy/make without need undo. But there are good arguments for either method. copy state is very fast. With undo, you STILL have to maintain "undo" state as well as additional logic for the make and of course unmake becomes another routine that takes time to execute so it's certainly not as clean and simple. Is it faster either way? I don't really know for sure, but in Komodo I once measured that making a move barely registers on the profile. I assume that making and then unmaking the moves would also be very cheap if I were doing it that way.OneTrickPony wrote:So I am completely new to chess programming and just trying to wrap my head around why basic things are done in the way they are done.
Take backtracking for example. There are two ways:
a)use one board representation struct/object and when visiting a node make a move on it but when going back take the move back to reuse for sibling node;
b)allocate new board representation struct/object for every new node visited; it isn't memory expensive because every time we go back stack pointer takes care of deallocating boards we created (we use N boards instead of 1 where N is length of the path from root to leaf)
(we allocate new memory once and then memcpy from original for every child visited);
Cost of a) is taking moves back. Cost of b) is cost of allocating stuff on stack + memcpy of arrays.
My question is if anybody tried b). Is it obviously slower ?
I tried it for N queens problem for fun and it was a fail (finding all solutions for 15x15 board took 20045ms for standard backtracking and 34061ms for one with allocating new boards) but in N queens problems taking moves backs is trivial while it's not so trivial in chess.
Thoughts ?
(EDIT: after compiling with /Ox the times were 9658ms and 25282ms so even worse)
One never knows for sure because everything impacts everything else in some way. So the speed of any given routine does not tell the entire story and neither does a profile tool.
Bottom line, I prefer the simplicity of copy/make. By the way, I did an experiment once where I artificially increased the size of the board state to see how it impacted things and I had to make the state a LOT bigger for it to have a noticeable slowdown effect on the program. Still, probably due to superstition more than anything else, I am careful to keep it relatively small.
Don

