-
-
Notifications
You must be signed in to change notification settings - Fork 102
feat: Optimize to_string
#959
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
Conversation
this step worked in my local environment . macOS (Installation) / macos-build (OFF, macos-15) (pull_request) ➜ ada git:(mert/fix/warnings) cmake --build build -j=3
[ 16%] Built target simdjson
[ 16%] Built target ada
[ 16%] Built target gtest
[ 21%] Built target gtest_main
[ 32%] Built target basic_fuzzer
[ 35%] Built target ada-singleheader-lib
[ 37%] Built target ada-singleheader-files
[ 43%] Built target wpt_urlpattern_tests
[ 48%] Built target wpt_url_tests
[ 54%] Built target url_components
[ 59%] Built target ada_c
[ 70%] Built target basic_tests
[ 70%] Built target from_file_tests
[ 75%] Built target url_search_params
[ 91%] Built target demo
[ 91%] Built target demo_no_url_pattern
[100%] Built target cdemo
➜ ada git:(mert/fix/warnings) |
include/ada/encoding_type.h
Outdated
@@ -25,7 +25,7 @@ enum class encoding_type { | |||
/** | |||
* Convert a encoding_type to string. | |||
*/ | |||
ada_warn_unused std::string to_string(encoding_type type); | |||
ada_warn_unused const std::string to_string(encoding_type type); |
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.
This changes the API, and if we do change the API, we might as well return a std::string_view instance. This will improve the efficiency of the code.
Note that there is nothing wrong with the existing code, except for the fact that it creates a new instance of an std::string which is wasteful (which the new code is likely to do as well).
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.
Thanks. I've applied that change.
src/implementation.cpp
Outdated
@@ -28,7 +28,7 @@ template ada::result<url_aggregator> parse<url_aggregator>( | |||
std::string href_from_file(std::string_view input) { | |||
// This is going to be much faster than constructing a URL. | |||
std::string tmp_buffer; | |||
std::string_view internal_input; | |||
std::string_view internal_input{}; |
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.
I don't understand the change here, can you elaborate on your purpose?
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.
warning: variable 'internal_input' is not initialized [cppcoreguidelines-init-variables]
31 | std::string_view internal_input;
| ^
compiler was warning about an uninitialized variable. I tried to fix this warning by initializing internal_input with an empty string_view ({}), following the C++ Core Guidelines to initialize every variable
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.
@mertcanaltin Can you elaborate on why internal_input
is uninitialized after... this line...
std::string href_from_file(std::string_view input) {
std::string tmp_buffer;
std::string_view internal_input;
...
}
This looks like textbook C++:
class A {...};
void myfunc() {
A a; // call default constructor of A
}
Please don't argue from the warning message of some tool. There is an endless stream of warnings one can get from static analyzers.
The std::string_view
class has a default constructor.
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.
You are absolutely right, std::string_view
has a default constructor and internal_input
is indeed created by default.
intention behind std::string_view internal_input{};
was primarily to explicitly state the initial (empty) state of the variable and satisfy the cppcoreguidelines-init-variables
static analysis check, which prefers explicit initialization even if a default constructor is present. This was a stylistic choice for clarity and to silence this particular analyzer warning, rather than to address an actual runtime error, but I agree with what you say that this is an tool (clang-tidy) tool's warning.
here I get the code back thank you very much again.
85c411c
to
16947ed
Compare
to_string
and resolve compiler warnings
16947ed
to
3ba32f4
Compare
to_string
and resolve compiler warningsto_string
cac84fe
to
57b1cdf
Compare
optimize to_string