r/chessprogramming • • 2d ago

Exact move sequence

How would you efficiently index 8 billion chess games by exact move sequence?

I'm interested in developing an open-source chess database using the publicly available Lichess game archives.

Existing chess opening explorers generally index board positions rather than exact move sequences.

I would like to build a system capable of:

- Storing billions of complete chess games.

- Searching exact move-sequence prefixes.

- Calculating the frequency and outcomes of each sequence.

- Finding the longest matching prefix between historical games.

- Identifying the first half-move at which a sequence becomes unique.

- Incrementally incorporating millions of newly played games.

I've considered prefix trees (tries), compressed tries and database indexing, but I'm relatively new to programming and would appreciate advice from experienced developers.

What data structures, programming languages, database systems and storage technologies would you recommend for a project of this scale?

Would it be feasible to process and index the entire dataset using consumer-grade hardware?

The source data is freely available at https://database.lichess.org/.

I'm particularly interested in developing a small working prototype before attempting to scale it.

0 Upvotes

12 comments sorted by

View all comments

2

u/eraoul 2d ago

A trie is the right answer with a caveat.

Additionally, chess moves can be represented pretty compactly. Each row or column has 8 values so 3 bits each should suffice: x1,y1.x2,y2 is then 3x4 =12 bits. And you need another 2 bits to encode which of 4 pieces you choose in a promotion (N,B,R,Q). So that’s 14 bits to store each move. Be sure to use a 16-bit word to store instead of something bigger if possible.

For the trie, it’s great in the start of the game but once the game becomes unique it wastes space with pointers. So you could try a packed radix trie instead: when the suffix collapses to a single game you store the whole remaining sequence in an array to save space.

Yeah this should work fine on a laptop.

For outcome data you can compute it at each node and store that extra statistic in the node.

2

u/AdjectiveNoun-123 2d ago

This is a pretty good answer but I'll ask the follow question for OP. Once you have this data what do you plan to do with this? I

If it's just for your personal recreation you can get away with a very compact mixed data structure with a trie for the first several half-moves and then a hashmap to arrays for when the games are basically unique. As a trivial optimization you can get this down from 16 bits per half-move to 9 bits with some computational overhead. List all possible half-moves in some sorted order, you'll never have more than 512 legal half-moves from a position so 9 bits should suffice (16 queens with 8 squares across 4 axis). You can probably get than down to 8 bits if you verify there's not a reachable position from the starting point that leads to more than 256 possible half moves. Lookup will be slower but still on the order of microseconds even without much optimization.

If storage is not a concern and you want to minimize game lookup time and maximize parallel searches. Have N copies of the trie for the first k half-moves. For these position also include an extra field in the trie that is a key in a hashmap where you have an array of all the transposition so you can go forward and backward from a specific position more effectively. You can also have more metadata on which way the game progresses based on how it arrived at that transposition . Then after that have a simple trie without those extra fields for transposition until the games become unique and then finally have a hasmap of arrays of the remaining game moves.

For even more lookup optimization, you can play around with the values of N and k depending on how much parallel access you need but be careful with concurrency issues when you update across all N tries. Finally although there might be several hundred legal half-moves from any position, the vast majority of played moves will cluster around a few options. If you sort by most frequently played and fetch 64 bytes of highly compressed data at a time, you can rapidly speed up lookup by unpacking snapshots of the trie into the most common lines and having that sit in L2 cache.

1

u/badde_jimme 2d ago

For move representation, you could have a well specified move generator make a list and a single byte could represent a move. Apparently there is proof that there can be at most 218 moves from any chess position.

1

u/eraoul 2d ago

That's a nice idea. I think I still wouldn't do it due to the computational cost of generating moves for every position in the DB, but it is essentially a one-time cost so it's feasible if you're trying to squeeze space for sure! 8 bits is definitely better than 14, no argument there on the 43% size reduction, but I still prefer the simpler explicit representation.

1

u/badde_jimme 1d ago

Okay, what about this:

  • Each side has 16 pieces at most, so four bits are used to specify which piece moves. I believe it's best for each piece to retain it's own id for the length of the game.
  • The moves available to the selected piece assume an empty board except pawns can capture and kings can castle. This allows us to precompute a list of moves, and each piece except for queens will have less than 16 moves, allowing the move to fit within 1 byte.
  • For queen moves, we need an extra bit. I suggest consolidating these extra bits and putting them before the moves proper. Each byte should have 7 bits used for queen moves and 1 bit indicating whether it is the last byte being used in this way. This should be easy to skip over and the game can be read without excessive bit fiddling.

There is another point I should make, here is a PGN:

[Event "F/S Return Match"]
[Site "Belgrade, Serbia JUG"]
[Date "1992.11.04"]
[Round "29"]
[White "Fischer, Robert J."]
[Black "Spassky, Boris V."]
[Result "1/2-1/2"]

1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 8. c3
O-O 9. h3 Nb8 10. d4 Nbd7 11. c4 c6 12. cxb5 axb5 13. Nc3 Bb7 14. Bg5 b4 15.
Nb1 h6 16. Bh4 c5 17. dxe5 Nxe4 18. Bxe7 Qxe7 19. exd6 Qf6 20. Nbd2 Nxd6 21.
Nc4 Nxc4 22. Bxc4 Nb6 23. Ne5 Rae8 24. Bxf7+ Rxf7 25. Nxf7 Rxe1+ 26. Qxe1 Kxf7
27. Qe3 Qg5 28. Qxg5 hxg5 29. b3 Ke6 30. a3 Kd6 31. axb4 cxb4 32. Ra5 Nd5 33.
f3 Bc8 34. Kf2 Bf5 35. Ra7 g6 36. Ra6+ Kc5 37. Ke1 Nf4 38. g3 Nxh3 39. Kd2 Kb5
40. Rd6 Kc5 41. Ra6 Nf2 42. g4 Bd3 43. Re6 1/2-1/2

A significant portion of the data is metadata, especially considering we can make the game itself 4-5 times smaller. I'm not sure what the plan is for that, OP would probably like to include it.

1

u/eraoul 21h ago

cool idea. It does sound cheaper to compute, at the cost of the extra annoyance of tracking the unique piece IDs. Not bad though.

Still overall I doubt that the extra space savings are that big a deal, but depends on the OP's exact use case for sure. And yeah I was assuming they were discarding all metadata aside from the game result.