Repository navigation
Closed
Labels
3.7 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usage
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/1st1'
closed_at = <Date 2017-01-16.08:23:59.319>
created_at = <Date 2016-01-14.17:09:06.979>
labels = ['interpreter-core', '3.7', 'performance']
title = 'Speedup method calls 1.2x'
updated_at = <Date 2021-05-16.09:26:18.303>
user = 'https://github.com/1st1'bugs.python.org fields:
activity = <Date 2021-05-16.09:26:18.303>
actor = 'kj'
assignee = 'yselivanov'
closed = True
closed_date = <Date 2017-01-16.08:23:59.319>
closer = 'methane'
components = ['Interpreter Core']
creation = <Date 2016-01-14.17:09:06.979>
creator = 'yselivanov'
dependencies = []
files = ['41614', '43599', '43601', '45786', '45787', '45788', '45866', '45887', '45888', '45889', '45891', '45896', '45963', '46294']
hgrepos = []
issue_num = 26110
keywords = ['patch']
message_count = 31.0
messages = ['258204', '258205', '258206', '258211', '258220', '258222', '259333', '259339', '264659', '269662', '269665', '282620', '282621', '282622', '282627', '283075', '283077', '283093', '283094', '283147', '283148', '283149', '283160', '283161', '283166', '283168', '283177', '285415', '285511', '285544', '345335']
nosy_count = 22.0
nosy_names = ['twouters', 'brett.cannon', 'jcea', 'ncoghlan', 'vstinner', 'benjamin.peterson', 'dino.viehland', 'methane', 'santagada', 'python-dev', 'serhiy.storchaka', 'maciej.szulik', 'jdemeyer', 'yselivanov', 'jstasiak', 'josh.r', 'abarry', 'alecsandru.patrascu', 'florin.papa', 'eric.fahlgren', 'Ryan May', 'kj']
pr_nums = ['552', '26014', '26159']
priority = 'normal'
resolution = 'fixed'
stage = 'commit review'
status = 'closed'
superseder = None
type = 'performance'
url = 'https://bugs.python.org/issue26110'
versions = ['Python 3.7']Metadata
Metadata
Assignees
Labels
3.7 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usage
Activity
1st1 commentedon Jan 14, 2016
This issue supersedes issue bpo-6033. I decided to open a new one, since the patch is about Python 3.6 (not 2.7) and is written from scratch.
The idea is to add new opcodes to avoid instantiation of BoundMethods. The patch only affects method calls of Python functions with positional arguments.
I'm working on the attached patch in this repo: https://github.com/1st1/cpython/tree/call_meth2
If the patch gets accepted, I'll update it with the docs etc.
Performance Improvements
------------------------
Method calls in micro-benchmarks are 1.2x faster:
### call_method ###
Avg: 0.330371 -> 0.281452: 1.17x faster
### call_method_slots ###
Avg: 0.333489 -> 0.280612: 1.19x faster
### call_method_unknown ###
Avg: 0.304949 -> 0.251623: 1.21x faster
Improvements in mini-benchmarks, such as Richards are less impressive, I'd say it's 3-7% improvement. The full output of benchmarks/perf.py is here: https://gist.github.com/1st1/e00f11586329f68fd490
When the full benchmarks suite is run, some of them report that they were slow. When I ran them separately several times, they all show no real slowdowns. It's just some of them (such as nbody) are highly unstable.
It's actually possible to improve the performance another 1-3% if we fuse __PyObject_GetMethod with ceval/LOAD_METHOD code. I've tried this here: https://github.com/1st1/cpython/tree/call_meth4, however I don't like to have so many details of object.c into ceval.c.
Changes in the Core
-------------------
Two new opcodes are added -- LOAD_METHOD and CALL_METHOD. Whenever compiler sees a method call "obj.method(..)" with positional arguments it compiles it as follows:
LOAD_METHOD implementation in ceval looks up "method" on obj's type, and checks that it wasn't overridden in obj.__dict__. Apparently, even with the __dict__ check this is still faster then creating a BoundMethod instance etc.
If the method is found and not overridden, LOAD_METHOD pushes the resolved method object, and 'obj'. If the method was overridden, the resolved method object and NULL are pushed to the stack.
CALL_METHOD then looks at the two stack values after call arguments. If the first one isn't NULL, it means that we have a method call.
Why CALL_METHOD?
----------------
It's actually possible to hack CALL_FUNCTION to support LOAD_METHOD. I've tried this approach in https://github.com/1st1/cpython/tree/call_meth3. It looks like that adding extra checks in CALL_FUNCTION have negative impact on many benchmarks. It's really easier to add another opcode.
Why only pure-Python methods?
-----------------------------
LOAD_METHOD atm works only with methods defined in pure Python. C methods, such as
list.appendare still wrapped into a descriptor, that creates a PyCFunction object on each attribute access. I've tried to do that in https://github.com/1st1/cpython/tree/call_cmeth. It does impact C method calls in a positive way, although my implementation is very hacky. It still uses LOAD_METHOD and CALL_METHOD opcodes, so my idea is to consider merging this patch first, and then introduce the necessary refactoring of PyCFunction and MethodDesctiptors in a separate issue.Why only calls with positional arguments?
-----------------------------------------
As showed in "Why CALL_METHOD?", making CALL_FUNCTION to work with LOAD_METHOD slows it down. For keyword and var-arg calls we have three more opcodes -- CALL_FUNCTION_VAR, CALL_FUNCTION_KW, and CALL_FUNCTION_VAR_KW. I suspect that making them work with LOAD_METHOD would slow them down too, which will probably require us to add three (!) more opcodes for LOAD_METHOD.
And these kind of calls require much more overhead anyways, I don't expect them to be as optimizable as positional arg calls.
vstinner commentedon Jan 14, 2016
I don't think that it's an issue to add 3 more opcodes for performance. If you prefer to limit the number of opcodes, you can pass a flag in arguments. For example, use 2 bytes for 2 arguments instead of only 1?
See also the (now old) WPython project which proposed kind of CISC instructions:
https://code.google.com/p/wpython/
1st1 commentedon Jan 14, 2016
I tried two approaches:
Push one more leading NULL to the stack in LOAD_METHOD (which makes it push 3 vals). Then, in CALL_FUNCTION, detect when the callable is NULL and to the stuff that CALL_METHOD does.
Use a bitflag on oparg for CALL_FUNCTION and CALL_* opcodes. This adds EXTENDED_ARG opcode overhead.
Long story short, the only option would be to add dedicated opcodes to work with LOAD_METHOD. However, I'd prefer to first merge this patch, as it's relatively small and easy to review, and then focus on improving other things (keyword/*arg calls, C methods, etc). This is just a first step.
gvanrossum commentedon Jan 14, 2016
I like this idea! I like the limitations to positional-only calls. I do think that it would be nice if we could speed up C calls too -- today, s.startswith('abc') is slower than s[:3] == 'abc' precisely because of the lookup. But I'm all for doing this one step at a time, so we can be sure it is solid before taking the next step(s).
1st1 commentedon Jan 14, 2016
Yes, I think we can make
<built-in type>.method(..)calls much faster with LOAD_METHOD. Since those types lack__dict__and their__class__is read-only, we can use a far better optimized code path without extra lookups and creation of BoundMethod/PyCFunction objects.vstinner commentedon Jan 14, 2016
I'm happy to see people working on optimizing CPython ;-)
1st1 commentedon Feb 1, 2016
For those interested in reviewing this patch at some point: please wait until I upload a new version. The current patch is somewhat outdated.
alecsandrupatrascu commentedon Feb 1, 2016
Yury, thank you for the heads up! Here at Intel, in the Dynamic Scripting Languages Optimization Team, we can help the community with reviewing and measuring this patch in our quiet and stable environment, the same one that we use to provide public CPython daily measurements. We will wait for your update.
gvanrossum commentedon May 2, 2016
This patch doesn't apply cleanly any more. Is it easy to update?
methane commentedon Jul 1, 2016
Updated, based on 102241:908b801f8a62
methane commentedon Jul 1, 2016
Oops, previous patch doesn't update magic number in PC/launcher.c
Should I update it? Or don't touch it to avoid additional conflicts?
7 remaining items
serhiy-storchaka commentedon Dec 13, 2016
Agreed if that so hard.
1st1 commentedon Dec 13, 2016
Inada-san, when I tested the patch last time, I think there was a regression somewhere, related to the descriptor protocol. Have you fixed that one?
python-dev commentedon Dec 14, 2016
New changeset 64afd5cab40a by Yury Selivanov in branch 'default':
Issue bpo-26110: Add LOAD_METHOD/CALL_METHOD opcodes.
https://hg.python.org/cpython/rev/64afd5cab40a
1st1 commentedon Dec 14, 2016
Seems to be either fixed, or maybe those bugs were related to my opcode cache patch. Anyways, I decided to commit the patch to 3.7, otherwise it might miss the commit window as it did for 3.6. Let's fix any regressions right in the repo now.
I like this idea!
Thanks Inada-san for pushing this patch through, and thanks to Serhiy for reviewing it.
methane commentedon Dec 14, 2016
I'm working on changing stack layout slightly
current patch: callable | NULL | arg1 | ...argN
next patch will: NULL | callable | arg1 | ...argN
After benchmark with PGO build, I'll post it.
methane commentedon Dec 14, 2016
I haven't noticed the patch is committed already.
Changing stack layout slightly is for easy to document, not for performance.
Please don't close this issue until adding document in Doc/library/dis.rst
methane commentedon Dec 14, 2016
PGO benchmark result
https://gist.github.com/methane/64ab60c38324423ef27af70cc6db1dfd
methane commentedon Dec 14, 2016
This patch modify stack layout slightly and adds document in Doc/library/dis.rst
vstinner commentedon Dec 14, 2016
INADA Naoki: "My current idea is adding new
tp_fastcallslot which has same signature to _PyFunction_FastCallDict or _PyCFunction_FastCallDict."FYI I'm working on a solution to avoid tuple and dict to pass parameters to tp_new, tp_init and tp_call. I have PoC implementations (yeah, more than one). Please come to me directly if you are interested, this issue is not the right place to discuss.
vstinner commentedon Jan 13, 2017
I just created the issue bpo-29263: "Implement LOAD_METHOD/CALL_METHOD for C functions".
methane commentedon Jan 15, 2017
Yury, could you review this?
python-dev commentedon Jan 16, 2017
New changeset a6241b2073c6 by INADA Naoki in branch 'default':
Issue bpo-26110: Add document for LOAD_METHOD and CALL_METHOD opcode.
https://hg.python.org/cpython/rev/a6241b2073c6
jdemeyer commentedon Jun 12, 2019
Note that this idea has been generalized by PEP-590: any type can support this optimization by setting the Py_TPFLAGS_METHOD_DESCRIPTOR flag.