STINNER Victor <vstin...@redhat.com> added the comment:

>  Also, the performance improvements are in argument parsing, but not when you 
> have numerical code like a * b, where a and b are already decimals.

If the function call takes around 100 ns, the benefit of FASTCALL and the new 
more efficient function to parse arguments becomes quite significant. Some 
Decimal methods are that fast if not faster.

python3 -m perf timeit \
   -s 'import decimal; a=decimal.Decimal(1); b=decimal.Decimal(2); 
ctx=decimal.getcontext()' \
   'ctx.copy_sign(a, b)' \
   --duplicate=1024

=> Mean +- std dev: [ref] 148 ns +- 1 ns -> [fastcall] 98.9 ns +- 4.9 ns: 1.49x 
faster (-33%)

./python -m perf timeit \
   -s 'import decimal; a=decimal.Decimal(1); b=decimal.Decimal(2)' \
   'a.copy_sign(b)' \
   --duplicate=1024

=> Mean +- std dev: [ref] 123 ns +- 5 ns -> [fastcall] 86.5 ns +- 1.4 ns: 1.42x 
faster (-29%)

I wrote a quick & dirty change using directly METH_FASTCALL with 
_PyArg_ParseStackAndKeywords() (dec_mpd_qcopy_sign) and 
_PyArg_CheckPositional() (ctx_mpd_qcopy_sign). Using Argument Clinic may be 
more verbose, but it generates *even more* efficient code for functions 
accepting keyword arguments (like Decimal.copy_sign) and generate better 
docstring (at least, the signature for function parameters if no text is given).

Obviously, if your application only uses large numbers, the benefit will be 
invisible. But I guess that some people use decimal with "small" numbers ;-)

Note: Sure, you're right that operators like "a * b" wouldn't benefit from 
FASTCALL since they don't parse explicitly arguments. BINARY_MULTIPLY 
instruction gets directly 2 values from the Python stack and pass them to 
PyNumber_Multiply() with is defined to always take exactly 2 PyObject* in C.

----------

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

Reply via email to