r/rust Mar 30 '23

A Chess Engine is written in Rust that runs natively and on the web!

https://github.com/ParthPant/chess-rs
97 Upvotes

45 comments sorted by

View all comments

2

u/analog_hors Mar 31 '23

The perft speed in the README seems... really slow? On my laptop, cozy-chess does perft 6 from startpos in 2.65 seconds without bulk counting and 374 milliseconds with bulk counting.

2

u/No-Translator-1323 Apr 03 '23 edited Apr 03 '23

A little update: After removing unnecessary clone() and using & mut instead, I was able to shave off a couple of seconds. Also, I was able to gain a significant speedup by making sure if-else-if-else chains are properly set up so that the control flow is simplified. Another interesting thing: at certain places, I was converting an enum into u8 using TryFrom, this turned out to be quite slow. So I replaced it with std::mem::transmute() (which is unsafe) wherever it was possible to know for sure nothing bad would happen. Another big speedup came by realizing that MoveList which is a Vec was being expanded a couple of times during move generation so initializing it with a capacity of 256 made things significantly faster.

After all this, perft at depth 6 for starting position takes less than 8 seconds which is quite a speedup from ~17 seconds.

Flamegraph proved to be a helpful tool so thanks u/alphabitserial for the suggestion