Visitar URL original
Jit and gil · Issue #362 · RustPython/RustPython · GitHub
The Wayback Machine - https://web.archive.org/web/20221228171251/https://github.com/RustPython/RustPython/issues/362
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

Jit and gil #362

Open
Yanpas opened this issue Feb 6, 2019 · 10 comments
Open

Jit and gil #362

Yanpas opened this issue Feb 6, 2019 · 10 comments
Labels
A-design About RustPython's own implementation

Comments

@Yanpas
Copy link

Yanpas commented Feb 6, 2019 •

One of the reasons why cpython is the slowest one is that it is interpreted. Do you have plans to support jit? And will you keep gil?

UPD:
As any regular user I'm not that interested what language an interpreter is written in. The whole point of switching to another interpreter (like pypy) is it's speed and/or concurrency (jython). So for the project to be successful I think it should have great compatibility with cpython and offer other benefits, even if it means that more code will be placed to unsafe blocks.

@vokar
Copy link
Contributor

vokar commented Feb 6, 2019

Speaking about JIT, there is already the existing issue: #300.

@cthulahoops
Copy link
Collaborator

cthulahoops commented Feb 6, 2019

On the subject of the GIL, ideally it would be nice to build a GIL-less python, but it's not something we've done any work on yet.

My expectation is that once we add the threading library Rust will start screaming that nothing is thread safe, and we'll have to add locks to make it run. I think we'll be aiming to add fine grained locks and see how it goes. The experience of CPython is that it's difficult to add more fine grained locking without breaking single core performance. Rust's safety checks might make this an easier task than in C, but that's just speculation at this stage.

And then there's the question of C extensions that expect to be able to acquire the GIL, so we may need an optional GIL for compatibility anyway.

In short, it's a very interesting can of worms without an easy answer.

@Yanpas
Copy link
Author

Yanpas commented Feb 6, 2019

As any regular user I'm not that interested what language an interpreter is written in. The whole point of switching to another interpreter (like pypy) is it's speed and/or concurrency (jython). So for the project to be successful I think it should have great compatibility with cpython and offer other benefits, even if it means that more code will be placed to unsafe blocks.

@Ironlenny
Copy link

Ironlenny commented Feb 6, 2019 •

@Yanpas In Python there's thread-level concurrency and there's process-level parallelism. It sounds like what you want is thread-level parallelism. The GIL is what prevents thread-level parallelism. As far as I know there are three strategies for thread-level parallelism in Python: 'Software Transactional Memory', subinterpreters, and OS thread backed Python threads.

Transactional Memory treats thread operations as atomic. Either a thread can successfully execute, or it is reset and tries again. In the case of memory access, each thread tries to perform a memory access. The first thread to complete the access moves on to the next stage in it's execution. The threads that cannot complete it reset to the beginning of the access attempt and try again. PyPy has experimented with Transactional Memory. Given the young nature of RustPython, I think some of the challenges PyPy has run into could be avoided. It is a promising avenue for retro-fitting thread-level parallelism into Python. Wikipedia on 'Software Transactional Memory'

There is also the subinterpreter PEP. This proposal exposes CPython's ability to spawn isolated interpreters in their own OS thread to the user through a module. Subinterpreters can be coupled with channels to implement 'goroutine' like structures (which are lock-free).

Finally OS thread back Python threads would require the interpreter to execute threads without the GIL.

Regardless of which approach is taken (if any), I think it would be beneficial to make the VM multi-threaded. A multi-threaded VM allows for things like multi-threaded GC. It also allows for runtime optimizations such as automatic parallel execution of loops. And the simple fact is multi-threaded processors are the norm, and to stay single-threaded is to let the bulk of the processor sit idle.

A first step might be replacing Rc with Arc

@Mec-iS
Copy link

Mec-iS commented Feb 14, 2019 •

I suppose the major possible improvement that Rust can produce will be based on its built-in concurrency. As far as I can understand, this project is in the early phase in which the first step to a better Python interpreter is to clone CPython standard library in Rust and keep them in sync as much as possible. After that it will be possible to unleash all the advantages that Rust can bring in terms of concurrency, safety and savings in maintenance time.

@jmfrank63
Copy link

jmfrank63 commented Feb 20, 2019

I am rather new to the subject, but of course a GIL less python that is capable of multi threading would be of a great advantage. The way CPython is implemented makes it almost impossible to remove the GIL any attempt let to a huge amount of extra logs distributed all over different parts of the implementation slowing everything massively down.
However there had been successful implementations like the implementation of IronPython. So I would first study successful approaches instead of reinventing the wheel.
Admittedly IronPython is almost dead and still is missing Python 3 support, but the way the implementation had been done in respect to multithreading might be a used as a valuable template.
https://wiki.python.org/moin/IronPython

@youknowone
Copy link
Member

youknowone commented Dec 6, 2020

basic jit with cranelift is recently added

@GoodiesHQ
Copy link

GoodiesHQ commented Mar 16, 2021

I am rather new to the subject, but of course a GIL less python that is capable of multi threading would be of a great advantage. The way CPython is implemented makes it almost impossible to remove the GIL any attempt let to a huge amount of extra logs distributed all over different parts of the implementation slowing everything massively down.
However there had been successful implementations like the implementation of IronPython. So I would first study successful approaches instead of reinventing the wheel.
Admittedly IronPython is almost dead and still is missing Python 3 support, but the way the implementation had been done in respect to multithreading might be a used as a valuable template.
https://wiki.python.org/moin/IronPython

Also, Cython is solidly mature at this point and creating functions that are C/Cython-only (any code that does not use the Python C API) can have the nogil attribute that will allow you to run parallelized code. Granted, you cannot use the Python API within that scope so python objects of any kind are off-limits... still worth a note, though! :)

@pca006132
Copy link
Contributor

pca006132 commented May 22, 2021

It seems that the current implementation uses OS thread for threading, but uses a lot of atomics for synchronization. I wonder if it is possible to have some kind of memory model that requires the user to explicitly synchronize the data, similar to using synchronize and volatile in Java. AFAIK, this is not defined for Python.

As mentioned in #2445 and #2474, atomic is causing serious performance issues, and the running time of simple programs increase linearly as the number of threads. We should try to minimize the synchronization needed for executing the code.

@youknowone
Copy link
Member

youknowone commented Oct 28, 2021

@youknowone youknowone added the A-design About RustPython's own implementation label Apr 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-design About RustPython's own implementation
Projects
None yet
Development

No branches or pull requests

9 participants