Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Added const references to various function parameters#52

Merged
ivancich merged 1 commit intoceph:masterfrom
Rubonnek:pass-by-reference
Mar 30, 2018
Merged

Added const references to various function parameters#52
ivancich merged 1 commit intoceph:masterfrom
Rubonnek:pass-by-reference

Conversation

@Rubonnek
Copy link
Contributor

@Rubonnek Rubonnek commented Mar 28, 2018

This pull request adds a small enhancement by avoiding copying data when unnecessary and mitigates the following cppcheck messages:

[sim/src/ConfUtils.cc:41]: (performance) Function parameter 'val_' should be passed by reference.
[sim/src/ConfUtils.cc:42]: (performance) Function parameter 'newsection_' should be passed by reference.
[sim/src/ConfUtils.cc:42]: (performance) Function parameter 'comment_' should be passed by reference.
[sim/src/ConfUtils.h:40]: (performance) Function parameter 'val_' should be passed by reference.
[sim/src/ConfUtils.h:41]: (performance) Function parameter 'newsection_' should be passed by reference.
[sim/src/ConfUtils.h:41]: (performance) Function parameter 'comment_' should be passed by reference.
[sim/src/str_list.h:96]: (performance) Function parameter 'sep' should be passed by reference.
[sim/src/simulate.h:308]: (performance) Function parameter 'time_unit' should be passed by reference.
[sim/src/simulate.h:354]: (performance) Function parameter 'time_unit' should be passed by reference.
[support/src/run_every.h:55]: (performance) Function parameter '_body' should be passed by reference.

public:

SimulatedClient(ClientId _id,
SimulatedClient(const ClientId& _id,
Copy link

Choose a reason for hiding this comment

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

If these "Id" types are actually just ints, you're probably better off passing them as const values rather than const references.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing it out, but ClientId is not a primitive type.

Copy link

Choose a reason for hiding this comment

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

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. ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link

Choose a reason for hiding this comment

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

Glad to be of help!

template<typename D>
RunEvery(D _wait_period,
std::function<void()> _body) :
const std::function<void()>& _body) :
Copy link

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Anytime! And thanks for the explanation! I was going to do that myself since I really didn't recall the details.

@Rubonnek Rubonnek force-pushed the pass-by-reference branch from 41d97df to 6beaf6d Compare March 28, 2018 18:17
@Rubonnek Rubonnek force-pushed the pass-by-reference branch from 6beaf6d to 3621fc7 Compare March 28, 2018 23:28
Signed-off-by: Wilson E. Alvarez <wilson.e.alvarez1@gmail.com>
@Rubonnek Rubonnek force-pushed the pass-by-reference branch from 3621fc7 to 2358ec4 Compare March 28, 2018 23:28
@Rubonnek Rubonnek changed the title Used const reference where possible Added const references to various function parameters Mar 28, 2018
Copy link

@chardan chardan left a comment

Choose a reason for hiding this comment

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

+1

@ivancich ivancich merged commit c4334e5 into ceph:master Mar 30, 2018
@Rubonnek Rubonnek deleted the pass-by-reference branch March 30, 2018 21:27
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants