-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reintroduce
vectorcall
optimization with new PyCallArgs
trait
- Loading branch information
Showing
4 changed files
with
401 additions
and
29 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
//! TODO | ||
//! | ||
//! | ||
use crate::ffi_ptr_ext::FfiPtrExt as _; | ||
use crate::types::{PyAnyMethods as _, PyDict, PyString, PyTuple}; | ||
use crate::{ffi, Borrowed, Bound, IntoPyObjectExt as _, Py, PyAny, PyResult}; | ||
|
||
pub(crate) mod private { | ||
use super::*; | ||
|
||
pub trait Sealed {} | ||
|
||
impl Sealed for () {} | ||
impl Sealed for Bound<'_, PyTuple> {} | ||
impl Sealed for Py<PyTuple> {} | ||
|
||
pub struct Token; | ||
} | ||
|
||
/// TODO | ||
pub trait PyCallArgs<'py>: Sized + private::Sealed { | ||
#[doc(hidden)] | ||
fn call( | ||
self, | ||
function: Borrowed<'_, 'py, PyAny>, | ||
kwargs: Borrowed<'_, 'py, PyDict>, | ||
token: private::Token, | ||
) -> PyResult<Bound<'py, PyAny>>; | ||
|
||
#[doc(hidden)] | ||
fn call_positional( | ||
self, | ||
function: Borrowed<'_, 'py, PyAny>, | ||
token: private::Token, | ||
) -> PyResult<Bound<'py, PyAny>>; | ||
|
||
#[doc(hidden)] | ||
fn call_method_positional( | ||
self, | ||
object: Borrowed<'_, 'py, PyAny>, | ||
method_name: Borrowed<'_, 'py, PyString>, | ||
_: private::Token, | ||
) -> PyResult<Bound<'py, PyAny>> { | ||
object | ||
.getattr(method_name) | ||
.and_then(|method| method.call1(self)) | ||
} | ||
} | ||
|
||
impl<'py> PyCallArgs<'py> for () { | ||
fn call( | ||
self, | ||
function: Borrowed<'_, 'py, PyAny>, | ||
kwargs: Borrowed<'_, 'py, PyDict>, | ||
token: private::Token, | ||
) -> PyResult<Bound<'py, PyAny>> { | ||
let args = self.into_pyobject_or_pyerr(function.py())?; | ||
args.call(function, kwargs, token) | ||
} | ||
|
||
fn call_positional( | ||
self, | ||
function: Borrowed<'_, 'py, PyAny>, | ||
token: private::Token, | ||
) -> PyResult<Bound<'py, PyAny>> { | ||
let args = self.into_pyobject_or_pyerr(function.py())?; | ||
args.call_positional(function, token) | ||
} | ||
} | ||
|
||
impl<'py> PyCallArgs<'py> for Bound<'py, PyTuple> { | ||
fn call( | ||
self, | ||
function: Borrowed<'_, 'py, PyAny>, | ||
kwargs: Borrowed<'_, '_, PyDict>, | ||
_: private::Token, | ||
) -> PyResult<Bound<'py, PyAny>> { | ||
unsafe { | ||
ffi::PyObject_Call(function.as_ptr(), self.as_ptr(), kwargs.as_ptr()) | ||
.assume_owned_or_err(function.py()) | ||
} | ||
} | ||
|
||
fn call_positional( | ||
self, | ||
function: Borrowed<'_, 'py, PyAny>, | ||
_: private::Token, | ||
) -> PyResult<Bound<'py, PyAny>> { | ||
unsafe { | ||
ffi::PyObject_Call(function.as_ptr(), self.as_ptr(), std::ptr::null_mut()) | ||
.assume_owned_or_err(function.py()) | ||
} | ||
} | ||
} | ||
|
||
impl<'py> PyCallArgs<'py> for Py<PyTuple> { | ||
fn call( | ||
self, | ||
function: Borrowed<'_, 'py, PyAny>, | ||
kwargs: Borrowed<'_, '_, PyDict>, | ||
_: private::Token, | ||
) -> PyResult<Bound<'py, PyAny>> { | ||
unsafe { | ||
ffi::PyObject_Call(function.as_ptr(), self.as_ptr(), kwargs.as_ptr()) | ||
.assume_owned_or_err(function.py()) | ||
} | ||
} | ||
|
||
fn call_positional( | ||
self, | ||
function: Borrowed<'_, 'py, PyAny>, | ||
_: private::Token, | ||
) -> PyResult<Bound<'py, PyAny>> { | ||
unsafe { | ||
ffi::PyObject_Call(function.as_ptr(), self.as_ptr(), std::ptr::null_mut()) | ||
.assume_owned_or_err(function.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
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.