Skip to content

Update just pycapsule lib to use pybind11#652

Merged
sloretz merged 3 commits intomasterfrom
pybind11_just_pycapsule_lib
Feb 23, 2021
Merged

Update just pycapsule lib to use pybind11#652
sloretz merged 3 commits intomasterfrom
pybind11_just_pycapsule_lib

Conversation

@sloretz
Copy link
Copy Markdown
Contributor

@sloretz sloretz commented Dec 22, 2020

Follow up test from #651

This updates the _rclpy_pycapsule lib to use Pybind11 instead of raw CPython APIs where possible. Pybind11 would enable rclpy to not use pycapsule at all if we made some thin C++ classes, but that would take a bit of refactoring. This PR looks at translating the APIs to pybind11 without any changes. This might be a good intermediate step in moving rclpy to pybind11.

@cottsay @IanTheEngineer FYI

@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Dec 22, 2020

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Dec 22, 2020

Code size increase

Build type Debug

The size of the pybind11 version is almost 50 times bigger than master

master

$ ls -l _rclpy_pycapsule.cpython-38-x86_64-linux-gnu.so
-rw-r--r-- 1 sloretz sloretz 36608 Dec 22 14:54 _rclpy_pycapsule.cpython-38-x86_64-linux-gnu.so

this branch

$ ls -l _rclpy_pycapsule.cpython-38-x86_64-linux-gnu.so 
-rw-r--r-- 1 sloretz sloretz 1810720 Dec 22 14:52 _rclpy_pycapsule.cpython-38-x86_64-linux-gnu.so

Build type None

The size of the pybind11 version is a little over 13 times bigger than master

master

$ ls -l _rclpy_pycapsule.cpython-38-x86_64-linux-gnu.so
-rw-r--r-- 1 sloretz sloretz 17408 Dec 22 14:58 _rclpy_pycapsule.cpython-38-x86_64-linux-gnu.so

this branch

$ ls -l _rclpy_pycapsule.cpython-38-x86_64-linux-gnu.so
-rw-r--r-- 1 sloretz sloretz 232800 Dec 22 14:59 _rclpy_pycapsule.cpython-38-x86_64-linux-gnu.so

Build type Release

The size of the pybind11 version is a little over 5 times bigger than master

master

$  ls -l _rclpy_pycapsule.cpython-38-x86_64-linux-gnu.so
-rw-r--r-- 1 sloretz sloretz 17400 Dec 22 15:02 _rclpy_pycapsule.cpython-38-x86_64-linux-gnu.so

this branch

$ ls -l _rclpy_pycapsule.cpython-38-x86_64-linux-gnu.so
-rw-r--r-- 1 sloretz sloretz 93304 Dec 22 15:01 _rclpy_pycapsule.cpython-38-x86_64-linux-gnu.so

@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Jan 6, 2021

@azeey FYI

@clalancette
Copy link
Copy Markdown
Contributor

A 5 times code size increase (at best) seems like a lot for what is essentially a lateral move for this code. If you are using the Python bindings at all, code size and performance are probably not your top concern, but it is still very large. The question I have to ask is: given that we already have a working version of the hand-coded version of this library, does the move to pybind11 still make sense? That is, does the move have enough benefits that we are willing to take a 5-50 times increase in compiled code size?

I don't have a good answer for that, and I'll leave it to the maintainers of rclpy to answer. But I do think it should be considered.

@ivanpauno
Copy link
Copy Markdown
Member

does the move to pybind11 still make sense?

IMO, one of the biggest pain points of the current implementation is object lifetimes.
RCL objects may keep a reference of other rcl objects and then you need to destroy them in order, and when creating bindings to a garbage collected language that's a nightmare.

If we move to pybind11 and don't use capsules at all, pybind11 smart pointers support might make things a lot easier.

@emersonknapp
Copy link
Copy Markdown
Collaborator

I'll also note that we could do a C++ implementation, and manage object lifetimes via C++ destructors - there's nothing forcing us to do a pure-c implementation as far as I can tell. If we created simple C++ wrapper objects for the CPython API objects - it would probably remove a lot of the lifetime management code. These might be easier to expose with pybind, as well.

Has it been considered to just selectively expose rclcpp to Python to implement rclpy functionality? This may have drawbacks I haven't considered.

@sloretz sloretz force-pushed the pybind11_just_pycapsule_lib branch from fb82e2a to 9132234 Compare January 21, 2021 21:43
@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Jan 21, 2021

Rebased on #667

I'll also note that we could do a C++ implementation, and manage object lifetimes via C++ destructors

@emersonknapp Definitely. Converting 1-to-1 is a hopefully quick process that will uncover any spots that are sensitive to pybind11's overhead. Using C++ and RAII comes after and adds a lot of benefit. You might be interested in the doc in #665.

@sloretz sloretz changed the base branch from master to add_pybind11_dependency January 21, 2021 21:58
@sloretz sloretz mentioned this pull request Jan 21, 2021
34 tasks
@sloretz sloretz force-pushed the add_pybind11_dependency branch from a11af4b to 850e84b Compare January 29, 2021 01:28
@sloretz sloretz force-pushed the add_pybind11_dependency branch 2 times, most recently from c04dc58 to 674b02c Compare February 12, 2021 17:06
@delete-merged-branch delete-merged-branch bot deleted the branch master February 12, 2021 17:07
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
Signed-off-by: Shane Loretz <sloretz@osrfoundation.org>
@sloretz sloretz force-pushed the pybind11_just_pycapsule_lib branch from 9132234 to 7b48d74 Compare February 12, 2021 17:32
@sloretz sloretz changed the base branch from add_pybind11_dependency to master February 12, 2021 17:32
@sloretz sloretz marked this pull request as ready for review February 12, 2021 17:33
@sloretz
Copy link
Copy Markdown
Contributor Author

sloretz commented Feb 23, 2021

CI (build: --packages-up-to rclpy test: --packages-select rclpy)

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@sloretz sloretz merged commit d916c62 into master Feb 23, 2021
@delete-merged-branch delete-merged-branch bot deleted the pybind11_just_pycapsule_lib branch February 23, 2021 19:02
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.

5 participants