Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust or Cython to speed up core components #722

Closed
jnewb1 opened this issue Jan 1, 2021 · 12 comments
Closed

Rust or Cython to speed up core components #722

jnewb1 opened this issue Jan 1, 2021 · 12 comments

Comments

@jnewb1
Copy link

jnewb1 commented Jan 1, 2021

Has any thought or development gone into using cython for this library, especially the core components which could benefit from a speedup (move generation and validation)? I think that might be a good way to speed up the library and any chess engines that use this library.

I tried just compiling just the core components using cythonize, but main issue right now appears to be lack of support for Generic and TypeVar from cython's end. (see cython/cython#2753)

I think if we get a basic cython build running and show the performance delta, that might be a good start (I'm interested in working on that, I was curious if anyone else had done something similar yet).

@niklasf
Copy link
Owner

niklasf commented Jan 1, 2021

I previously rejected ideas diverging from pure Python to retain more flexibility, but now that we're on version 1.x it might be time to reconsider.

Other similar ideas were to use C/C++/Rust for some performance critical paths.

@jnewb1
Copy link
Author

jnewb1 commented Jan 1, 2021

Awesome! Maybe I'll try to get a cython build of the move generation working to see what kind of performance increases we could get. I do agree that pure python is much cleaner, but the speed improvements could be substantial, A small C/C++ library would also do nicely, or perhaps we could use an existing chess library and just add an option to use that library rather than the python core components. If anyone else is interested in this or has other ideas, please leave them here.

@jkterry1
Copy link
Contributor

jkterry1 commented Jan 2, 2021

Wouldn't C/C++/Rust make more sense than Cython?

@jnewb1
Copy link
Author

jnewb1 commented Jan 2, 2021

Well the idea with cython is that most of your python code remains the same, but yea I think a C/C++ helper library would also make sense

@sudochop
Copy link

sudochop commented Jan 5, 2021

+1 If others are like me, this is a huge want as I use this library for processing large pgn files and my only solution left for speedups is renting beefier vms.

@niklasf niklasf changed the title Cython for core components Rust or Cython to speed up core components Jan 25, 2021
@niklasf
Copy link
Owner

niklasf commented Sep 13, 2021

Potentially usefu for this: A C++ translation of the core module: https://github.com/pjpuzzler/cpp-chess (thanks for sharing @pjpuzzler!)

@pawngrubber
Copy link

@niklasf I don't know enough about this repository to know where the bottlenecks are, but I know enough about Cython to take a swing at writing a PR. Can you point me in the right direction for identifying bottlenecks? For now I'm mostly interested in faster PGN reading and move generation (for example speeding up perft). Also, if I want to write any tests for my PR where should I start?

@niklasf
Copy link
Owner

niklasf commented Mar 9, 2022

@pawngrubber Maybe profiling python examples/perft/perft.py examples/perft/random.perft could show some points of interest. Other than that I don't really know what to do, except trial and error.

python test.py is a fairly comprehensive test suite, so we might not need much more (for existing functionality).

@tissatussa
Copy link

lately i stumbled upon "Python C extensions" at https://book.pythontips.com/en/latest/python_c_extension.html .. 3 methods are described to directly use C code in Python, without compiling the C code into a binary.

@tissatussa
Copy link

i guess, when writing a perft function with python-chess, the speed is most important .. my intuition tells me C is better than Python anyway, so why not leave such task to C and "embed" it.

@niklasf niklasf mentioned this issue May 23, 2022
@luki139
Copy link

luki139 commented Oct 20, 2022

I've been developing a Rust port (almost 1:1) of this library some time ago: https://github.com/lukaszpancer/RustChess
It implements only standard chess functionality, pgn parsing, syzygy endgame, and some basic Engine communication.
Right now i'm working on making this a python library with pyo3 (which is going quite well). And this python library is approximately 10x faster.

It might be a bit messy but it might help someone. I hope that authors don't mind.

@qwertyquerty
Copy link

Update on this?

@jnewb1 jnewb1 closed this as not planned Won't fix, can't repro, duplicate, stale May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants