Skip to content
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

Speed up rclpy loggers #84

Merged
merged 1 commit into from
Apr 4, 2024
Merged

Conversation

mhidalgo-bdai
Copy link
Contributor

@mhidalgo-bdai mhidalgo-bdai commented Mar 29, 2024

This patch introduces an alternative implementation of the rclpy.logging.RcutilsLogger that will cache log call configurations. This has been observed to reduce the average logging time significantly.

As to why this is the case, other than the obvious advantage of executing code on first call rather than on every call, I suspect stack frame inspection in Python may be taking some amount of time (as it fetches code from source files by default when populating frame info and traceback objects, see here).

@mhidalgo-bdai mhidalgo-bdai self-assigned this Mar 29, 2024
@robodreamer
Copy link

Thanks for proposing a quick alternative solution! Would you mind elaborating on what the issues in the original code were and how the new change addresses them?

@mhidalgo-bdai mhidalgo-bdai force-pushed the mhidalgo-bdai/speed-up-logging branch from 80bb4aa to 1cc42c5 Compare April 3, 2024 19:34
@mhidalgo-bdai mhidalgo-bdai force-pushed the mhidalgo-bdai/speed-up-logging branch from 1cc42c5 to b495bf3 Compare April 3, 2024 19:53
@mhidalgo-bdai mhidalgo-bdai marked this pull request as ready for review April 3, 2024 19:54
@mhidalgo-bdai
Copy link
Contributor Author

mhidalgo-bdai commented Apr 3, 2024

Thanks for proposing a quick alternative solution! Would you mind elaborating on what the issues in the original code were and how the new change addresses them?

I updated the PR description. I think it's a combination of factors that can be summarized as "too much work on every log call". This patch addresses this issue by "doing less work and only on first log call".

throttle_duration_sec: typing.Optional[float] = None,
throttle_time_source: typing.Optional[Clock] = None,
skip_first: typing.Optional[bool] = None,
once: typing.Optional[bool] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between None and False?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this context, none. I annotate optional arguments as optional purely out of habit (and for the sake of readability).

Copy link
Collaborator

@amessing-bdai amessing-bdai Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, not going to block on this.

I think whether bool = False or Optional[bool] = None is more readable is another discussion lol. I do think there is a difference between Optional and has a default value.

Copy link

@robodreamer robodreamer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting a quick workaround! I wonder if we would like to contribute this change back to ROS2 as well?

Copy link

I am looking forward to seeing this change live from the bdai container soon! I had to suppress a lot of log messages currently to get around the performance issue.

return log


class MemoizingRcutilsLogger:
Copy link

@robodreamer robodreamer Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This this class name a standard one? The name (memoizing) sounds a bit new to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not standard, no. Memoizing is me adjectivizing memoization.

@mhidalgo-bdai
Copy link
Contributor Author

I wonder if we would like to contribute this change back to ROS2 as well?

I'm onboard.

I am looking forward to seeing this change live from the bdai container soon! I had to suppress a lot of log messages currently to get around the performance issue.

Note you will have to wrap your logger, like logger = as_memoizing_logger(node.get_logger()). We could inject it for our bdai_ros2_wrappers.node.Node subclass, but I suspect that may create some confusion when people start mixing up our nodes with regular nodes (and ask why the logger in their regular node has awful performance).

@mhidalgo-bdai mhidalgo-bdai merged commit 25ba838 into main Apr 4, 2024
3 checks passed
@mhidalgo-bdai mhidalgo-bdai deleted the mhidalgo-bdai/speed-up-logging branch April 4, 2024 20:27
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.

3 participants