Conversation
What is RPython? ---------------- "RPython is a restricted subset of Python2 that is amenable to static analysis" - rpython.readthedocs.io/en/latest/rpython.html No, you really shouldn't be writing programs in it. It's really designed for writing language interpreters. It is distributed as part of the pypy_ project. .. _pypy: http://pypy.org/ What is this? ------------- An implementation of `forest.py`, adapted to the RPython toolchain Changes needed: - Add `entry_point` (where the compiled binary starts) - Add `target` (where the rpython compiler starts) - Add compatibility `__name__ == __main__` (where python can start) - No use of the standard library, not even `sys`. Namedtuples have been replaced with tuples (this might be a rule breaker). - Reduced string formatting options, you basically have str/str concat, and C-style formatting (if you need to convert ints). - RPython has no implementation of `any` or `set`. - RPython does have generators, but they're not very complex. - Watch out for functions that return multiple types. e.g. avoid returning both lists and iterators, as they don't share a subclass (and thus are not allowed). It's probably more efficient in RPython to just use preallocated lists. How do I run it? ---------------- RPython programs are valid Python programs. You could just run this under a normal python interpreter. Since the language is simpler for an interpreter to reason with, you might get a boost in performance anyway. The tradeoff is that an RPython implementation is no longer an idiomatic Python implementation. I'm not even sure if "idiomatic RPython" is a phraise that should exist. How do I compile it? -------------------- You will need a copy of the RPython toolchain. It is implemented in python, so you don't need much more than to clone the pypy source. $ hg clone https://bitbucket.org/pypy/pypy ../pypy The rpython toolchain in in the `rpython` subdirectory. $ ../pypy/rpython/bin/rpython ./forest_r.py This will output `forest_r-c`. $ time ./forest_r-c 305 295 300
|
That's pretty cool. I'm impressed it runs this fast.
|
XXX: this doesn't seem to work The compiled program behaves differently to interpreting the source. - Use a dedicated class to represent the forest. We get nice names back. - Pull `has_stable_forest` into a function to make type inference easier. - fix a bug causing solve to enter an infinite loop
|
Hah, I got to try this now. Found The compiled version totally locks up, whereas just running it under pypy produces sensible results. |
|
I'm tempted to revert back to the tuple method, as it appears that classes are a bit screwy. At least I've fixed the logic when running under the interpreter. It's a shame there are no good socket libraries that are in rpython. I would have tried to have a bit of fun trying to write a webserver. Having to unpack complex generators into helper functions, and then it works is an interesting side note. Oh, also, we have to use the |
- Can't subclass object, no custom `__hash__` functions - Can't subclass tuple, it's a builtin and has other funnyness around `getuniqueclassdef`.
|
Nice. This is a pretty cool toy now. Actually about as fast as fortran. I think we can rename it to diff --git a/forest_r.py b/forest_r.py
index 7c7acc4..42f5c8c 100755
--- a/forest_r.py
+++ b/forest_r.py
@@ -40,6 +40,8 @@ def solve(forest):
forests = [forest]
while any(forests) and not has_stable_forest(forests):
forests = mutate(forests)
+ for f in forests:
+ print 'Mutation:', f
return [f for f in forests if is_stable(f)]
Adding it to the bench / test scripts would be trivial, but need to actually find a way to add rpython to the |
The `forest.rpy` can be compiled with extra optimisations.
cp forest.rpy forest-r.py
$RPYTHON --output rpyforrest -O3 forest-r.py
This will compile the forest rpython program at the -O3 level.
You can enable the JIT version. There is a small performance
improvement, but it's honestly not that much. Compilation times will
dramatically increase. As in, go-and-make-some-tea durations.
$RPYTHON --output rpyforest-jit -Ojit forest-r.py
|
You can add rpython to the build image via AUR with this patch to the diff --git a/Dockerfile b/Dockerfile
index 1f86971..5cb4e28 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -18,3 +18,10 @@ RUN pacman -Syu --noconfirm && pacman --noconfirm -S \
RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && locale-gen
ENV LANG=en_US.UTF_8
+
+RUN pacman -S --noconfirm sudo git fakeroot && \
+ useradd -ms /bin/bash ci -G wheel && \
+ echo "%wheel ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
+USER ci
+RUN cd && git clone https://aur.archlinux.org/rpython.git && \
+ cd rpython && makepkg -si --noconfirmMay need a few tweaks to the build scripts and |
|
Awesome. I'll see how the run there goes and merge it in. Btw, is the jit helping at all? I'm not seeing any difference locally. |
|
The jit is not active by default. Actually the toolchain is using the |
|
Hah, wow. Well, the results are already super solid. So is the point of de76b41 to eliminate that weird bug you're having primarily then? Seems to pass now at any rate, you good with me merging this now? |
|
de76b41 is so that you can run (python2) interpreters on I nabbed this from https://morepypy.blogspot.co.uk/2011/04/tutorial-part-2-adding-jit.html example5 https://bitbucket.org/brownan/pypy-tutorial/src/tip/example5.py?fileviewer=file-view-default I'm good with merging this. Not super happy with the Dockerfile ( |
|
Yeah, I think might probably a better idea. The aur pin package is also pretty outdated upon inspection. I'll have a go at cleaning it up later on. |
|
Thanks for the clarification. Not sure how much I'll goof around with rpython after this, but it's a pretty cool implementation to have :) |
|
I did a small amount of restructuring it in master now. Ended up taking out the JIT just to not the complexity of it detract from the main problem. But as you said, it didn't affect performance. There's a small paragraph now about it on the readme as well. |
What is RPython?
"RPython is a restricted subset of Python2 that is amenable to static
analysis" - rpython.readthedocs.io/en/latest/rpython.html
No, you really shouldn't be writing programs in it. It's really
designed for writing language interpreters.
It is distributed as part of the pypy_ project.
.. _pypy: http://pypy.org/
What is this?
An implementation of
forest.py, adapted to the RPython toolchainChanges needed:
entry_point(where the compiled binary starts)target(where the rpython compiler starts)__name__ == __main__(where python can start)sys. Namedtuples havebeen replaced with tuples (this might be a rule breaker).
concat, and C-style formatting (if you need to convert ints).
anyorset.e.g. avoid returning both lists and iterators, as they don't
share a subclass (and thus are not allowed). It's probably
more efficient in RPython to just use preallocated lists.
How do I run it?
RPython programs are valid Python programs. You could just run
this under a normal python interpreter. Since the language is
simpler for an interpreter to reason with, you might get a boost
in performance anyway. The tradeoff is that an RPython implementation
is no longer an idiomatic Python implementation.
I'm not even sure if "idiomatic RPython" is a phraise that should exist.
How do I compile it?
You will need a copy of the RPython toolchain. It is implemented
in python, so you don't need much more than to clone the pypy source.
The rpython toolchain in in the
rpythonsubdirectory.This will output
forest_r-c.PR Notes
We need to check that this doesn't further break any rules. Maybe a class based implementation would be the right thing to do here.
Benchmarks