5 ms·
> If it needs to be fast for CPython, it has to be written in C, but if it needs to be fast for a JIT, you cannot use C. He showed a simple mysum() function th
by chrisseaton 10y ago
> If it needs to be fast for CPython, it has to be written in C, but if it needs to be fast for a JIT, you cannot use C. He showed a simple mysum() function that totaled up the elements in an iterable. If it is passed a Python object like list(range(N)), the JIT knows what it is and can do lots of optimizations. But if it is passed a NumPy array, which is "opaque C stuff", the JIT doesn't understand it, so it will have trouble even achieving the performance of a non-NumPy version on a JIT-less Python.
There's a third way here - run the C code using the same JIT as the Python code, instead of compiling it natively.
That might sound mind-bending, but C is just a language like any other. It's actually fairly simple and consistent to implement compared to something like Python. You can interpret and JIT compile it if you want to - there's no major magic to that.
Then you can optimise the C code and the Python code at the same time, inline the two, do the same optimisations as you do on Python code, etc. We're using this technique to run Ruby C extensions in JRuby and the results so far are great - running real C extensions faster than native code because we can optimise both the Ruby and the C at the same time.
http://chrisseaton.com/rubytruffle/cext/ http://chrisseaton.com/rubytruffle/cext/
- ricardobeat 10y agoAt that point what do you gain versus porting all the C stuff to the host language so it can be JIT compiled?
- chrisseaton 10y agoI'm not sure what you mean - you gain not having to port anything, which sounds pretty great to me. And for the Ruby C extensions we tried not only did we not have to port them from C to Ruby, they actually ran faster than either the compiled C version or the pure Ruby version because we can inline and optimise between the two languages.
- ricardobeat 10y agoI mean the cost of porting vs writing a whole new C interpreter - sounds like a lot of work.
- chrisseaton 10y agoThis way it's a lot of work for one small team to write the C interpreter, compared to a still quite a lot of work for every team with a C extension that would need to port it to Python.
- johncolanduoni 10y agoWriting a C interpreter isn't that bad, especially considering you can co-opt Clang's frontend and use it to do all the syntax and semantic analysis.
- njharman 10y agoThere's a reason new languages, compilers are made rather than everyone just porting their code to the Assembler of new CPU. Same reason applies here.
- rocqua 10y agoIn the case of python, we already have Cython that allows the porting of Python to C. Why do work creating the inverse (with potentially much harder semantics to JIT) when we can already go from Python to C.
- jamiesonbecker 10y agoAgreed! JIT's are interesting but we use Cython in production to build Userify Enterprise (SSH key management) (self-hosted distributable). It works incredibly well and we are thrilled with the result. (We do require a bunch of third party pip installs rather than wrap them into the binary.) This was partly because one thing that we had trouble with was lots of imports, especially for (ironically) compiled C libraries. It's a lot easier and probably safer to just give a user a script of installs (or a requires manifest.) Someday soon we'll probably even make it installable via pip and bring things full circle. (We're exploring a free five server version.) Userify's architecture is probably a bit unusual. It's distributed as a single binary that uses an external redis server for locking and stores data in either S3 or a local filesystem (NFS, EBS, iSCSI, etc.) When it first launches, it automatically installs any missing python prereqs (if pip is available) and explodes out a web server static file repo in case you want to front-end it with nginx (etc) and then starts up multiple web servers (HTTPS, HTTP, alternative ports, etc). We've found Python to work extremely well for all of this. We've looked at a few other languages that we think fit our core requirements (incl lua, scala, and haskell) but Python works pretty much perfectly for this use case, we can scale it horizontally using Redis as a synchronization mechanism, and the GIL hasn't caused any problems for us because we just add more processes as needed. Cython is really fantastic and 'just works'. Things aren't perfect.. we're still on Python 2.x, but we're going to be moving forward as soon as all of the third-party libraries we rely on move to 3.
- wlievens 10y agoExtensibility from C isn't there for people to accelerate their slow code, it's much rather there to cooperate with existing libraries, and C is the lowest common denominator there.
- dikaiosune 10y agoIs there anything blocking Jython from using this same approach?
- PeCaN 10y agoJython isn't (yet) implemented on Truffle, I believe (and would require basically a rewrite). The approach chrisseaton describes is generally only feasible if you're implementing a Futamura projection à la PyPy or Truffle. That said, it's only a matter of time before a high-performance Python pops up on Truffle and JITs extensions like RubyTruffle.
- gsnedders 10y agoThere is however ZipPy which is a Py3 implementation built on Truffle, though obviously not directly comparable to the Py2 Jython.
- JonathonW 10y agoJIT-compiling your C code covers one of the two reasons a Python programmer might write C code (dropping down to C to do some mathematical operation faster). How do you handle the other reason people write C code-- to bridge Python code to an external native library? In that case, you'd be JIT-compiling the bridge itself, but your native library remains "opaque [insert language here] stuff" that the JIT can't do anything with.
- chrisseaton 10y agoEither you can interpret the external native library as well, if you have the source code, or you can leave the calls to that library as normal native calls generated by the JIT. So yes, that'll be an optimisation barrier, but it's the same barrier as you would already have if you were using an interpreter for Python making native calls to the same library. So it won't be any slower than our current best case. (In fact the JIT can do some clever things to make the native call more efficient than in general, as it can do things like schedule values to be in the correct registers from the start rather than copying them in place just for the call, but that's probably not a significant source of inefficiency anyway).
- __s 10y agoWhat of JITing x86 assembly into x86?
- johncolanduoni 10y agoThis is basically how QEMU works when it's not using KVM
- chrisseaton 10y agoYes that's another option. We already do that for inline assembly in C programs.
- weberc2 10y agoIf you're using C for interop and not performance, it doesn't matter (much) if the JIT can optimize for it or not. You'll still be no worse off than you are today with CPython + C extensions.
- vmorgulis 10y agoVery interesting point of view. I see a fourth option: - compile the scripting language to C (like Crystal for Ruby)
- duaneb 10y agoSure, but then you lose any advantage using a scripting language gives you.
- natermer 10y agoThat and you lose the advantage of what JIT compiling can get you.
- deleted 10y ago[deleted]
- brettcannon 10y agoPython already has this through Cython: http://cython.org/ http://cython.org/
- ufo 10y agoThe biggest gains of JIT is that you can use knowledge gained at runtime to generate specialized code and skip lots of the runtime checks that are typical of dynamic languages. You can't get rid of these checks if you compile the source language to C so from this point of view compiling to C is going to be just as slow as writing a traditional interpreter in C.
- vmorgulis 10y agoYes but you can bootstrap the compiler.
- ufo 10y agoThe implicit runtime checks in a Python program get converted into explicit if-statements if you use an ahead-of-time compiler to compile it to a static language like C. Bootstrapping will not be able to magically get rid of these if statements. Not to mention, bootstrapping is about the compiler executable and the problem I was talking about applies to any attempt to use an ahead-of-time compiler for a dynamic language.
- makmanalp 10y agoOne thing is that the "C stuff" can actually be, like, fortran stuff, because of LAPACK and BLAS and what have you, depending on the implementation your numpy is using.
- chrisseaton 10y agoNo problem - let's interpret FORTAN too. What we actually do is interpret LLVM IR, so if you can compile your language to that we can interpret it.
- DanWaterworth 10y agoThis is more difficult than you might think. LLVM IR more like a family of closely related, but poorly-specified languages, than a language of its own. There is an interpreter in the LLVM codebase, but this is essentially only possible because it is part of the codebase and so tracks the rest of the project very closely.
- cygx 10y agoThis is more difficult than you might think. Note that chris is not talking in the abstract, but about an existing codebase: https://github.com/graalvm/sulong https://github.com/graalvm/sulong
- DanWaterworth 10y agoOK, that's impressive.
- dagss 10y agoOne of the main BLAS implementations (where lots of numerical computation spends 99% of its time) is OpenBLAs, hand-crafted assembly per CPU. Main competitor is Intel MKL, closed source. Even if you could interpret that code, your JIT-ing is not going to create exactly the same assembly and will thus perform worse. You can very easily get a C implementation that you can interpret easily but at least as output by a C compiler, that performance is nowhere near where you want to be. (I realize this is not a problem though, just have the JIT call the libraries, as long as the wrapper can be interpreted you are good.)
- stuaxo 10y agoI think CFFI is supposed to be able to link between Pypy and C code with minimal code (same should be possible with other JIT Pythons).