perf: fix missing noexcepts on minpybind in functorch (#94135)

Noticed this performance bug in functorch. We got a pretty big perf in pybind11 improvement by explicitly marking at noexcept, see https://quuxplusone.github.io/blog/2022/08/26/vector-pessimization/

Pull Request resolved: https://github.com/pytorch/pytorch/pull/94135
Approved by: https://github.com/ezyang
This commit is contained in:
Aaron Gokaslan
2023-02-04 20:07:11 +00:00
committed by PyTorch MergeBot
parent f54fd6fb28
commit 3693039bb7

View File

@ -59,8 +59,7 @@ struct vector_args;
struct handle {
handle(PyObject* ptr)
: ptr_(ptr) {}
handle()
: ptr_(nullptr) {}
handle() = default;
PyObject* ptr() const {
@ -90,7 +89,7 @@ struct handle {
}
protected:
PyObject * ptr_;
PyObject* ptr_ = nullptr;
};
@ -107,7 +106,7 @@ struct hdl : public handle {
}
hdl(T* ptr)
: hdl((PyObject*) ptr) {}
hdl(obj<T> o)
hdl(const obj<T>& o)
: hdl(o.ptr()) {}
private:
hdl(handle h) : handle(h) {}
@ -119,14 +118,14 @@ struct object : public handle {
: handle(other.ptr_) {
Py_XINCREF(ptr_);
}
object(object&& other)
object(object&& other) noexcept
: handle(other.ptr_) {
other.ptr_ = nullptr;
}
object& operator=(const object& other) {
return *this = object(other);
}
object& operator=(object&& other) {
object& operator=(object&& other) noexcept {
PyObject* tmp = ptr_;
ptr_ = other.ptr_;
other.ptr_ = tmp;
@ -165,14 +164,14 @@ struct obj : public object {
: object(other.ptr_) {
Py_XINCREF(ptr_);
}
obj(obj&& other)
obj(obj&& other) noexcept
: object(other.ptr_) {
other.ptr_ = nullptr;
}
obj& operator=(const obj& other) {
return *this = obj(other);
}
obj& operator=(obj&& other) {
obj& operator=(obj&& other) noexcept {
PyObject* tmp = ptr_;
ptr_ = other.ptr_;
other.ptr_ = tmp;
@ -503,7 +502,7 @@ struct dict_view : public handle {
return PyDict_Check(h.ptr());
}
bool next(Py_ssize_t* pos, py::handle* key, py::handle* value) {
PyObject *k, *v;
PyObject *k = nullptr, *v = nullptr;
auto r = PyDict_Next(ptr(), pos, &k, &v);
*key = k;
*value = v;