New submission from Josh Rosenberg:

Right now, in typeobject.c, the call_method and call_maybe utility functions 
have a fast path for no argument methods, where a NULL or "" format string just 
calls PyTuple_New(0) directly instead of wasting time parsing Py_VaBuildValue.

Problem is, nothing uses it. Every no arg user (the slot wrappers for __len__, 
__index__ and __next__ directly, and indirectly through the SLOT0 macro for 
__neg__, __pos__, __abs__, __invert__, __int__ and __float__) is passing along 
"()" as the format string, which fails the test for NULL/"", so it calls 
Py_VaBuildValue that goes to an awful lot of trouble to scan the string a few 
times and eventually spit out the empty tuple anyway.

Changing the three direct calls to call_method where it passes "()" as the 
format string, as well as the definition of SLOT0, to replace "()" with NULL as 
the format string argument should remove a non-trivial number of C varargs 
function calls and string processing, replacing it with a single, cheap 
PyTuple_New(0) call (which Py_VaBuildValue was already eventually performing 
anyway). If I understand the purpose of these slot wrapper functions, that 
should give a free speed up to all types implemented at the Python level, 
particularly numeric types (e.g. fractions.Fraction) and container/iterator 
types (speeding up __len__ and __next__ respectively).

I identified this while on a work machine which I can't use to check out the 
Python repository; I'll submit a patch later today if no one else gets to it, 
once I'm home and can use my own computer to make/test the fix.

----------
components: Interpreter Core
messages: 263419
nosy: josh.r
priority: normal
severity: normal
status: open
title: Unnecessary format string handling for no argument slot wrappers in 
typeobject.c
versions: Python 3.5, Python 3.6

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26758>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to