This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author BTaskaya
Recipients BTaskaya, Mark.Shannon, pablogsal, serhiy.storchaka
Date 2021-06-23.19:40:41
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1624477242.42.0.942780430737.issue44501@roundup.psfhosted.org>
In-reply-to
Content
> I already experimented with this in issue33325. It does not worth an effort.

Yea, that is what the results for the CFG optimization shows to me (1.13x at best, ~1.03x in real cases). 

> For optimizing calls with constant arguments, it looks more interesting. Do you have any statistic about what percentage of calls is with constant arguments, what is the average number of constant arguments, what are microbenchmark results in debug mode for the average number of constant arguments? Please test for trivial Python function with variable and fixed number of parameters.

Okay, addressing multiple points here (with the common dataset of a couple thousand pypi projects)

>> Do you have any statistic about what percentage of calls is with constant arguments

Since calls are the most common operation (?) in the language, the percentage might seem low since the total number is just absurd: 0.6840838575699993% (96170/14058218) of all calls are made with positional only constant arguments (LEN(args) >= 3, not including 1 or 2 arguments). 

>> what is the average number of constant arguments


> stats = {3: 69533, 4: 10867, 5: 9071, 6: 4323, 7: 1504, 8: 252, 9: 167, ...}
> np.average(list(stats.keys()), weights=list(stats.values()))
> 3.5765623375272955

points me out the result of 3.57 arguments, which is rounded to 4.

>> what are microbenchmark results in debug mode for the average number of constant arguments?

-s 'f = lambda *s: None' 'f(1,2,3,4)'
3  arguments: 1.01x faster
4  arguments: 1.05x faster
5  arguments: 1.08x faster
6  arguments: 1.11x faster
7  arguments: 1.13x faster
8  arguments: 1.16x faster
9  arguments: 1.30x faster
10 arguments: 1.32x faster

(there seems to be something special about 9, since it breaks the chart about the speedups)

An interesting (and potentially something related to the nature of datetime.datetime being implemented in C) benchmark is that instead of calling an empty python function, if we switch to datetime.datetime the early speed-up goes 1.13x for even 3 arguments!

-s 'from datetime import datetime' 'datetime(1,2,3)'

3 arguments: 1.13x faster
4 arguments: 1.15x faster
5 arguments: 1.16x faster
6 arguments: 1.17x faster
7 arguments: 1.20x faster

Another really good example is re.match().group() (I assume this is also implemented in C, so some sort of CALL_FUNCTION_EX magic)

-s 'import re; group = re.match("(a)(a)(a)(a)(a)(a)(a)(a)", "a" * 8).group' 'group(0,1,2)'

3 arguments: 1.25x faster (seems to be the general speed-up)
4 arguments: 1.26x faster
5 arguments: 1.24x faster
6 arguments: 1.23x faster
7 arguments: 1.24x faster
8 arguments: 1.24x faster
9 arguments: 1.27x faster

These all can be verified by simply running the same code but wrapping all arguments as a tuple and expanding them (generates the same code) (e.g: -s 'import re; group = re.match("(a)(a)(a)(a)(a)(a)(a)(a)", "a" * 8).group' 'group(*(0,1,2,3,4,5,6,7,8))' would perform as fast as the optimized one) 

>> Please test for trivial Python function with variable and fixed number of parameters.

I already shared the variable one, so this is the results for a fixed one;

-s 'def foo(a, b, c): pass' 'foo(1,2,3)'

3 arguments: 1.03x faster
4 arguments: 1.07x faster
5 arguments: 1.10x faster
6 arguments: 1.13x faster
7 arguments: 1.17x faster
8 arguments: 1.20x faster
9 arguments: 1.36x faster (consistent with the other jump)

Here is the list of functions that I've found to be called with only constant arguments (very raw!): https://gist.github.com/isidentical/099da256f2a46d13cef8570e839a2cea

Overall I believe it shows a great improvement for little-to-none change on the codebase (especially for C-level functions).
History
Date User Action Args
2021-06-23 19:40:42BTaskayasetrecipients: + BTaskaya, Mark.Shannon, serhiy.storchaka, pablogsal
2021-06-23 19:40:42BTaskayasetmessageid: <1624477242.42.0.942780430737.issue44501@roundup.psfhosted.org>
2021-06-23 19:40:42BTaskayalinkissue44501 messages
2021-06-23 19:40:41BTaskayacreate