-
Notifications
You must be signed in to change notification settings - Fork 787
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
pyfunction: refactor argument extraction #1440
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
authors = ["PyO3 Authors"] | ||
name = "pyo3-benchmarks" | ||
version = "0.1.0" | ||
description = "Python-based benchmarks for various PyO3 functionality" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
|
||
[dependencies.pyo3] | ||
path = "../../" | ||
features = ["extension-module"] | ||
|
||
[lib] | ||
name = "_pyo3_benchmarks" | ||
crate-type = ["cdylib"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
include pyproject.toml Cargo.toml | ||
recursive-include src * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# rustapi_module | ||
|
||
A simple extension module built using PyO3. | ||
|
||
## Build | ||
|
||
```shell | ||
python setup.py install | ||
``` | ||
|
||
## Testing | ||
|
||
To test install tox globally and run | ||
|
||
```shell | ||
tox -e py | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from ._pyo3_benchmarks import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pip>=19.1 | ||
hypothesis>=3.55 | ||
pytest>=3.5.0 | ||
setuptools-rust>=0.10.2 | ||
psutil>=5.6 | ||
pytest-benchmark~=3.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import sys | ||
import platform | ||
|
||
from setuptools import setup | ||
from setuptools_rust import RustExtension | ||
|
||
|
||
def get_py_version_cfgs(): | ||
# For now each Cfg Py_3_X flag is interpreted as "at least 3.X" | ||
version = sys.version_info[0:2] | ||
py3_min = 6 | ||
out_cfg = [] | ||
for minor in range(py3_min, version[1] + 1): | ||
out_cfg.append("--cfg=Py_3_%d" % minor) | ||
|
||
if platform.python_implementation() == "PyPy": | ||
out_cfg.append("--cfg=PyPy") | ||
|
||
return out_cfg | ||
|
||
|
||
setup( | ||
name="pyo3-benchmarks", | ||
version="0.1.0", | ||
classifiers=[ | ||
"License :: OSI Approved :: MIT License", | ||
"Development Status :: 3 - Alpha", | ||
"Intended Audience :: Developers", | ||
"Programming Language :: Python", | ||
"Programming Language :: Rust", | ||
"Operating System :: POSIX", | ||
"Operating System :: MacOS :: MacOS X", | ||
], | ||
packages=["pyo3_benchmarks"], | ||
rust_extensions=[ | ||
RustExtension( | ||
"pyo3_benchmarks._pyo3_benchmarks", | ||
rustc_flags=get_py_version_cfgs(), | ||
debug=False, | ||
), | ||
], | ||
include_package_data=True, | ||
zip_safe=False, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use pyo3::prelude::*; | ||
use pyo3::types::{PyDict, PyTuple}; | ||
use pyo3::wrap_pyfunction; | ||
|
||
#[pyfunction(args = "*", kwargs = "**")] | ||
fn args_and_kwargs<'a>( | ||
args: &'a PyTuple, | ||
kwargs: Option<&'a PyDict>, | ||
) -> (&'a PyTuple, Option<&'a PyDict>) { | ||
(args, kwargs) | ||
} | ||
|
||
#[pyfunction(a, b = 2, args = "*", c = 4, kwargs = "**")] | ||
fn mixed_args<'a>( | ||
a: i32, | ||
b: i32, | ||
args: &'a PyTuple, | ||
c: i32, | ||
kwargs: Option<&'a PyDict>, | ||
) -> (i32, i32, &'a PyTuple, i32, Option<&'a PyDict>) { | ||
(a, b, args, c, kwargs) | ||
} | ||
|
||
#[pyfunction] | ||
fn no_args() {} | ||
|
||
#[pymodule] | ||
fn _pyo3_benchmarks(_py: Python<'_>, m: &PyModule) -> PyResult<()> { | ||
m.add_function(wrap_pyfunction!(args_and_kwargs, m)?)?; | ||
m.add_function(wrap_pyfunction!(mixed_args, m)?)?; | ||
m.add_function(wrap_pyfunction!(no_args, m)?)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import pyo3_benchmarks | ||
|
||
|
||
def test_args_and_kwargs(benchmark): | ||
benchmark(pyo3_benchmarks.args_and_kwargs, 1, 2, 3, a=4, foo=10) | ||
|
||
|
||
def args_and_kwargs_py(*args, **kwargs): | ||
return (args, kwargs) | ||
|
||
|
||
def test_args_and_kwargs_py(benchmark): | ||
rust = pyo3_benchmarks.args_and_kwargs(1, 2, 3, bar=4, foo=10) | ||
py = args_and_kwargs_py(1, 2, 3, bar=4, foo=10) | ||
assert rust == py | ||
benchmark(args_and_kwargs_py, 1, 2, 3, bar=4, foo=10) | ||
|
||
|
||
def test_mixed_args(benchmark): | ||
benchmark(pyo3_benchmarks.mixed_args, 1, 2, 3, bar=4, foo=10) | ||
|
||
|
||
def mixed_args_py(a, b=2, *args, c=4, **kwargs): | ||
return (a, b, args, c, kwargs) | ||
|
||
|
||
def test_mixed_args_py(benchmark): | ||
rust = pyo3_benchmarks.mixed_args(1, 2, 3, bar=4, foo=10) | ||
py = mixed_args_py(1, 2, 3, bar=4, foo=10) | ||
assert rust == py | ||
benchmark(mixed_args_py, 1, 2, 3, bar=4, foo=10) | ||
|
||
|
||
def test_no_args(benchmark): | ||
benchmark(pyo3_benchmarks.no_args) | ||
|
||
|
||
def no_args_py(): | ||
return None | ||
|
||
|
||
def test_no_args_py(benchmark): | ||
rust = pyo3_benchmarks.no_args() | ||
py = no_args_py() | ||
assert rust == py | ||
benchmark(no_args_py) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[tox] | ||
# can't install from sdist because local pyo3 repo can't be included in the sdist | ||
skipsdist = true | ||
|
||
[testenv] | ||
description = Run the unit tests under {basepython} | ||
deps = -rrequirements-dev.txt | ||
commands = | ||
python setup.py install | ||
pytest {posargs} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how this benchmark is useful.
It may be difficult to observe some speedup when trying to extract only a few args.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually although the variance is quite high, there's still a reasonably clear signal as to how good a change is using these benchmarks. I wouldn't take these numbers super seriously for any kind of scientific conclusion. However I can see (for example) that there's still some overhead calling pyo3 functions vs CPython functions, and also that returning
Vec
fromextract_keyword_arguments
leads to noticeable slowdown.Some example numbers I see at the moment:
At the moment it's clear we're a bit slower than CPython for function calls, but I hope vectorcall support and #1308 will improve things.
Later I would like to add more numbers to these benchmarks, for example #661.