Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/meooow25/recopt
https://github.com/meooow25/recopt
Last synced: 26 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/meooow25/recopt
- Owner: meooow25
- License: mit
- Created: 2022-05-10T22:33:36.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-05-15T06:39:50.000Z (over 2 years ago)
- Last Synced: 2023-04-03T21:42:20.270Z (almost 2 years ago)
- Language: Python
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# recopt
Optimize recursion in [PyPy](https://www.pypy.org/download.html)
---
```py
@recopt.optrec('fact') # <--- this
def run_fact(n, mod):
def fact(n):
if n == 0:
return 1
r = fact(n-1)
return n * r % mod
return fact(n)
```**What is this?**
Recursion in PyPy is very slow (why? I don't know). This function transforms the [AST](https://docs.python.org/3/library/ast.html) of a recursive function to remove the recursive calls while preserving the behavior.**How does it work?**
A function call is about executing a function and resuming where we left off once the called function is done. This is achieved here by splitting the body of a recursive function into multiple functions, such that resuming means executing another function. The functions to resume are maintained on a stack.There are some restrictions on what is supported.
* The recursive function must have positional arguments only.
* Recursive call statements must be simple calls `f()` or assignments `x = f()`.
* The only control flow statements supported are `if`, `while`, `for`.**How fast is it?**
See [`bench_fact.py`](bench_fact.py) and [`bench_dfs.py`](bench_dfs.py).**Tell me more about how to use it**
Don't.