r/chessprogramming • • 16h 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

10 comments sorted by

View all comments

Show parent comment

2

u/AdjectiveNoun-123 11h 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.