niklasf / python-chess

A chess library for Python, with move generation and validation, PGN parsing and writing, Polyglot opening book reading, Gaviota tablebase probing, Syzygy tablebase probing, and UCI/XBoard engine communication
https://python-chess.readthedocs.io/en/latest/
GNU General Public License v3.0
2.45k stars 531 forks source link

BinaryFen in python-chess #1099

Open Torom opened 4 months ago

Torom commented 4 months ago

You introduced the efficient BinaryFen in this blog post on Lichess.

My question is whether it would be possible to implement this in python-chess. I guess python-chess is used by quite a few people to store a lot of positions (at least I do). For this it would be very helpful to be able to use the space-saving and reversible BinaryFen.

Thank you for this awesome library.

vondele commented 3 months ago

indeed, would be real nice to have.

vondele commented 3 months ago

see also https://github.com/Disservin/chess-library/pull/109

Torom commented 2 months ago

Here is my implementation in Python:

https://github.com/Torom/BinaryFen_python/blob/main/BinaryFen.py

It is definitely not perfect, pretty much a 1:1 conversion of Disservin's implementation. So it also has no adaptations to variants. And the size savings in Python also seem to be rather small:

>>> sys.getsizeof(bytearray(24))
81
>>> sys.getsizeof(chess.Board().epd())
93

Edit: Okay apparently it was unnecessary to return bytearray. It gets better with bytes:

>>> sys.getsizeof(bytes(24))
57
niklasf commented 2 months ago

I'd imagine that this will be most useful when serializing to disk, so despite the relatively large memory usage as per sys.getsizeof(bytes(24)), it's really the 24 that makes it interesting.