Skip to content

perf: add reference_internal policy for const-ref returns#182

Open
jashshah999 wants to merge 1 commit intoborglab:masterfrom
jashshah999:perf/reference-return-policy
Open

perf: add reference_internal policy for const-ref returns#182
jashshah999 wants to merge 1 commit intoborglab:masterfrom
jashshah999:perf/reference-return-policy

Conversation

@jashshah999
Copy link
Copy Markdown

Summary

  • Methods returning const T& now emit py::return_value_policy::reference_internal in the generated pybind11 bindings
  • The generated lambda uses -> const auto& trailing return type to preserve the reference semantics
  • This avoids unnecessary copies when Python accesses C++ members by const reference, keeping the returned reference tied to the parent object's lifetime

Motivation

Without this policy, pybind11 defaults to copy for returned references from lambdas (since the lambda return type is deduced as a value). This causes:

  1. Unnecessary deep copies of potentially large objects (matrices, vectors)
  2. Surprising semantics where modifying the "reference" in Python doesn't affect the C++ object

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: Detect is_ref on the return type and emit -> const auto& + py::return_value_policy::reference_internal for instance methods
  • tests/expected/python/class_pybind.cpp: Updated expected output for return_vector2 and return_matrix2 const-ref overloads

Test plan

  • All existing tests pass (pytest tests/test_pybind_wrapper.py - 9/9 passing)
  • Verify with a downstream project (e.g. GTSAM) that const-ref accessors no longer copy

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant