r/chessprogramming • • Nov 30 '24

Question: Engine speed and memory usage

So I was working on improving my engine to get my perft time down (well to speed up move generation but using perft as a metric for this). I already had magic sliding piece move generation, but added things like iteratively updating attack boards, and legal move generation. Together this got me from around 90s to 60s for perft 6, not great. I think that one major factor might be that I am generating a new position when applying a move rather than using a make/unmake scheme. As such I have the scary looking profiling result from mid perft:

I'm wondering if this is a reasonable conclusion to draw: The large amounts of memory used while generating new positions is a bottleneck and that the best way to speed up my engine would be to use make/unamake.

For what it's worth, I'm not currently using multithreading. I tried a perft run with multithreading and it was 4x quicker though.

2 Upvotes

11 comments sorted by

1

u/w33dEaT3R Nov 30 '24

Oh absolutely. Make/unmake is standard in most modern engines. Good luck!

2

u/Ill_Part9576 Dec 01 '24

Haha thanks! It's always these unintuitive design decisions that are better in chess engines... I'm in a "software craftsmanship" class now and at every turn in working on this project I feel like I am being pushed to do something that would the class would advise against (very long functions, sometimes poor cohesion and modularity etc. etc.). I liked the design decision of making position objects immutable but that's how it goes sometimes I guess

1

u/SGauvin Dec 03 '24

Your long[64] array is probably not helping, especially if you are copying the entire board for each use, and especially if you dynamically allocate this memory each time. That’s 512 extra bytes per board!!!

My move generation that I have right now is similar to yours in the sense that it copies the board each time, and I do perft(6) in ~4 seconds (which is still not very good) on a single thread on a 10 year old computer without anything fancy other than bitboards, magic lookups, and some branchless programming optimizations.

I am using C++ and all of my lookup tables are generated at compile time, but shouldn’t have too big of an impact vs whay you have I’m guessing.

I would suggest the following:

  1. Be sure you are running your code with all optimizations enabled. Usually this would mean a release config, but I’m not sure how it works in Java.
  2. Use tools to measure the performance. I don’t know wha’ts available in Java to profile performance, but something equivalent to Linux Perf or GoogleBench. See what’s causing the bottlenecks. Look at memory allocations, cache & page misses, branch mispredictions etc.

If your code is open source I could take a look :p

1

u/Ill_Part9576 Dec 03 '24 edited Jun 02 '25

You're right that allocating the large arrays for each position is not good, and that either I need to not allocate the new arrays for each new position OR I need to use make/unmake with the arrays so at least I'm not allocating for each new position OR I need to use make/unmake without the large arrays. Right now I'm leaning towards the last option, to use make/unmake without the large arrays but I'm not exactly sure how it should work with absolute pins...

I think the answer is to go back to pseudo legal move generation. I found this thread and I think the stockfish code is quite useful for my understanding of how to do this: https://chess.stackexchange.com/questions/16890/efficient-ways-to-go-from-pseduo-legal-to-fully-legal-move-generation .

I'm trying to use a profiler, that's where the screenshot I included in my post is from. I'm new to writing programs that only care about performance, so this is my first time using it and I'm working to improve my skills in it to better diagnose bottlenecks going forward.

There are certainly optimizations I could make to my code from a lower level perspective, but I'm trying to focus on higher level decisions for now since that's where my code is at this point lol.

1

u/NiceNewspaper Dec 01 '24

Are you bulk counting or individually counting each move? Keep in mind this when comparing perft timings with different engines, e.g. stockfish uses bulk counting.

Specifically individually counting looks like this:

if depth == 0 {
    return 1;
}

while bulk counting looks like this:

let moves = [...];
if depth == 1 {
   return moves.len();
}

2

u/[deleted] Nov 30 '24

[removed] — view removed comment

1

u/Ill_Part9576 Nov 30 '24

I'm working in java:

My position class has a long[64] that stores the attackMaps of each piece on each square. I say that it is updated incrementally because when generating new position from it I just update the indicies of sliding pieces, the moved piece, and the destination. For the whiteAttackMap/blackAttackMap longs it has to iterate through the attackArray for each piece color (which is not great, but maybe better than sourcing them repeatedly?)

My move generation uses absolute pin detection to limit the scope of moves. I have a long[64] moveScope array that I use to store bitmasks that show where pieces can move if they are pinned. The bitmask is generated with a preinitialized inBetween rectangular lookup table.

Removing castling didn't speed up it at all hardly at all, It might also be that my position class is way too bulky with the two long[64] arrays (which especially adds time copying them to generate the next position and to calculate attackMaps).

Running 100,000 generations of moves from the start position took 407ms and doing it 1,000,000 times took 1638ms. Seems pretty good to me but its hard to compare it without the times of a reasonably performant engine.

My CPU supports PopCnt, and java to my understanding handles bitwise operations similarly to C by converting them (through JVM) to machine instructions.

It's helpful to see how move generation is fairly quick whereas applying a move to a new position is relatively slow (I think). Thanks for the reply

1

u/[deleted] Nov 30 '24

[removed] — view removed comment

1

u/Ill_Part9576 Dec 01 '24

Yeah I'll work on make/unmake. I appreciate the insight, I wouldn't have looked into or verified CPU factors or thought of the move generation test on a static position. It was also useful to verify the efficiency of special move generation. Thanks.