Skip to content

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

Merged
merged 1 commit into from
Jun 14, 2025
Merged

Conversation

mertcanaltin
Copy link
Contributor

@mertcanaltin mertcanaltin commented Jun 14, 2025

optimize to_string

@mertcanaltin
Copy link
Contributor Author

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) 

@anonrig anonrig requested a review from lemire June 14, 2025 14:29
@@ -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);
Copy link
Member

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

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. I've applied that change.

@@ -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{};
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

@lemire lemire Jun 14, 2025

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.

Copy link
Contributor Author

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.

@mertcanaltin mertcanaltin changed the title fix: resolve compiler warnings in implementation.cpp feat: Optimize to_string and resolve compiler warnings Jun 14, 2025
@anonrig anonrig requested a review from lemire June 14, 2025 16:21
@mertcanaltin mertcanaltin changed the title feat: Optimize to_string and resolve compiler warnings feat: Optimize to_string Jun 14, 2025
@anonrig anonrig merged commit dfed89b into ada-url:main Jun 14, 2025
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants