perf: add reference_internal policy for const-ref returns#182
Conversation
Methods returning const T& now generate lambdas with `-> const auto&` return type and `py::return_value_policy::reference_internal`, avoiding unnecessary copies and keeping the reference tied to the parent object lifetime.
dellaert
left a comment
There was a problem hiding this comment.
@varunagrawal might be too busy - sorry. My comment is: please add a unit test to (a) show me how emitted code differs, and (b) verify that for posterity.
Adds test_const_ref_return_policy that explicitly checks: - Methods returning const T& emit `-> const auto&` trailing return - Methods returning const T& emit `py::return_value_policy::reference_internal` - Methods returning by value (T, not T&) do NOT get the policy
|
Thanks for adding the test; it covers the intended One issue with the implementation: the generator currently checks only Could you either gate this path on both |
|
Actually, I will add that in a follow up PR. Don;t pull wrap into gtsam yet. |
Summary
const T&now emitpy::return_value_policy::reference_internalin the generated pybind11 bindings-> const auto&trailing return type to preserve the reference semanticsMotivation
Without this policy, pybind11 defaults to
copyfor returned references from lambdas (since the lambda return type is deduced as a value). This causes:With
reference_internal, pybind11 returns a reference and ties the Python reference's lifetime to the parent object, matching C++ semantics.Changes
gtwrap/pybind_wrapper.py: Detectis_refon the return type and emit-> const auto&+py::return_value_policy::reference_internalfor instance methodstests/expected/python/class_pybind.cpp: Updated expected output forreturn_vector2andreturn_matrix2const-ref overloadsTest plan
pytest tests/test_pybind_wrapper.py- 9/9 passing)