2

Speeding up CPython

 1 year ago
source link: https://lwn.net/Articles/840248/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Speeding up CPython

Please consider subscribing to LWN

Subscriptions are the lifeblood of LWN.net. If you appreciate this content and would like to see more of it, your subscription will help to ensure that LWN continues to thrive. Please visit this page to join up and keep LWN on the net.

Python, at least in the CPython reference implementation, is not a particularly speedy language. That is not at all surprising to anyone who has used it—the language is optimized for understandability and development speed, instead. There have been lots of efforts over the years to speed up various parts of the interpreter, compiler, and virtual-machine bytecode execution, though no comprehensive overhaul has been merged into CPython. An interesting new proposal could perhaps change that, though it is unclear at this point if it will take off.

Mark Shannon posted a message about his ideas to the python-dev mailing list in late October. He noted that CPython is slow, "yet little is done to fix it"; he would like to change that. He has a four-phase plan to get there on the technical side, along with some ideas on how to fund the effort. Beyond that, he had some thoughts on what was different from earlier efforts:

I am aware that there have been several promised speed ups in the past that have failed. You might wonder why this is different.

Here are three reasons:

  1. I already have working code for the first stage.
  2. I'm not promising a silver bullet. I recognize that this is a substantial amount of work and needs funding.
  3. I have extensive experience in VM [virtual machine] implementation, not to mention a PhD in the subject.

He pointed those interested to a GitHub repository where he had placed a few documents describing the plan. The overall idea is for each phase to increase CPython performance by 50%, which results in a 5x speedup at the end of all four phases. The intent is for these improvements to apply widely: "Performance should improve for all code, not just loop-heavy and numeric code." He described a tiered execution model that would separate code into tiers based on how frequently it runs; each tier would be handled differently:

All programs have parts that are executed very often and other parts that are executed infrequently or not at all. It makes sense to treat these different parts differently. For parts of the code that are never executed, or executed just once, it makes sense to spend the minimum amount of effort loading that code, even if it slows down execution a bit. For code that is executed frequently, it makes sense to streamline its execution at much as possible, even if that slows down loading considerably and costs more of other resources, such as memory.

It is, of course, impossible to know in general which parts of a program will be "hot", that is run very frequently, and which parts will be "cold", that is [never] run or run only once. Not only that, but there is not simple distinction between hot and cold, but a range. Much code is "warm", that is run reasonably often, but considerably less than the hottest part of the program.

To address this range of runtime characteristics, several tiers of execution should be employed. We will number these 0 to 3. A minimum viable implementation would consist of tiers 0 and 1. Tier 2 can then be added later for improved performance. Tier 3 can then be added for better performance of longer running programs.

The funding plan is rather creative and different, but also seems to be drawing some scrutiny—and perhaps controversy. Each phase would require roughly $500,000; half of that would be used to pay for the development and the other half would be held by the organization that is coordinating the funding for use in the maintenance of CPython. He suggests the Python Software Foundation (PSF) as the obvious choice for that role.

In order to reduce the risks of a contractor for a specific phase being unable to deliver the specified performance increase, payment would only be made on delivery. For the first phase, Shannon has working code, so he is essentially suggesting that the PSF raise the funds to engage him as the contractor for that phase. But that approach raised the eyebrows of Steven D'Aprano:

I don't mean to be negative, or hostile, but this sounds like you are saying "I have a patch for Python that will make it 1.5 times faster, but you will never see it unless you pay me!"

He went on to ask what happens to the code if the PSF decides not to pursue the idea. Shannon did not directly address that question, but he is trying to negotiate a business deal of sorts:

I believe that's how business works ;)
I have this thing, e.g an iPhone, if you want it you must pay me. I think that speeding CPython 50% is worth a few hundred iPhones.

Chris Angelico wondered about Shannon's plan for phase two, which is envisioned as a grab bag of fairly small improvements: "you're proposing a number of small tweaks in stage 2 and expecting that (combined) they can give a 50% improvement in overall performance?" But Shannon pointed out that compounding helps here: "20 tweaks each providing a 2% is a 49% speedup."

