[PyTorch] Use plain old function pointer for RecordFunctionCallback (reapply)#49408
[PyTorch] Use plain old function pointer for RecordFunctionCallback (reapply)#49408swolchok wants to merge 3 commits intogh/swolchok/47/basefrom
Conversation
…reapply) Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback. Differential Revision: [D25563207](https://our.internmc.facebook.com/intern/diff/D25563207/) **NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D25563207/)! [ghstack-poisoned]
…nCallback (reapply)" Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback. Differential Revision: [D25563207](https://our.internmc.facebook.com/intern/diff/D25563207/) **NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D25563207/)! [ghstack-poisoned]
…reapply) Pull Request resolved: #49408 Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback. ghstack-source-id: 118625284 Differential Revision: [D25563207](https://our.internmc.facebook.com/intern/diff/D25563207/) **NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D25563207/)!
| try { | ||
| if (is_start) { | ||
| ctx = rfcb.start()(rf); | ||
| ctx = rfcb.start() ? rfcb.start()(rf) : nullptr; |
There was a problem hiding this comment.
How about something like?
| ctx = rfcb.start() ? rfcb.start()(rf) : nullptr; | |
| if (auto& start = rcbf.start()) ctx = start(rf) else ctx = nullptr; |
There was a problem hiding this comment.
there's no reason to complicate the code like this; start() is an inlineable load of a single pointer
| if (rfcb.end()) { | ||
| rfcb.end()(rf, ctx.get()); | ||
| } |
There was a problem hiding this comment.
Nit
| if (rfcb.end()) { | |
| rfcb.end()(rf, ctx.get()); | |
| } | |
| if (auto& end = rfcb.end()) { | |
| end(rf, ctx.get()); | |
| } |
| [](const RecordFunction&, ObserverContext*) {}): | ||
| start_(std::move(start)), | ||
| end_(std::move(end)) { | ||
| StartCallback start, |
There was a problem hiding this comment.
Why not pass by reference here?(or keep std::move() when member variable is initialized
| StartCallback start, | |
| const StartCallback& start, |
There was a problem hiding this comment.
because function pointers should be passed by value, just like void* or int, for efficiency reasons. Similarly, there is no need to move them.
| } | ||
|
|
||
| inline const std::function<std::unique_ptr<ObserverContext>(const RecordFunction&)>& start() const { | ||
| inline StartCallback start() const { |
There was a problem hiding this comment.
Why do we want a copy here?
| inline StartCallback start() const { | |
| inline const StartCallback& start() const { |
There was a problem hiding this comment.
because function pointers are cheap to copy, like int or void*
There was a problem hiding this comment.
std::function is not just a function pointer, is it?
There was a problem hiding this comment.
No, but this diff changes StartCallback to be a function pointer above.
| at::RecordFunctionCallback::StartCallback fn = | ||
| [](const at::RecordFunction&) -> std::unique_ptr<at::ObserverContext> { return nullptr; }) { |
There was a problem hiding this comment.
Why do we need a non-trivial initializer here?
| at::RecordFunctionCallback::StartCallback fn = | |
| [](const at::RecordFunction&) -> std::unique_ptr<at::ObserverContext> { return nullptr; }) { | |
| at::RecordFunctionCallback::StartCallback fn = at::RecordFunctionCallback::StartCallback() |
There was a problem hiding this comment.
I just left it the way the existing code had it, don't think it's expensive either way because it's a constant expression (address of a function)
💊 CI failures summary and remediationsAs of commit 62d7d46 (more details on the Dr. CI page): 💚 💚 Looks good so far! There are no failures yet. 💚 💚 This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.Please report bugs/suggestions to the (internal) Dr. CI Users group. This comment has been revised 4 times. |
Codecov Report
@@ Coverage Diff @@
## gh/swolchok/47/base #49408 +/- ##
====================================================
Coverage 80.56% 80.56%
====================================================
Files 1875 1875
Lines 202701 202697 -4
====================================================
- Hits 163309 163308 -1
+ Misses 39392 39389 -3 |
…nCallback (reapply)" Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback. Differential Revision: [D25563207](https://our.internmc.facebook.com/intern/diff/D25563207/) **NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D25563207/)! [ghstack-poisoned]
…reapply) Pull Request resolved: #49408 Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback. ghstack-source-id: 118665808 Differential Revision: [D25563207](https://our.internmc.facebook.com/intern/diff/D25563207/) **NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D25563207/)!
|
This pull request has been merged in 22c6daf. |
…reapply) (pytorch#49408) Summary: Pull Request resolved: pytorch#49408 Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback. ghstack-source-id: 118665808 Test Plan: Wait for GitHub CI since we had C++14-specific issues with this one in previous PR pytorch#48629 Reviewed By: malfet Differential Revision: D25563207 fbshipit-source-id: 6a2831205917d465f8248ca37429ba2428d5626d
Stack from ghstack:
Nearly every non-test callsite doesn't need to capture any variables anyway, and this saves 48 bytes per callback.
Differential Revision: D25563207
NOTE FOR REVIEWERS: This PR has internal Facebook specific changes or comments, please review them on Phabricator!