[Python-Dev] Speeding up CPython 5-10% (original) (raw)

Yury Selivanov yselivanov.ml at gmail.com
Wed Jan 27 15:37:49 EST 2016


On 2016-01-27 3:10 PM, Damien George wrote:

Hi Yuri,

I think these are great ideas to speed up CPython. They are probably the simplest yet most effective ways to get performance improvements in the VM.

Thanks!

MicroPython has had LOADMETHOD/CALLMETHOD from the start (inspired by PyPy, and the main reason to have it is because you don't need to allocate on the heap when doing a simple method call). The specific opcodes are: LOADMETHOD # same behaviour as you propose CALLMETHOD # for calls with positional and/or keyword args CALLMETHODVARKW # for calls with one or both of /* We also have LOADATTR, CALLFUNCTION and CALLFUNCTIONVARKW for non-method calls.

Yes, we'll need to add CALL_METHOD{_VAR|_KW|etc} opcodes to optimize all kind of method calls. However, I'm not sure how big the impact will be, need to do more benchmarking.

BTW, how do you benchmark MicroPython?

MicroPython also has dictionary lookup caching, but it's a bit different to your proposal. We do something much simpler: each opcode that has a cache ability (eg LOADGLOBAL, STOREGLOBAL, LOADATTR, etc) includes a single byte in the opcode which is an offset-guess into the dictionary to find the desired element. Eg for LOADGLOBAL we have (pseudo code): CASE(LOADGLOBAL): key = DECODEKEY; offsetguess = DECODEBYTE; if (globaldict[offsetguess].key == key) { // found the element straight away } else { // not found, do a full lookup and save the offset offsetguess = dictlookup(globaldict, key); UPDATEBYTECODE(offsetguess); } PUSH(globaldict[offsetguess].elem); We have found that such caching gives a massive performance increase, on the order of 20%. The issue (for us) is that it increases bytecode size by a considerable amount, requires writeable bytecode, and can be non-deterministic in terms of lookup time. Those things are important in the embedded world, but not so much on the desktop.

That's a neat idea! You're right, it does require bytecode to become writeable. I considered implementing a similar strategy, but this would be a big change for CPython. So I decided to minimize the impact of the patch and leave the opcodes untouched.

Thanks! Yury



More information about the Python-Dev mailing list