python-chess icon indicating copy to clipboard operation
python-chess copied to clipboard

BinaryFen in python-chess

Open Torom opened this issue 1 year ago • 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.

Torom avatar Jul 22 '24 18:07 Torom

indeed, would be real nice to have.

vondele avatar Aug 18 '24 11:08 vondele

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

vondele avatar Aug 22 '24 17:08 vondele

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

Torom avatar Aug 28 '24 21:08 Torom

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.

niklasf avatar Aug 29 '24 13:08 niklasf