sandermvdb / chess22k

Chessengine written in Java
GNU General Public License v3.0
32 stars 8 forks source link

Prospective improvements in the C# port #12

Open respel opened 3 years ago

respel commented 3 years ago

Hey, it's me again. So, the C# port is live at https://github.com/respel/Chess22kDotNet. In one of our previous discussions, you mentioned some stuff that you would like to implement in the C# port. I'm quoting you here

These are some features I would like to implement which are currently not possible in Java: use a long array for the hashtable with more than 2^31 entries use struct items in the hashtable instead of 2 long values *add endgame tablebase support by calling external libraries

I'm interested in implementing them. Can you expand a bit more on exactly what hash tables were you talking about here?

Another thing I was thinking about was to make a library out of the common code and then make the engine use that. However, I guess that would render the engine much weaker, so wasn't totally sure whether to go forward with it.

sandermvdb commented 3 years ago

Hi,

With the hashtable I meant the transposition table. I can think of 2 possible improvements: 1) In Java the maximum length of an array is the maximum value of an integer, which is 2147483647. I use 2 long arrays for the transposition table which results in a maximum memory usage of 8 GB. Normally this is more than enough but under TCEC conditions (64 threads, long timecontrol) this is probably a bottleneck. It is hard to say how much but I guess 25 elo. 2) The transposition table consists of 2 long arrays, one containing the zobrist keys, the other the values (score, bestmove, depth, etc...). I think its much cleaner to store a struct of 128 bit containing all this data. I think there are also some minor performance improvements: no bit shifting necessary to create the value, no xorring necessary to validate that the key and value belong to each other and finally better cache usage because the key and value are adjacent to each other in memory.

Does this help? :)

respel commented 3 years ago

@sandermvdb Thanks for the reply, it did help a lot.

You are already using only one array for the transpositions table, so that's a non-issue 😄

Regarding the struct itself, I have implemented it, however, it's clocking in at 136 bits instead of 128.

namespace Chess22kDotNet.Search
{
    public struct TtEntry
    {
        private short _depth;
        public long Key { get; set; }
        public int Move { get; set; }
        public short Score { get; set; }
        public byte Flag { get; set; }

        public short Depth
        {
            set => _depth = (short)(value + TtUtil.HalfMoveCounter);
            get => (short)(_depth - TtUtil.HalfMoveCounter);
        }
    }
}
sandermvdb commented 3 years ago

You are indeed correct that the transposition table is already one array, I changed that some time ago :)

Regarding the 136 bits, you could use 16 bits for the Move and then reconstruct the missing bits (6 I believe). Or combine _depth or Move with Flag, since Flag only needs 2 bits.

respel commented 3 years ago

@sandermvdb Thanks for the tip. Merged move with flag.