Skip to content
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

BigInt speedup #3379

Merged
merged 3 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3379.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sped up FromPyObject::extract for BigInt and BigUint by up to 43% (although mileage may vary depending on int size and sign)
6 changes: 6 additions & 0 deletions pyo3-benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pyo3 = { path = "../", features = ["auto-initialize"] }

[dev-dependencies]
criterion = "0.5.1"
num-bigint = "0.4.3"

[[bench]]
name = "bench_any"
Expand Down Expand Up @@ -77,4 +78,9 @@ harness = false
name = "bench_extract"
harness = false

[[bench]]
name = "bench_bigint"
harness = false
required-features = ["pyo3/num-bigint"]

[workspace]
83 changes: 83 additions & 0 deletions pyo3-benches/benches/bench_bigint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};

use pyo3::{types::PyDict, PyAny, Python};

use num_bigint::BigInt;

fn extract_bigint_extract_fail(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let d = PyDict::new(py) as &PyAny;

bench.iter(|| match black_box(d).extract::<BigInt>() {
Ok(v) => panic!("should err {}", v),
Err(e) => black_box(e),
});
});
}

fn extract_bigint_small(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let int = py.eval("-42", None, None).unwrap();

bench.iter(|| {
let v = black_box(int).extract::<BigInt>().unwrap();
black_box(v);
});
});
}

fn extract_bigint_big_negative(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let int = py.eval("-10**300", None, None).unwrap();

bench.iter(|| {
let v = black_box(int).extract::<BigInt>().unwrap();
black_box(v);
});
});
}

fn extract_bigint_big_positive(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let int = py.eval("10**300", None, None).unwrap();

bench.iter(|| {
let v = black_box(int).extract::<BigInt>().unwrap();
black_box(v);
});
});
}

fn extract_bigint_huge_negative(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let int = py.eval("-10**3000", None, None).unwrap();

bench.iter(|| {
let v = black_box(int).extract::<BigInt>().unwrap();
black_box(v);
});
});
}

fn extract_bigint_huge_positive(bench: &mut Bencher<'_>) {
Python::with_gil(|py| {
let int = py.eval("10**3000", None, None).unwrap();

bench.iter(|| {
let v = black_box(int).extract::<BigInt>().unwrap();
black_box(v);
});
});
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("extract_bigint_extract_fail", extract_bigint_extract_fail);
c.bench_function("extract_bigint_small", extract_bigint_small);
c.bench_function("extract_bigint_big_negative", extract_bigint_big_negative);
c.bench_function("extract_bigint_big_positive", extract_bigint_big_positive);
c.bench_function("extract_bigint_huge_negative", extract_bigint_huge_negative);
c.bench_function("extract_bigint_huge_positive", extract_bigint_huge_positive);
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Loading