r/chessprogramming • u/pbcgw • 23h 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.
4
u/joeyrobert 21h ago
A trie
1
u/ChessViper 22h ago
You could build the tree like you described which works for small datasets, and is how my app ChessViper handles <100k games.
Large databases (e.g. Lumbra's gigabase, 12M) are generally pre-processed, stored in a database, and use some more complicated techniques. You can check out SCID or en-croissant which are open source and handle large datasets pretty well.
Also... I don't understand the exact move sequence requirement. Why wouldn't you want to handle transpositions?
2
u/eraoul 21h 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.
1
u/Glittering_Sail_3609 19h ago
What you basically want is to build one giant opening book. You could store positions on a SSD inside polyglot binary format: (https://hgm.nubati.net/book_format.html).
It is a binary format in which each entry has fixed size. If you make sure the opening book file is sorted by position key, you can find position you are looking up pretty quickly by using simple binary search in O(log n) time.
1
u/Free-Ingenuity-6859 19h ago
Look at the Lichess source code for how they store their multi billion game database that’s served on their site no need to reinvent the wheel
Would it be possible to create and index the entire data set? Without bigger hardware unlikely you’ll run into major issues with storage as I think the full DB is a few terabytes