-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Clarify publish behavior for shared_ptr and unique_ptr #5966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yuchen966
wants to merge
2
commits into
ros2:rolling
Choose a base branch
from
Yuchen966:rolling
base: rolling
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+29
−8
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,7 +163,26 @@ In this case, since they only come once per second, usually only the first messa | |
| Finally, you can see that "Published message..." and "Received message ..." lines with the same value also have the same address. | ||
| This shows that the address of the message being received is the same as the one that was published and that it is not a copy. | ||
| This is because we're publishing and subscribing with ``std::unique_ptr``\ s which allow ownership of a message to be moved around the system safely. | ||
| You can also publish and subscribe with ``const &`` and ``std::shared_ptr``, but zero-copy will not occur in that case. | ||
|
|
||
| Understanding publish() behavior with different message types | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| The behavior of the ``publish()`` function varies depending on the message type used: | ||
|
|
||
| **std::shared_ptr messages:** | ||
| - Cannot be published directly | ||
| - This is a limitation of the current ROS 2 implementation | ||
|
|
||
| **std::unique_ptr messages:** | ||
| - Zero-copy transfer is achieved for the first subscriber | ||
| - Additional subscribers receive copies of the message | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these statements are not really correct. |
||
| - Ownership is moved from publisher to the first subscriber | ||
| - This is the recommended approach for intra-process communication | ||
|
|
||
| **Raw messages** (both ``msg`` and ``std::move(msg)``): | ||
| - Both result in copying | ||
| - No zero-copy benefits are obtained | ||
| - Use ``const &`` for subscription callbacks with raw messages | ||
|
|
||
| The cyclic pipeline demo | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
@@ -372,15 +391,17 @@ To understand why this is happening consider the graph's topology: | |
| -> image_view_node2 | ||
|
|
||
| The link between the ``camera_node`` and the ``watermark_node`` can use the same pointer without copying because there is only one intra process subscription to which the message should be delivered. | ||
| But for the link between the ``watermark_node`` and the two image view nodes the relationship is one to many, so if the image view nodes were using ``unique_ptr`` callbacks then it would be impossible to deliver the ownership of the same pointer to both. | ||
| It can be, however, delivered to one of them. | ||
| Which one would get the original pointer is not defined, but instead is simply the last to be delivered. | ||
| But for the link between the ``watermark_node`` and the two image view nodes the relationship is one to many. | ||
|
|
||
| When a ``unique_ptr`` message is published and there are multiple intra-process subscribers, the behavior is as follows: | ||
| - The last subscriber receives the original pointer (zero-copy) | ||
| - Additional subscribers receive copies of the message | ||
| - This applies regardless of whether subscribers use ``unique_ptr`` or ``shared_ptr`` callbacks | ||
|
|
||
| Note that the image view nodes are not subscribed with ``unique_ptr`` callbacks. | ||
| Instead they are subscribed with ``const shared_ptr``\ s. | ||
| This means the system deliveres the same ``shared_ptr`` to both callbacks. | ||
| When the first intraprocess subscription is handled, the internally stored ``unique_ptr`` is promoted to a ``shared_ptr``. | ||
| Each of the callbacks will receive shared ownership of the same message. | ||
| Instead they are subscribed with ``const shared_ptr`` s. | ||
| This means the system delivers the original message to the first callback and copies to subsequent callbacks. | ||
| When the first intraprocess subscription is handled, the internally stored ``unique_ptr`` is promoted to a ``shared_ptr`` for delivery to multiple subscribers. | ||
|
|
||
| Pipeline with interprocess viewer | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra '~'?