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

Add nightly-optimizations feature #927

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## Added
- Gate some optimizations needing specialization in a `nightly-optimizations` feature (disabled by default). [#927](https://github.com/PyO3/pyo3/pull/927)

## [0.10.0] - 2020-05-14
### Fixed
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ python3 = []
# so that the module can also be used with statically linked python interpreters.
extension-module = []

# Use this feature to enable optimizations only possible on nightly Rust.
# (Dependent on specialization.)
nightly-optimizations = []

# The stable cpython abi as defined in PEP 384. Currently broken with
# many compilation errors. Pull Requests working towards fixing that
# are welcome.
Expand Down
15 changes: 15 additions & 0 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ impl<T> ToBorrowedObject for T
where
T: ToPyObject,
{
#[cfg(not(nightly_optimizations))]
fn with_borrowed_ptr<F, R>(&self, py: Python, f: F) -> R
where
F: FnOnce(*mut ffi::PyObject) -> R,
{
let ptr = self.to_object(py).into_ptr();
let result = f(ptr);
unsafe {
ffi::Py_XDECREF(ptr);
}
result
}

#[cfg(nightly_optimizations)]
default fn with_borrowed_ptr<F, R>(&self, py: Python, f: F) -> R
where
F: FnOnce(*mut ffi::PyObject) -> R,
Expand All @@ -117,6 +131,7 @@ where
}
}

#[cfg(nightly_optimizations)]
impl<T> ToBorrowedObject for T
where
T: ToPyObject + AsPyPointer,
Expand Down
17 changes: 17 additions & 0 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2017-present PyO3 Project and Contributors

#[cfg(nightly_optimizations)]
use crate::buffer;
use crate::err::{self, PyDowncastError, PyErr, PyResult};
use crate::exceptions;
Expand Down Expand Up @@ -265,13 +266,22 @@ macro_rules! array_impls {
where
T: Copy + Default + FromPyObject<'a>,
{
#[cfg(not(nightly_optimizations))]
fn extract(obj: &'a PyAny) -> PyResult<Self> {
let mut array = [T::default(); $N];
extract_sequence_into_slice(obj, &mut array)?;
Ok(array)
}

#[cfg(nightly_optimizations)]
default fn extract(obj: &'a PyAny) -> PyResult<Self> {
let mut array = [T::default(); $N];
extract_sequence_into_slice(obj, &mut array)?;
Ok(array)
}
}

#[cfg(nightly_optimizations)]
impl<'source, T> FromPyObject<'source> for [T; $N]
where
for<'a> T: Copy + Default + FromPyObject<'a> + buffer::Element,
Expand Down Expand Up @@ -304,11 +314,18 @@ impl<'a, T> FromPyObject<'a> for Vec<T>
where
T: FromPyObject<'a>,
{
#[cfg(not(nightly_optimizations))]
fn extract(obj: &'a PyAny) -> PyResult<Self> {
extract_sequence(obj)
}

#[cfg(nightly_optimizations)]
default fn extract(obj: &'a PyAny) -> PyResult<Self> {
extract_sequence(obj)
}
}

#[cfg(nightly_optimizations)]
impl<'source, T> FromPyObject<'source> for Vec<T>
where
for<'a> T: FromPyObject<'a> + buffer::Element + Copy,
Expand Down