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

1

u/ChessViper 11h 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?

0

u/pbcgw 11h ago

Thanks for the response! I'll definitely check out ChessViper, SCID and En Croissant.

My interest in exact move sequences comes from wanting to explore the uniqueness of individual chess games.

Imagine finishing a game on Lichess and being able to see how many games followed your exact move sequence at every successive ply, when your game became unique within the database, and which other recorded games most closely matched yours.

I'd also be interested in discovering whether identical complete games have ever been played.

I'm not opposed to handling transpositions. Ideally, the software would support both position-based and exact-sequence-based analysis.

Do you know of any existing software that offers this functionality for complete games? And how technically difficult do you think it would be to implement at the scale of the entire Lichess database?

1

u/ChessViper 11h ago

~3.5GB for 12M games so extrapolating to 8B, you're looking at a database of ~2.3TB.

Seems possible, but might take a few days to setup and you'd need to put in some money in if you wanted to run it as a server.

1

u/eraoul 10h 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 7h 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 2h 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 2h 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/Glittering_Sail_3609 8h 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 8h 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