-
Notifications
You must be signed in to change notification settings - Fork 912
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
Improve const correctness #3083
Conversation
This PR may bring feature or behavior changes in the Falco engine and may require the engine version to be bumped. Please double check userspace/engine/falco_engine_version.h file. See versioning for FALCO_ENGINE_VERSION. /hold |
@@ -33,7 +33,7 @@ static std::vector<std::string> rule_matching_names = { | |||
"all" | |||
}; | |||
|
|||
bool falco_common::parse_priority(std::string v, priority_type& out) | |||
bool falco_common::parse_priority(const std::string& v, priority_type& out) |
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.
@federico-sysdig may we summon you again to help decide on a style more uniformly? For example have gotten feedback both ways, pass by value vs const ref ... and lately we transitioned to using std::string_view in libs. Thank you 🙏 .
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.
TL;DR:
Pass by value all types that are cheap to copy (e.g. built-in types, but not only...) and pass by const reference the other ones.
There are other cases where you'll here advice to pass-by-value and then move or pass-by-rvalue-reference. I won't discuss them here. I think the matter gets more confused and difficult and a simple rule that is good enough is a superior choice. So, these cases apart an std::string
should not be passed by value as it might not be "cheap" to copy, meaning it might require a heap allocation (unless it's very short, but small-string-optimization is another topic I'd leave out of the discussion).
Regarding std::string_view
, the type is not built-in, clearly, but it's implemented as a pair of pointers (or a pointer and an integer, which is the same), thus it is cheap to copy and should be passed by value. See it as a more structured way to pass (char* str, int strlen)
, like we often do in C.
"Style" might come into play on the choice between const std::string&
or std::string_view
, but the latter came only recently with the library moving to C++17, so you won't see it very much. It is more efficient at times when the caller of the function uses a string literal as no temporary string gets created, so it is a better choice which I fully endorse, but, beware, it cannot be always used for passing the string buffer to a C function as it gives no guarantee of being null-terminated and it has some gotchas if you use it with an associative container whose key is an std::string
(e.g. std::map<std::string, widget> C;
and then, later on, C.find(mystringview)
).
Briefly put, passing strings by const reference is fine, passing them through a std::string_view
is even better, if you can afford it.
@@ -128,7 +128,7 @@ std::string falco_formats::format_event(sinsp_evt *evt, const std::string &rule, | |||
line = full_line; | |||
} | |||
|
|||
return line.c_str(); | |||
return line; |
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.
ohhh
/milestone 0.38.0 |
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.
Thank you!
LGTM label has been added. Git tree hash: 0bbfbb72f405468c07f8c4a6ece269fc240c9538
|
Uh needs a rebase :) |
b0cc26a
to
2e0550d
Compare
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.
/approve
LGTM label has been added. Git tree hash: f4dab3003225f41dd0be2a91582bd0085d850434
|
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.
/approve
CI seems to be in a bad state right now all over, we will check tomorrow. |
@incertum it's innocent (in this case), I broke the test with too much constness. |
Reported by cppcheck Signed-off-by: Samuel Gaist <[email protected]>
I may have found something that could be refactored in a followup PR. In falco_engine::describe_rule, does an empty pointer and an empty string represent the same thing ? I would say yes since it would not make sense to have a rule indexed at an empty string but it also does not seem to be forbidden. |
2e0550d
to
7d1e379
Compare
@jasondellaluce and @mstemm, would you have any insights since you worked a lot on those parts? |
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.
/approve
LGTM label has been added. Git tree hash: 9122969f58f3d490f292659289d553967e3e4e60
|
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.
/approve
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: FedeDP, incertum, sgaist The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/unhold |
What type of PR is this?
/kind cleanup
Any specific area of the project related to this PR?
/area engine
What this PR does / why we need it:
This PR refactors the code to make use of const references rather than just reference or copy when applicable.
It also applies similar recommendations for pointers.
They were detected by cppcheck.
Which issue(s) this PR fixes:
Fixes #3082
Special notes for your reviewer:
Does this PR introduce a user-facing change?: