Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use character-by-character comparison for comparison operators #112

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 4 commits into from
Jul 25, 2018

Conversation

csk-ableton
Copy link
Contributor

This follows the standard proposal in section [uri.header-synopsis.equality-comparison].

Otherwise the type would do expensive allocation due to normalization of the
URIs when used in containers or algorithms.

The operator functions have been made friend functions of the uri class
to take advantage of comparing the underlying string_view instead of
re-implementing the behavior based on iterators.

I adapted the less-than operator test to use a more interesting example as I did a mistake initially.

@glynos I'm not sure you want these kinds of API breaking changes. I still thought I create a PR to discuss.

This follows the proposal in section [uri.header-synopsis.equality-comparison].

Otherwise the type would do expensive allocation due to normalization of the
URIs when used in containers or algorithms.

The operator functions have been made friend functions of the uri class
to take advantage of comparing the underlying string_view instead of
re-implementing the behavior based on iterators.
Copy link
Member

@glynos glynos left a comment

Choose a reason for hiding this comment

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

Thanks for identifying this problem and creating a PR.

If I understand what you're trying to do (make the equality operator more efficient by comparing the underlying string_view), I would think it might be much simpler to create an accessor for the view:

string_view uri::string_view() const noexcept {
  return view_;
}

(cf. also string(), wstring(), ustring16() etc. member functions)

Then you can implement the equality and comparison operators in terms of this and you don't need to make the operators friends.

Otherwise I agree with comparing the views directly and not calling the compare function.

Did you try to measure the performance?

@@ -70,7 +70,7 @@ TEST(uri_comparison_test, inequality_test) {
TEST(uri_comparison_test, less_than_test) {
// lhs is lexicographically less than rhs
network::uri lhs("http://www.example.com/");
network::uri rhs("http://www.example.org/");
network::uri rhs("http://www.example.com/path");
Copy link
Member

Choose a reason for hiding this comment

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

Was there an issue with the original test? I'd prefer if you added this as a new test case instead of modifying an existing one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah sure I should have added that case instead of replacing the existing one.

I did a mistake when re-implementing operator< before deciding to use the string_views operators. I think now that this is using string_view::operator< there is no need to increase test coverage here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted in 1a55ecf

@csk-ableton
Copy link
Contributor Author

Thank you for the review! Introducing a string_view accessor is unfortunately in conflict with the string_view member type. I also tried to stick with the proposals interface.

I still went with your solution in c0de1e3 and called the accessor view for the lack of a better name. Let me know what you think.

Copy link
Member

@glynos glynos left a comment

Choose a reason for hiding this comment

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

LGTM

@glynos glynos merged commit c1eaf68 into cpp-netlib:master Jul 25, 2018
@csk-ableton
Copy link
Contributor Author

I didn't rebase, so now cpp-netlib:master contains the fixup commits. Might make sense to squash them.

@glynos
Copy link
Member

glynos commented Jul 25, 2018

I noticed that after I merged them... I can only do that later.

Thanks again for the PR. What are you using the library for?

@csk-ableton
Copy link
Contributor Author

I just used it to represent URIs in our code base for referencing resources. Given it was based on the standard proposal and already ran through some review it made sense to not re-invent the wheel.

Regarding your benchmark question: I didn't write benchmarks but previously the compare function normalized both URIs which allocates memory and re-parser the whole thing, so this is probably magnitudes faster now.

glynos added a commit that referenced this pull request Jul 25, 2018
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.

2 participants