Also, as D'Aprano noted, each phase stands on its own. No commitment would need to made for phase two until phase one has been delivered (and been paid for). Then if Shannon was unable to achieve the 50% performance increase for phase two, as Angelico worries, Shannon would not be paid for that work. There is some wiggle room in the proposal, D'Aprano pointed out, so that getting a 49% increase would still result in payment, perhaps on "some sort of sliding scale".

Angelico also asked if the $2 million might be better spent on improving PyPy. Shannon said that there is a difference in what partial success would mean for the different projects. The main hurdle for PyPy as a replacement for CPython is getting the C extensions to work; a partial solution to that problem only goes so far:

Partial success of speeding up CPython is very valuable.
Partial success in getting PyPy to support C extensions well and perform well when it currently does, is much less valuable.

CPython that is "only" 2 or 3 times faster is a major improvement, but a PyPy that supports 80% of the C extensions that it currently does not is still not a replacement for CPython.

Paul Moore was generally in favor of the proposal if someone could be found to fund it. But it is important to ensure that the usual development workflow, including code review, pull requests, and so forth, is going to be followed, he said. While Shannon did not directly reply to that message, elsewhere in the thread he made it clear that he plans to handle these changes in the normal fashion. In fact, both he and Moore said that some of the money might be used to pay for code review.

Gregory P. Smith was favorably inclined toward the idea as well. Shannon is effectively proposing a path toward bringing ideas from his earlier HotPy (2) project to CPython "with an intent to help maintain them over the long term", Smith said. He believes the terms are quite reasonable, with regard to both the money and the risk protections; it is a good fit for CPython:

We've been discussing on and off in the past many years how to pay people for focused work on CPython and the ecosystem and balance that with being an open source community project. We've got some people employed along these lines already, this would become more of that and in many ways just makes sense to me.

The Python steering council also weighed in. Thomas Wouters posted a lengthy reply in early November that relayed some of the council discussion of the idea. The council is not opposed to the idea at all, but does caution that there are a lot of details that need to be worked out before it could make a serious effort to find an entity or entities to fund phase one. There is also a question of prioritization:

Regarding donations to CPython development (as earmarked donations, or from the PSF's general fund), the SC drew up a plan for investment that is centered around maintenance: reducing the maintenance burden, easing the load on volunteers where desired, working through our bug and PR backlog. [...] Speeding up pure-Python programs is not something we consider a priority at this point, at least not until we can address the larger maintenance issues.

And it may not be immediately obvious from Mark's plans, but as far as we can tell, the proposal is for speeding up pure-Python code. It will do little for code that is hampered, speed-wise, by CPython's object model, or threading model, or the C API. We have no idea how much this will actually matter to users. Making pure-Python code execution faster is always welcome, but it depends on the price. It may not be a good place to spend $500k or more, and it may even not be considered worth the implementation complexity.

Shannon replied that he had not yet made a proposal to the council but was glad to get the feedback. He agreed that maintenance is an important priority, which is why he included money for that in his proposal, but that it will be easier to justify performance improvements to potential funding entities:

I think it is a lot easier to say to corporations, give us X dollars to speed up Python and you save Y dollars, than give us X dollars to improve maintenance with no quantifiable benefit to them.

He also said that the total amount of code being added to CPython is not that large and that he hopes to offset any complexities in the implementation with better organization of the code base.

It is perfectly possible to *improve* code quality, if not necessarily size, while increasing performance. Simpler code is often faster and better algorithms do not make worse code.

It is an ambitious proposal that was generally met with approval, even if the "show me the money" funding model is somewhat unorthodox. It is not entirely clear where things go from here, but it would seem a bit sad to miss out on an opportunity to make such a large difference in CPython performance. For many Python-using companies, $500,000 (or even $2 million) is not much of a stretch financially—gathering a few of them together should make that even easier. With luck, some discussions are already underway.


(Log in to post comments)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK