Slashdot Mirror


Project Aims For 5x Increase In Python Performance

cocoanaut writes "A new project launched by Google's Python engineers could make the popular programming language five times faster. The project, which is called Unladen Swallow, seeks to replace the Python interpreter's virtual machine with a new just-in-time (JIT) compilation engine that is built on LLVM. The first milestone release, which was announced at PyCon, already offers a 15-25% performance increase over the standard CPython implementation. The source code is available from the Google Code web site."

35 of 234 comments (clear)

  1. Speed ups for EVE online, perhaps? by KnightElite · · Score: 5, Insightful

    I hope this translates into further speed ups for EVE online down the road.

    1. Re:Speed ups for EVE online, perhaps? by idlemachine · · Score: 5, Informative

      I believe EVE uses Stackless Python. I'm not sure how well these improvements would translate across.

  2. Re:Unladen Swallow by Rip+Dick · · Score: 3, Funny

    What if Oprah's ass got 5x's smaller?

  3. Kill the GIL! by GlobalEcho · · Score: 5, Informative

    The summary misses one of the best bits -- the project will try to get rid of the Global Interpreter Lock that interferes so much with multithreading.

    Also, it's based on v2.6, which they are hoping will make 3.x an easy change.

    1. Re:Kill the GIL! by eosp · · Score: 3, Interesting

      The summary misses one of the best bits -- the project will try to get rid of the Global Interpreter Lock that interferes so much with multithreading.

      Good luck with that. The last time someone tried that, they slowed Python down by half.

    2. Re:Kill the GIL! by dgatwood · · Score: 4, Insightful

      The key is to find the right balance of granularity in locking. A big giant mutex is always a bad idea, but having tens of thousands of little mutexes can also be bad due to footprint bloat and the extra time needed to lock all those locks. The right balance is usually somewhere in the middle. Each lock should have a moderate level of contention---not too little contention or else you're wasting too much time in locking and unlocking the mutex relative to the time spent doing the task---not too much contention or else you're likely wasting time waiting for somebody else that is doing something that wouldn't really have interfered with what you're doing at all. Oh, and reader-writer locks for shared resources can be a real win, too, in some cases.

      --

      Check out my sci-fi/humor trilogy at PatriotsBooks.

    3. Re:Kill the GIL! by Just+Some+Guy · · Score: 4, Insightful

      Good luck with that. The last time someone tried that, they slowed Python down by half.

      Yes, good luck with that! Because the current implementation slows it down by 7/8ths on my 8-core server.

      --
      Dewey, what part of this looks like authorities should be involved?
    4. Re:Kill the GIL! by Red+Alastor · · Score: 4, Informative

      Good luck with that. The last time someone tried that, they slowed Python down by half.

      Only because Python uses a refcounting garbage collector. When you get many threads, you need to lock all your data structures because otherwise you might collect them when they are still reachable. This project plans to change the garbage collection strategy first. Once it's done, killing the GIL is easy.

      --
      Slashdot anagrams to "Sad Sloth"
    5. Re:Kill the GIL! by Nevyn · · Score: 3, Informative

      That's funny, because os.fork() etc. work fine on my version of python.

      --
      ustr: Managed string API with ave. 44% overhead over strdup(), for 0-20B
    6. Re:Kill the GIL! by Nevyn · · Score: 4, Interesting

      Then you probably want to read: Patrick Logan on why SMT isn't "awesomez".

      --
      ustr: Managed string API with ave. 44% overhead over strdup(), for 0-20B
    7. Re:Kill the GIL! by jd · · Score: 4, Insightful

      If developers were working from a clean-slate and didn't have the problems of excessive legacy code to work with, I suspect Digital Mars' D, Inmos' Occam and Erikkson's Erlang would be the three main languages in use today.

      If hardware developers were working from a clean-slate, you'd probably also see a lot more use of Content Addressable Memory, Processor-In-Memory and Transputer/iWarp-style "as easy as LEGO" CPUs.

      Sadly, what isn't patented was invented 30 years too late and 20 years before the technology existed to make these ideas really work, so we're stuck with neolithic monoliths in both the software and hardware departments.

      (Remember, Y2K was worth tens of billions, but wasn't worth enough to get people to stop using COBOL, and that was practically dead. To get people to kick their current habits would need a kick in the mind a thousand times bigger.)

      --
      It's a small world and it smells funny; I'd buy another if it wasn't for the money; Take back what I paid (SoM)
  4. How fast is five times faster really? by LingNoi · · Score: 5, Funny

    They say five times faster however it really depends on if they're talking about a European or African Python Interpreter.

    1. Re:How fast is five times faster really? by ArsonSmith · · Score: 4, Funny

      Java spokes person: "5x faster? We already do that."

      Java spokes person to other java people: "(whisper)Hehe, I told them we already do that. Hehe."

      --
      Paying taxes to buy civilization is like paying a hooker to buy love.
    2. Re:How fast is five times faster really? by rackserverdeals · · Score: 3, Informative

      I know you're trying to be funny but... If you're talking plain Java vs Python, Java looks to be quite a bit faster. You don't have to look hard to find benchmarks that show java is faster.

      Jython seems to be about 2-3 times faster than CPython according to those test.

      This could give CPython the performance edge over Jython, but it still has a way to go to catch up to Java.

      --
      Dual Opteron < $600
    3. Re:How fast is five times faster really? by meringuoid · · Score: 5, Funny

      Joking aside, though, I find this target to be overambitious. Speeding up by a factor of three would be plausible; two would be OK, but I'd hope they'd keep working on it to get it up to three. Four strikes me as unlikely, and five is right out.

      --
      Real Daleks don't climb stairs - they level the building.
    4. Re:How fast is five times faster really? by rackserverdeals · · Score: 3, Informative

      How is Java faster? If it's a trivial program, than it just doesn't matter. Actually, if it's a trivial program, for your own use, a Pythoneer will write the script and run the interpret (no compile!) before you can fire up Eclipse and type "private static void".

      You know you can write trivial java programs without using an IDE such as Eclipse. I started out in the late 90's writing Servlets in vi and notepad. The time it takes to compile is meaningless. You only need to do it once. You don't have to recompile every time you run the application.

      If we are talking about a non trivial program, then algorithms, data structures, caching, micro-optimization (like re-writing bits in C) and profiling can improve things by many many orders of magnitude. Too bad if the code has so many layers and adapters that any real change will be prohibitively expensive.

      Or they could use any of the many java libraries available so they don't have to write those parts of the code. Since they've been around for years, they've already been optimized.

      The productivity gains of writing fewer lines of code seems stupid to me. Programmers aren't secretaries. I can't type maybe 90wpm but a few lines of code might take an hour to get right. It doesn't matter what the language is.

      --
      Dual Opteron < $600
    5. Re:How fast is five times faster really? by bnenning · · Score: 3, Insightful

      If you're organization has 50 Java developers, the effort needed to train them to be Python developers is not trivial. Then you can't just rewrite everything because you still have all that Java code to maintain.

      Yes, you shouldn't rewrite working Java code in Python just for kicks, or vice versa. I'm not sure how that's relevant.

      It's not like Python is significantly less lines of codes than Java or anything. Especially now with annotations. Maybe 2x as many LOC

      I'll agree that 2x is in the ballpark, and I find that to be quite significant, considering that studies have found that developers tend to produce lines of (debugged, working) code at the same rate regardless of language. Doubling developer productivity will very often be worth sacrificing performance, especially when the software isn't CPU-bound. Why do you think Java took over from C?

      Plus, I don't think fewer LOC means greater maintainability.

      All I can say is that I've been developing in Java for 12 years and Python for 2, and that's been my experience.

      Let me give an example using a pizza recipe intead of a programming language.

      I don't agree with that, because the short version leaves out critical information so of course it's not as useful. What I like about Python is that it largely lets me deal with *only* the stuff that matters to my application. In my questionable metaphor Python would be "Bake at 400 degrees for 15 minutes", and Java would be "Turn the temperature dial to 400, open the oven door, insert the pan in the oven, close the oven door, wait 15 minutes, open the oven door...". Ok not quite that bad, but the essential details are often obscured by unimportant boilerplate. And yes, you can get tools that automatically create and hide some of it, but that should just make you question why the language can't do that itself.

      The main problem I see though. In 5 years, a lot of those Python developers are probably going to be working in a different language all together.

      A fine argument for COBOL :)

      --
      How to solve most of our problems: 1.Lots of nuclear plants. 2.Cure aging.
    6. Re:How fast is five times faster really? by AigariusDebian · · Score: 4, Funny

      The difference is more like between:


      Prepare the bread.
      Put the sauce on the bread.
      Put the cheese on the sauce on the bread.
      Bake.

      And:

      define PizzaDoughFactory : AbstractDoughFactory{
              sub PizzaDoughFactory( PizzaDoughFactory cls, Integer thickness ){
                      cls.AbstractDoughFactory( thickness )
              }

              sub Sauce ( PizzaDoughFactory cls, Topping top){
                      cls.toppings = org.coolpace.JavaSmart.List( -1 )
                      cls.toppings.appendToTop( top )
              }
      }

      define PizzaCreator : AbstractApplication {
              def main( Integer argc, String *argv ){
                      new pizza = PizzaFactory()
                      pizza.set_dough = PizzaDoughFactory()
                      sauce = SauceFactory()
                      cheese = CheeseFactory()
                      pizza.dough.Sauce( sauce )
                      pizza.dough.Sauce( cheese ) // historically all toppings are called sauces as well
                      new ready_pizza = PizzaBakery( pizza )
              }
      }

  5. This is a very interesting project by Max+Romantschuk · · Score: 5, Interesting

    I read about what they intend to do, and they seem to have quite a few interesting ideas... But there are also major drawbacks:

    - No Windows support (apparently a Linux-only VM in the plans)
    - No Python 3.0 support

    And thus no guarantees most of the work will merge back into CPython.

    But competition is good, I can't really see a problem with having an alternative faster Python runtime, even if it's not as compatible as CPython. :)

    --
    .: Max Romantschuk :: http://max.romantschuk.fi/
    1. Re:This is a very interesting project by MightyYar · · Score: 4, Informative

      Psyco is x86 only and uses a lot of memory. It also requires additional coding... you have to actively use it, so you don't automatically get the speedup that a faster interpreter gets you. You also have to pick-and-choose what you want to get compiled with Psyco - the extra overhead isn't always worth it.

      To be fair, I don't know what the memory requirements of this new project are.

      --
      W..w..W - Willy Waterloo washes Warren Wiggins who is washing Waldo Woo.
    2. Re:This is a very interesting project by maxume · · Score: 4, Informative

      It might be easy to port over to 3.0, but not because it is using 2.6. Basically, they are planning on ripping out a big chunk of the internals of 2.6 and replacing it with a LLVM based system. To the extent that those internals changed for 3.0 (there wasn't necessarily effort put into making them compatible across 2.6 and 3.0...), the code would need to be updated for 3.0. The python level portability between 2.6 and 3.0 isn't a huge factor for something like this.

      They are targeting 2.6 because that is what made sense for Google (who is paying for the work). Or so they say:

      http://code.google.com/p/unladen-swallow/wiki/FAQ

      --
      Nerd rage is the funniest rage.
    3. Re:This is a very interesting project by Tumbleweed · · Score: 4, Funny

      I'm not quite sure what benefits this gives that Psyco doesn't already.

      It doesn't get as stabby.

    4. Re:This is a very interesting project by MightyYar · · Score: 4, Informative

      I think it's only Linux-only right now, because the developers currently use Linux. But they consider loss of Windows support a "risk", not a design goal:

      Windows support: CPython currently has good Windows support, and we'll have to maintain that in order for our patches to be merged into mainline. Since none of the Unladen Swallow engineers have any/much Windows experience or even Windows machines, keeping Windows support at an acceptable level may slow down our forward progress or force us to disable some performance-beneficial code on Windows. Community contributions may be able to help with this.

      --
      W..w..W - Willy Waterloo washes Warren Wiggins who is washing Waldo Woo.
  6. Re:Unladen Swallow by davester666 · · Score: 5, Funny

    It would still be huge! :-)

    --
    Sleep your way to a whiter smile...date a dentist!
  7. slowed it down by half? by Anonymous Coward · · Score: 5, Funny

    0.5x slower is like 2x faster, right? Reciprocals?

  8. Re:No windows by Anonymous Coward · · Score: 4, Informative

    Quite to the contrary, the FreeBSD guys have been building with clang+llvm for a while now, and they seem to like it. The kernel boots, init inits, filesystems mount, the shell runs.

    What other platforms, Darwin? Apple employs the largest number of LLVM developers. Windows? Both MinGW and Visual Studio based builds are tested for each release.

    It's still not as portable as the python interpreter, but that will come if and when developers who are interested in working on it start to contribute.

  9. Re:It's probably pining for the fiords. by Abcd1234 · · Score: 4, Informative

    Not really. Parrot is a much higher-level VM, providing things like closures, multiple dispatch, garbage collection, infrastructure to support multiple object models, and so forth, whereas LLVM really models a basic RISC instruction set with an infinite number of write-only registers.

    In fact, it would make a fair bit of sense to actually use LLVM as the JIT-compiling backend for Parrot...

  10. Binspam by Thelasko · · Score: 5, Funny

    I get emails claiming to increase my python's performance all of the time, I just delete them.

    --
    One of our competitors trademarked the term "hypothesis". From now on, we will call them "boneheaded ideas".
  11. Re:Too many levels of translation? by Abcd1234 · · Score: 4, Informative

    Wouldn't a more direct compile yield a better result?

    No, it wouldn't.

    The entire point of LLVM is that it provides an easy-to-target machine (it's basically a RISC instruction set) that you can use as your intermediate representation (the p-code you described). You then use the LLVM backends to compile the IR down to machine code. And because of the way the IR is structured (for example, it has write-only registers, which makes certain classes of optimizations much easier), you can do a really good job of optimizing.

    Basically, you "direct compile" to the LLVM IR, and then let LLVM take care of the details of generating the machine code. This gives you better abstraction (no more machine-specific code generation in Python itself), portability (to whatever LLVM targets), and you get all the sophisticated optimization that LLVM provides for free. That's a huge potential win.

  12. It all depends by mkcmkc · · Score: 5, Insightful

    I find Python is about 20x slower (and about 10x faster to implement) than C, with the number varying quite a bit depending on how CPU-bound the code is. Given the speed of modern processors, this is plenty fast for many tasks.

    Beyond that, many Python programmers employ a strategy of writing just the CPU-intensive inner loops in C or C++. This gives you most of the speed of an all-compiled solution but with much of the easier programming (and shorter programs) of the all-Python approach.

    My particular scientific application runs on 1500 cores, is about 75% Python/25% C++, is 4-5x smaller than similar all-C/C++ programs, and runs at about 95-99.99% of the speed of an all C++ solution.

    (Somewhat ironically, some of the worst performance bottlenecks in this app had to do with the overhead of some of the STL containers, which I ended up having to replace with C-style arrays, etc. to get best performance.)

    Not all apps will fall out this way, but you definitely can't assume that just because something's written in Python that it will be slow.

    (Going beyond that, we all know that better algorithms usually trump all of this anyway. If writing in Python gives you the time and clarity to be able to use an O(n)-better algorithm, that may pay off in itself.)

    --
    "Not an actor, but he plays one on TV."
    1. Re:It all depends by mkcmkc · · Score: 3, Informative

      I smell bullshit. There is no overhead from using STL containers.

      If you used an std::vector, you couldn't have a bottleneck, for the simple reason that the std::vector is an array.

      That was my impression, too, but careful timing and profiling suggested otherwise.

      In addition, we can by simple reasoning determine that there's gotta be some overhead involved with vector implementations. First, vectors know their size; in particular, they know it in constant time. This means that they essentially must include a size field and update it whenever size changes. Also, I can have a pointer to a vector, and that vector can grow arbitrarily without invalidating the pointer. That means that there pretty much has to be an indirect pointer to the vector's storage. It also means that the vector's storage must more or less be coming from a heap, which definitely slows things down. ("more or less" because one can imagine certain optimizations that might be possible if you somehow knew an upper bound on the vector's lifetime size)

      All of this stuff costs you in time and space.

      Suppose I have a function I'm going to call a million times and it needs a temporary array of ints, of a size I can bound (maybe even small enough to be cache-beneficial). I can allocate that array in the parent function and pass in a pointer each time. Overhead to create and destroy the array in the inner function each time: zero. If you do this with a vector, the implementation has to zero the length, which costs time. Or you can delete and recreate it by letting it go out of scope, but that also costs time.

      Most of the time these minor effects don't matter, but if it's in the innermost loop and is going to run billions of times, it can be quite noticeable.

      It could conceivably be that gcc's implementation of STL is a little slow. Doesn't matter why, though, because that's my target, and that's where my program has to run.

      It's been a while since I went through this exercise, so I don't have the exact scenario. But the code is GPL'ed and available here. If you can replace any of the arrays with an as-simple, as-fast use of vectors, I'd be happy to have it.

      --
      "Not an actor, but he plays one on TV."
  13. Re:every project starts that way by mkcmkc · · Score: 3, Insightful

    You're not CPU bound until you: add all the features, handle the special cases, add the error checking, scale up beyond trivial test data, etc.

    Then what? Rewrite?

    Yes. If you didn't know all of that was going to happen, you're prototyping. If you're prototyping, you should be doing it in a prototyping language.

    Rewriting from Python to C++ is not particularly difficult. Completely overhauling the design of a project written entirely in C++ is really unpleasant and takes a long time. So much so that many early design decisions on large C++ projects simply cannot be undone.

    Model in clay first, then in stone later if you have to.

    --
    "Not an actor, but he plays one on TV."
  14. Effort in wrong place by Animats · · Score: 4, Informative

    This is disappointing. Shed Skin has shown speed improvements of 2 to 220x over CPython. Going for 5x over CPython is lame. But Shed Skin is a tiny effort, and needs help.

    PyPy got a lot of press, but they tried to do an optimizing compiler with "agile programming" and "sprints", and, at six years on with substantial funding, it's still not done.

    The fundamental problem with running Python fast is its gratuitous dynamism. In CPython, almost everything is late-bound, and most of the time goes into name lookups. This makes it easy to treat everything as dynamic. You can store into the local variables of a function from outside the function, for example. In order to make Python go fast, the compiler has to be able to detect the 99.99% of the time when that isn't happening and generate pre-bound code accordingly.

    Dynamic typing requires similar handling. Most variables never change type. Recognizing int and float variables that will never contain anything else creates a significant speedup. In CPython, all numbers are "boxed", stored in an object structure. This is general but slow.

    CPython is nice and simple, but slow. Serious speedup requires global analysis of the program to detect the hard cases and generate fast code for the easy ones. Shed Skin actually does this, but has to place some limitations on the language to do it. If someone did everything right, Python could probably achieve the speed of C++.

    There's also the problem that if you want to be compatible with existing C modules for CPython, you're stuck with CPython's overly general internal representation.

  15. Re:Too many levels of translation? by MtHuurne · · Score: 3, Informative

    The Python object files are just a more convenient way to store the program compared to text files. No information is lost or glue is added in that first step.

    LLVM is, like its name suggests, really low level. You should think of it as a kind of portable assembly. It's much closer to actual hardware architectures than for example Java byte code. I don't expect much overhead from the LLVM to native step. A while ago I ran some tests with C++ compiled by GCC directly to native and compiled by GCC to LLVM byte code and then by LLVM to native; sometimes one approach was faster and sometimes the other, but they were pretty close.

    So that leaves the glue added in the Python object to LLVM step. I expect this to have a significant overhead, but I don't see it becoming a smaller overhead by going directly to native. The advantage of using LLVM is that you only have to write this step once, instead of once for each architecture.

    With LLVM it is possible to compile parts of the interpreter to LLVM byte code in advance and then inline that into the program being JIT-compiled. That way, you can be sure that the JIT and the interpreter actually do the same thing. Apple did this for their OpenGL driver, there is a nice presentation (PDF) about it.

  16. Re:Did know it was that bad by daver00 · · Score: 3, Insightful

    The thing about Python is you are replacing every lost hour in runtime with a day gained in development time. That is the point of Python. Numpy (formerly scipy I think) is mostly written in C anyway and provides fast n-dimensional array objects for vector and matrix operations, there are really only a few bottlenecks for maths/science purposes. Generally anything that is going to take a seriously long amount of time you would be doing in C over anything else anyway, what Python is is a viable alternative to Matlab etc, and a damn sight less expensive!

    Where I study Engineering they teach Python for this very reason. It has a gentle syntax which appeals to engineers and scientists who often aren't bargaining to become coders, and it is so much cheaper than Matlab that any missing features are rendered a moot point.

    Seriously, sitting on the sidelines and saying "I'm not gonna use Python because it is slow" is silly, it is so damn easy to code in python that you would learn it in a weekend if you already have coding experience. And as I said before, any lost time running python scripts over other languages is made up ten time over at least in the ridiculously short development times that go with Python scripts. Yes, it really is THAT easy to do anything in Python, there is a reason people bug you to try it out. Just give it a weekend, Python deserves it!