Python 3
Hi,
This looks very interesting! Is python3 support planned?
Thanks, Jeremy
On Fri, Jun 09, 2017 at 03:52:07AM -0700, Jeremy Herbert wrote:
This looks very interesting! Is python3 support planned?
Thanks.
In earlier revisions, I made the code Python3 compatible and ran some benchmarks. Unfortunately, those benchmarks showed that Python3 was measurably slower than Python2.
Some additional Python2 only code has likely been introduced since that time, but it should not be difficult to make the code support Python3 again. However, there does not appear to be a compelling reason to use the slower Python3.
-Kevin
Would you be happy for me to look into this further? Perhaps you tripped on a bug in python itself, because one would think that python3 should be better (not that I don't believe you of course). I find this host/real time slave paradigm very interesting; I've tried to do something like this in a custom CNC application before, but I unfortunately ran out of time. I'd like to use python3 due to some of the newer features (asyncio, etc) which I would like to use elsewhere, and of course because python2 only has bare minimum bugfix releases.
It might also be useful to set up Travis to protect against any regressions, I can try to give this a go too.
On Fri, Jun 09, 2017 at 07:12:48PM -0700, Jeremy Herbert wrote:
Would you be happy for me to look into this further? Perhaps you tripped on a bug in python itself, because one would think that python3 should be better (not that I don't believe you of course). I find this host/real time slave paradigm very interesting; I've tried to do something like this in a custom CNC application before, but I unfortunately ran out of time. I'd like to use python3 due to some of the newer features (asyncio, etc) which I would like to use elsewhere, and of course because python2 only has bare minimum bugfix releases.
It's not difficult to get Klipper running on python3 - I just did it as a quick test. (At least enough porting to make batch processing of gcode files work.) If you're curious, I pushed the changes to a new work-python3-20170610 branch.
Tests with python3 continue to show around a 10% slow down relative to python2. That's not a huge deal if there was something in python3 that was important to have.
-Kevin
Could something like Numba be used do JIT compiling of the code? You could then really speed up things offloading to a GPU.
https://numba.pydata.org/
What benchmarks are you using to compare Python2 & Python3?
That's not a huge deal if there was something in python3 that was important to have.
Support past 2020?
One of the significant efforts on the part of the Python developers for Python 3 is to improve performance. This is very surprising given http://speed.python.org numbers showing most things being faster in python 3, while getting even faster over time.
But premature optimisation is the root of all evil according to Knuth, so likely a more significant improvement could be made simply by optimising the worst performing codepaths than fiddling around with different interpreters. The entire thing is moot anyway, since one could simply go use PyPy.
Perhaps you tripped on a bug in python itself, because one would think that python3 should be better (not that I don't believe you of course).
Python3 being slower in a number of places is a very well-known issue (among people who care about performance) with tons of microbenchmarks to support the case. "Better" != "faster", at least as far as the core devs are concerned apparently. The consensus view seems to be that since hw performance has increased more than interpreter performance has decreased, everything is alright. Without looking at the code, one would assume that in this specific case the issue is integer arithmetic and the way int is implemented in py3.
But premature optimisation is the root of all evil according to Knuth
Knuth is the root of half the performance problems in computing with the other half being abject ignorance and on deeper examination those turn out to be isometric. Knuth's utility at this point rests in teaching beginners algorithmic complexity concepts and no more. The model he uses in his work bears no relation to real hardware. This is why real engineers profile/bench or use more modern (albeit still woefully inadequate) models like RAM if they have to use a model for whatever reason (top tier journals and conferences seem to like them).
The entire thing is moot anyway, since one could simply go use PyPy
The major use case targeted by klipper is running on a pi or pi-equivalent together with octoprint. Pypy needs x86(-64) afaik. The same is true of most other jits and isometric domain-specific equivalents.
and of course because python2 only has bare minimum bugfix releases.
Py2 is a stable and mature codebase, it doesn't need anything more than that. I don't think anyone would argue against python3 having some sexy features, not all of which are backported. I'm a big fan of asyncio which you cite for example, as nearly anyone who has had experience with large codebases using twisted would do, but that's neither here nor there for klipper's use case. Unless Kevin can identify a python3 feature that would be explicitly beneficial, saddling klipper with the burden of maintaining compatibility and the performance hit in exchange for 0 benefit would seem to be unwise.
@KevinOConnor Since we're discussing performance (and I happened to glance at the reactor code as a result of this discussion), you don't seem to try to use epoll when it's available unless I missed something.
Pypy needs x86(-64) afaik. The same is true of most other jits
Not true at all.
From PyPy's website: We provide binaries for x86, ARM, PPC and s390x running on different operating systems such as Linux, Mac OS X and Windows.
Numba is available (on debian) on amd64, arm64, i86, mips, mips64el, mipsel, powerpc, ppc64, pp64le and s390x.
saddling klipper with the burden of maintaining compatibility
Sticking to Python 2 is saddling it with technical debt on a language that will not be maintained. https://pythonclock.org/
On Mon, Sep 11, 2017 at 01:28:34AM +0000, Paul G wrote:
@KevinOConnor Since we're discussing performance (and I happened to glance at the reactor code as a result of this discussion), you don't seem to try to use epoll when it's available unless I missed something.
The epoll system call does not improve performance when only a small number of file descriptors are in use (as is done in the klippy host code). And, it does not allow registering an fd that is a local file (which I use extensively in debugging to process gcode files in "batch mode"). So, for this use case, regular poll() is sufficient and more flexible.
-Kevin
On Mon, Sep 11, 2017 at 01:06:28AM +0000, Paul G wrote:
The entire thing is moot anyway, since one could simply go use PyPy The major use case targeted by klipper is running on a pi or pi-equivalent together with octoprint. Pypy needs x86(-64) afaik. The same is true of most other jits and isometric domain-specific equivalents.
Pypy does work on the RPi, and my initial plans with the project was to use pypy. Unfortunately, the JIT warmup time with pypy caused scheduling problems that made the project unusable. Ultimately, to make the system work, I had to port the latency and performance sensitive parts of the host software to C code (serialqueue.c and stepcompress.c). Once that was done, there was no point in using pypy as regular python was sufficient and more stable.
Separately, FWIW, I don't understand this discussion with python3 vs python2. As I demonstrated in the work-python3-20170610 branch it is a ~20 line patch to convert Klipper to python3 instead of python2. So, for all practical purposes, the code is python3 ready. If there is a reason to use python3 then I'll use it. Until that time (2020 or other) I'll continue to use the best tool available.
Cheers, -Kevin
From PyPy's website: We provide binaries for x86, ARM, PPC and s390x running on different operating systems such as Linux, Mac OS X and Windows.
The same website states: "“JIT Compiler” version These binaries include a Just-in-Time compiler. They only work on x86 CPUs that have the SSE2 instruction set (most of them do, nowadays), or on x86-64 CPUs."
I see references to ARMv6 and ARMv7 jit support in the docs, so perhaps the text on the download page should be read as them merely not providing binaries.
Sticking to Python 2 is saddling it with technical debt on a language that will not be maintained.
As Kevin stated, the patch to enable py3 support is small and can be used if needed. As far as not being maintained, so what? What exactly are you proposing will break as a result? I use a version of tcc in a project that hasn't been touched in nearly a decade and it still works.
I don't understand this discussion with python3 vs python2. If there is a reason to use python3 then I'll use it. Until that time (2020 or other) I'll continue to use the best tool available.
It's the "new shiny thing" syndrome. It's everywhere. It's how we ended up with code editors written in javascript running in electron (essentially embedded chromium) and emulating vi with popular plugins and weighing in at 1gb ram. Shiiiny.
As an aside, is there a mailing list or something similar? I'm curious to find out your view on how klipper compares with the likes of Sailfish and what needs to be done to advance it to production-ready status in your view for example, which isn't a good fit for an issue.
On Sun, Sep 10, 2017 at 08:57:47PM -0700, Paul G wrote:
As an aside, is there a mailing list or something similar?
I hope to have a mailing list setup in the near future. There is a reprap forum at: http://forums.reprap.org/read.php?147,667655 , but I don't think it gets much traffic.
-Kevin
Do you have a protocol or procedure for benchmarking? I'm just curious as to profiling it.
On Thu, Sep 14, 2017 at 03:51:02PM +0000, Jed Frey wrote:
Do you have a protocol or procedure for benchmarking? I'm just curious as to profiling it.
Yes. To benchmark the host software, I process a large g-code file with the host software in a "batch mode" and time the results - see docs/Debugging.md for further info. Something like:
time ~/klippy-env/bin/python ./klippy/klippy.py config/example.cfg -i test.gcode -o /dev/null -d out/klipper.dict
For benchmarking the micro-controller software, I connect to the mcu via the console.py script (also described in docs/Debugging.md) and send it a series of queue_step commands with a large number of steps. I re-run this test while decreasing the interval between steps until I find the lowest interval the micro-controller can step at without reporting a timing error.
-Kevin
Python 3.7 brought some nifty speed improvements. And python 2's sunset is scheduled for the end of next year. It may be time to redo this benchmark.
i would rather see python code replaced with C++
i would rather see python code replaced with C++
The Core functions why need good speed are written in C. In other places they is no point replace Python with C/C++.
but it couldn't hurt either, not that big a python fan :p , always a hassle.
Pretty sure for 99% people is C++ more a hassle then python. We got already so many extra modules created be different people, I don't think it would be so many if Klipper would be written in C++