7 ms·
The other major issue is backwards compatibility. Ruby and Python have large API surfaces on both the C API and scripting language sides, with many libraries m
by nostrademons 12y ago
The other major issue is backwards compatibility. Ruby and Python have large API surfaces on both the C API and scripting language sides, with many libraries making use of obscure language features. If any single one of those features doesn't work, the library won't. Witness how tough the Python3 migration has been.
I talked with the Unladen Swallow guys sometime after the project was canceled. They said that the main issue was not getting Python to run fast on LLVM, where they said they had some really encouraging results. Instead, it was:
1. Bugs in LLVM which they had to spend a lot of engineering effort working around. This is probably much less of an issue now, with Clang, XCode, Julia, and Rust all being used in production with a good deal of success.
2. Compatibility with the C API. This was considered a "non-negotiable" for Unladen Swallow's design, and one major reason they started by forking CPython rather than building from scratch on LLVM. The problem is, the C API makes a lot of assumptions about how objects are laid out in memory, how they are memory-managed, how they are accessed, etc. If you must support defining & calling methods from C, there's a lot less freedom to, say, stick a trampoline in the method preamble or a PIC at the call site or switch to using a tagged representation of pointers and ints. That means that the VM has to fall back on the interpreter (and possibly waste cycles converting & marshalling data) for a lot of things that are supposed to be fast, which kills many of the real-world performance gains.
V8 had the luxury of defining its own C interface. I've often wondered whether a Python that gives up C extension compatibility could get similar speed gains - I even ran the idea by a former Python core team member who worked on my team at Google, and he said there was no theoretical reason why not. But then, Python without C extension compatibility would be Python without Numpy, Scipy, scikit-learn, PIL/Pillow, JSON, StringIO, and many other critical packages. At that point you might as well start over with a new language.
- moe 12y agoPython without C extension compatibility would be Python without Numpy, Scipy, scikit-learn, PIL/Pillow, JSON, StringIO Would we really miss them if Python was then fast enough to do these things natively?
- aardvark179 12y agoThe code backing things like Numpy and Scipy is not small, has been well optimised over many years, and would be extremely difficult to reimplement at speed even with a faster Python. The freedom high level languages give tends to prevent many low level optimisations that are really useful for big maths and data routines. For the language I work on we were able to write macros and functions to allow our C libraries to continue to work when we changed our VM, but we've spent considerable effort optimising things so the C has to reach back into objects as little as possible, and we could only do this because recompilation and refactoring was okay.
- ris 12y ago"The code backing things like Numpy and Scipy is not small, has been well optimised over many years, and would be extremely difficult to reimplement at speed even with a faster Python." I assume you're unaware of the numpypy project then, which is doing exactly that, with reasonable results. I also think you overestimate the performance of numpy/scipy when compared to, for instance, C++ template-based libraries which are able to make large performance gains from compiling specializations for the specific array shapes being used. JITs can potentially go further, being able to compile specializations in places where array shapes are not known at compile-time.
- gaius 12y agoThey are rewriting BLAS in Python? Seriously? Links please. You may not be aware that NumPy does it's heavy lifting by offloading the work to C and FORTRAN, and that's where the speed comes from. You set up your data in Python, use the nice high level language to parse files, etc, then you pass chunks of memory containing matrices off to the FORTRAN to crunch, then use Python again to say write it back to a file.
- masklinn 12y ago> They are rewriting BLAS in Python? Nah. Note that these are optional even in the original numpy.
- coldtea 12y agoWell, JSON would take all of an afternoon to write in pure Python or in the new C extension interface. PIL a little more, still quite fast to port though (a month or so). No reason why Numpy and Scipy couldn't be written for a new C extension interface too, though that would take much more time (1-2 years). Thing is, with 10-20 million dollars, Google could have sponsored all of the above.
- nostrademons 12y agoAn afternoon to write, and then a year or more to work out all the bugs & corner-cases, write the documentation, and convince everyone to switch. Writing the code is rarely the hard part. There's a whole ecosystem around libraries and languages that takes time to build.
- coldtea 12y ago>An afternoon to write, and then a year or more to work out all the bugs & corner-cases, write the documentation, and convince everyone to switch. For a JSON parser? I think you overestimate the difficulty in implementing one. If anything, a parser for such a format is one of the easiest things to know you got correct -- there are extensive test suites. As for switching to it, Python users already use several JSON libs anyway, so it's not like adopting another would be any great deal. For the other libs, like numpy etc, yes, it would be more effort, but nothing unsurmountable. And speed is a far greater hook to get users to adopt that version of Python than "we now got unicode all over and fixed a couple more annoyances".
- RubyPinch 12y ago> so it's not like adopting another would be any great deal. Isn't there a pure-python json parser in the standard lib already?
- unimpressive 12y agoI don't know if it's pure python but. import json Does in fact give you a working JSON parser. I wasn't aware there was a different one to use.
- Freaky 12y agoSpeaking of C APIs, JRuby+Truffle is going all-out: > Our new solution in JRuby+Truffle is pretty radical - we're going to interpret the C source code of your extension. We use the same high performance language implementation framework, Truffle, and dynamic compiler Graal, to implement C in the same way as we have implemented Ruby. http://www.chrisseaton.com/rubytruffle/cext/ http://www.chrisseaton.com/rubytruffle/cext/
- omaranto 12y agoAnd it seems that even though their C implementation is not as fast as compiled code from, say, GCC would be, the mix of Ruby and C can run faster than using compiled C extensions from another Ruby implementation! This is due to cross-boundary optmization opportunities. Cool stuff!
- btown 12y agoI wonder if a Python JIT could do the same thing... Has anyone thought about this? It seems that since Dropbox's Pyston uses LLVM, it would be a perfect fit. https://github.com/dropbox/pyston https://github.com/dropbox/pyston
- simcop2387 12y agoThat would be pypy largely. From what I've seen it's got the biggest backing of the community and tends to be really fast computer to other interpreters.
- grandalf 12y agoIronically, Python w/o c extensions is what you get to use on Google App Engine and it works quite nicely for many purposes.
- dragonwriter 12y ago> Ironically, Python w/o c extensions is what you get to use on Google App Engine and it works quite nicely for many purposes. That is not quite correct. While you can't add your own C-extensions, some popular extensions that aren't pure Python are included in the provided platform (including, particularly, numpy.)
- stefanix 12y agoSame difference. If you only supporting a few you have the option to redesign the interface (e.g: use effi).
- warbiscuit 12y agoRegarding Python C extension compatibility ... A number of projects (e.g. this bcrypt package -- https://pypi.python.org/pypi/bcrypt https://pypi.python.org/pypi/bcrypt) have switched from using CPython's API for C extensions, and switched to the cffi package (https://pypi.python.org/pypi/cffi https://pypi.python.org/pypi/cffi), giving them what seems to be a much cleaner (and less CPython-specific) break between the C-level and the Python-level code. I say "seems to be" because I'm not too familiar with the internals to know for sure :) What cffi did give them is that bcrypt (with it's C component) can be used by both CPython and PyPy -- even though PyPy doesn't support any of CPython's native C API. Meaning that PyPy just has to support cffi, and is otherwise freed from any fixed C interface itself. I think the reason cffi gives it this freedom is that cffi provides Python-side metadata about the C datastructures being worked with through cffi, allowing the PyPy JIT to efficiently integrate that information into it's compilation process.
- yoklov 12y agoMy guess is that this would still be a bottleneck in practice. LuaJit has a similar feature, and Mike Pall has said several times that if you care about performance, you're almost certainly better off using 100% Lua, because calling into C is slow. Though, it seems much easier to work around for the implementation than the existing Python C API.
- justincormack 12y agoWith LuaJIT calling C from the ffi is fast (from jitted code), just the traditional Lua C interface is slow. Lua code can be faster as it can optimise through the boundary of course.
- gcb0 12y agoyou wouldn't need json, string io, numpy etc done in C if Python was fast... so the snake is eating its own tail?
- fa 12y agoCan this logic be applied to another environment that has had a ton of money poured into it? Specifically, .NET. Can you replace Ruby/Python in the question with C# and if so, why doesn't the latter benefit from money and experienced developers?
- noblethrasher 12y agoYou can spend money on performance or on features. Google has historically been good at high performance computing, and chose to spend enough money to get and fund a team of experts on building high performance VMs. Microsoft has historically been good at adding backwards compatible feature sets, and chose to spend enough money to get and fund a team of experts on tasteful design and evolution of programming languages.
- pekk 12y agoThe Python 3 migration hasn't been tough, it was just subjected to a political campaign. That kind of risk can only be controlled by never changing anything.