You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using the tempoline class to resolve the inheritance between C++ and Python and notice unnecessary overhead from PYBIND11_OVERRIDE.
Assuming I have a base class A in C++, which I have a evaluate method:
classA {
public:virtualvoidevaluate();
}
and the tempoline class
template <classBase = A>
classPyA : publicBase {
public:/* Inherit the constructors */using Base::Base;
voidevaluate() final { PYBIND11_OVERRIDE(void, Base, evaluate) }
};
I noticed that every time the PYBIND11_OVERRIDE is involved, it will try to check in python side and try to get the python evaluate method if exists.
I think I can totally cache the python evaluate method like below and I see noticeable performance gain due to it.
template <classBase = A>
classPyA : publicBase {
public:/* Inherit the constructors */using Base::Base;
voidevaluate() final {
if (!cached_func_.has_value()) [[unlikely]] {
pybind11::gil_scoped_acquire gil;
cached_func_ = pybind11::get_override(static_cast<const Base*>(this), "evaluate");
}
pybind11::function& override = cached_func_.value();
if (override) {
pybind11::gil_scoped_acquire gil;
override();
return;
}
returnBase::evaluate(event);
}
~PyA() override {
if (cached_func_.has_value()) {
pybind11::gil_scoped_acquire gil;
cached_func_ = std::nullopt;
}
}
private:
std::optional<pybind11::function> cached_func_{};
};
So is there any reason why pybind11 doesn't do the caching? The only reason I can think is that the evaluate method could be changed dynamically in python side, so we need to get it every time.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm using the tempoline class to resolve the inheritance between C++ and Python and notice unnecessary overhead from
PYBIND11_OVERRIDE
.Assuming I have a base class A in C++, which I have a
evaluate
method:and the tempoline class
I noticed that every time the
PYBIND11_OVERRIDE
is involved, it will try to check in python side and try to get the pythonevaluate
method if exists.I think I can totally cache the python
evaluate
method like below and I see noticeable performance gain due to it.So is there any reason why pybind11 doesn't do the caching? The only reason I can think is that the
evaluate
method could be changed dynamically in python side, so we need to get it every time.Beta Was this translation helpful? Give feedback.
All reactions