perf: add reference_internal policy for const-ref returns#182
Open
jashshah999 wants to merge 1 commit intoborglab:masterfrom
Open
perf: add reference_internal policy for const-ref returns#182jashshah999 wants to merge 1 commit intoborglab:masterfrom
jashshah999 wants to merge 1 commit intoborglab:masterfrom
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.
3 tasks
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)