Added const references to various function parameters#52
Added const references to various function parameters#52ivancich merged 1 commit intoceph:masterfrom
Conversation
sim/src/sim_client.h
Outdated
| public: | ||
|
|
||
| SimulatedClient(ClientId _id, | ||
| SimulatedClient(const ClientId& _id, |
There was a problem hiding this comment.
If these "Id" types are actually just ints, you're probably better off passing them as const values rather than const references.
There was a problem hiding this comment.
Thanks for pointing it out, but ClientId is not a primitive type.
There was a problem hiding this comment.
No problem-- the other thing I should point out after I hem and haw with myself is that with const& in tricker situations (like here, where I wrongly assumed it was as per the definition at "https://github.com/Rubonnek/dmclock/blob/41d97df5493b23bcbb8c3aaf53934231c82e48e4/sim/src/sim_recs.h"), any errors will be on the side of good. ;-)
There was a problem hiding this comment.
Welp, now I think you are right. I first saw the ClientId when I scanned the whole ceph repository with cppcheck which included this dmclock repository. I thought ClientId was also defined as a struct here, but it's not, it's just what you pointed out.
I also didn't notice the inconclusive flag from cppcheck. That usually brings up false positives and this change is included with that flag.
Thanks for the review! I'll drop these changes, and the other inconclusive ones.
| template<typename D> | ||
| RunEvery(D _wait_period, | ||
| std::function<void()> _body) : | ||
| const std::function<void()>& _body) : |
There was a problem hiding this comment.
Same here with a function pointer. As a general rule if you're using small/built-in types (ints, chars, iterators, etc.) usually it's best to pass them by const value if you in effect want to make a copy.
There was a problem hiding this comment.
I understand, but std::function is not the same as a function pointer as far as I know. They are similar indeed, but a pointer cannot hold data other than the address it points to. An std::function can copy the function which this const reference will avoid if its not necessary.
There was a problem hiding this comment.
std::function<> is a bit of a magical beast, as you're right, it's not the same as a function pointer. Instead, it's wrapping a Callable. I looked into this in more detail, and the answer is interesting: the code generated for a function pointer or reference-to-lambda is impressively concise. So the real question is "what happens when we copy a std::function that's pointing to some big Callable object" and the answer is... it will have to do the right thing and make a copy. Which could be expensive.
So, one of two things needs to happen in that case: either the object needs to be moved, or the cost of that copy is something that's expected. So, you're right: if you want to avoid that happening at all, passing by const& looks like the way to go! Thanks for giving me the opportunity to look into it more.
There was a problem hiding this comment.
Anytime! And thanks for the explanation! I was going to do that myself since I really didn't recall the details.
41d97df to
6beaf6d
Compare
6beaf6d to
3621fc7
Compare
Signed-off-by: Wilson E. Alvarez <wilson.e.alvarez1@gmail.com>
3621fc7 to
2358ec4
Compare
This pull request adds a small enhancement by avoiding copying data when unnecessary and mitigates the following cppcheck messages: