niklasf/python-chess

BinaryFen in python-chess

Torom opened this issue · 4 comments

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.

indeed, would be real nice to have.

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

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.