Skip to content

gh-85283: _statistics uses the limited C API #108500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Aug 25, 2023

Argument Clinic supports positional-only arguments for the limited C API.

Argument Clinic supports positional-only arguments for the limited C
API.
@vstinner
Copy link
Member Author

With this change, _statistics._normal_dist_inv_cdf() uses less efficient code to parse its arguments:

  • Use METH_VARARGS calling convention (pass arguments as a tuple), instead of METH_FASTCALL (no Python object needed to pass arguments)
  • Use PyArg_ParseTuple() to parse arguments with the "ddd:_normal_dist_inv_cdf" string. PyArg_ParseTuple() has to parse the format string. Instead of using inlined code _PyArg_CheckPositional(), PyFloat_CheckExact() / PyFloat_AsDouble() and PyFloat_AS_DOUBLE().

@vstinner
Copy link
Member Author

Micro-benchmark on Linux with CPU isolation, Python built with -O3 (without LTO, without PGO):

$ python -m pyperf compare_to ref.json limited.json 
Mean +- std dev: [ref] 88.6 ns +- 1.5 ns -> [limited] 160 ns +- 1 ns: 1.81x slower

Code:

import _statistics
import pyperf

p = 0.5
mu = 0.0
sigma = 1.0

runner = pyperf.Runner()
runner.bench_func('inv_cdf', _statistics._normal_dist_inv_cdf, p, mu, sigma)

@vstinner
Copy link
Member Author

I'm not sure if the _statistics is a good candidate for this work on converting stdlib extensions to the limited C API. The Lib/statistics.py module has a pure Python implementation, and the C code is there for optimization. So I would expect best performance for that. Moreover, the function is quite fast, around 100 ns, so the cost of argument parsing is significant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants