Ras wrote: ↑Tue Mar 24, 2020 8:43 pm
I think the difference to C is that stack variables are not zero initialised in C. Of course it's going to be slow if you have some zeroing loop going on for 256 elements even if it turns out that you only have 20 moves, like in the initial position itself. So the question would be how to get uninitialised stack arrays in Rust.
Possibly. Rust requires that a variable/array is completely initialized before it's used. If you don't, it won't compile, so getting uninitialized arrays is probably impossible without using unsafe code.
Also, I've been running some more tests. I repeated each of them 5 times. Results matched in the order of being off by 1-10 milliseconds or so (a few moves per second).
Pool: Vector. Move list: Vector:
Peft 1: 20 (0 ms, inf moves/sec)
Peft 2: 400 (0 ms, inf moves/sec)
Peft 3: 8902 (0 ms, inf moves/sec)
Peft 4: 197281 (7 ms, 28183000 moves/sec)
Peft 5: 4865609 (169 ms, 28790585 moves/sec)
Peft 6: 119060324 (4856 ms, 24518188 moves/sec)
Peft 7: 3195901860 (113672 ms, 28115119 moves/sec)
Perft 7 runs at +/- 113.7 seconds.
====
Pool: Vector. Move list: Array:
Peft 1: 20 (0 ms, inf moves/sec)
Peft 2: 400 (0 ms, inf moves/sec)
Peft 3: 8902 (0 ms, inf moves/sec)
Peft 4: 197281 (7 ms, 28183000 moves/sec)
Peft 5: 4865609 (171 ms, 28453853 moves/sec)
Peft 6: 119060324 (4256 ms, 27974700 moves/sec)
Peft 7: 3195901860 (112512 ms, 28404986 moves/sec)
Perft 7 runs at +/- 112.5 seconds.
After many runs, perft 7 turned out to be about 1.0 to 1.3 seconds faster when the move list is an array instead of a vector. The difference is almost 300.000 moves per second.
====
Pool: Array. Movelist: Array.
Peft 1: 20 (0 ms, inf moves/sec)
Peft 2: 400 (0 ms, inf moves/sec)
Peft 3: 8902 (0 ms, inf moves/sec)
Peft 4: 197281 (7 ms, 28183000 moves/sec)
Peft 5: 4865609 (170 ms, 28621229 moves/sec)
Peft 6: 119060324 (4226 ms, 28173290 moves/sec)
Peft 7: 3195901860 (110367 ms, 28957042 moves/sec)
Perft 7 runs at 110.3 seconds.
This is when both the pool and the move list are an array. After numerous tests, it turns out that this is the fastest option, consistently. Compared with the vector solution, the difference in speed is about 840.000 moves/sec.
In time, it's a small difference. Even when doing short runs of a few seconds such as perft 6, it starts to be noticable with the fastest option being more than half a second faster than the slowest. (Perft 5 is particularly slow, for some reason. That one takes the biggest hit when using vectors.)
Note that the only difference between these options is indexing vectors using var[iindex] versus addressing arrays using var[index]. It can be concluded that indexing arrays is faster than indexing vectors in Rust. At least in the latest version 1.42. The difference is small, but it is consistently noticable and measurable.