-
Notifications
You must be signed in to change notification settings - Fork 5.5k
header_map: copy constructor for HeaderMapImpl. #4129
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
Changes from 9 commits
01f7423
fffdbbc
b0aa98a
e5ce010
76dfa6c
080a830
f88da73
86cc2fa
d7d65ad
3c525eb
aa008ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -207,6 +207,17 @@ HeaderMapImpl::HeaderEntryImpl::HeaderEntryImpl(const LowerCaseString& key, Head | |
| HeaderMapImpl::HeaderEntryImpl::HeaderEntryImpl(HeaderString&& key, HeaderString&& value) | ||
| : key_(std::move(key)), value_(std::move(value)) {} | ||
|
|
||
| HeaderMapImpl& HeaderMapImpl::operator=(const HeaderMapImpl& rhs) { | ||
| if (&rhs == this) { | ||
| return *this; | ||
| } | ||
|
|
||
| removePrefix(LowerCaseString("")); | ||
| copyFrom(rhs); | ||
|
|
||
| return *this; | ||
| } | ||
|
|
||
| void HeaderMapImpl::HeaderEntryImpl::value(const char* value, uint32_t size) { | ||
| value_.setCopy(value, size); | ||
| } | ||
|
|
@@ -276,21 +287,10 @@ void HeaderMapImpl::appendToHeader(HeaderString& header, absl::string_view data) | |
|
|
||
| HeaderMapImpl::HeaderMapImpl() { memset(&inline_headers_, 0, sizeof(inline_headers_)); } | ||
|
|
||
| HeaderMapImpl::HeaderMapImpl(const HeaderMap& rhs) : HeaderMapImpl() { | ||
| rhs.iterate( | ||
| [](const HeaderEntry& header, void* context) -> HeaderMap::Iterate { | ||
| // TODO(mattklein123) PERF: Avoid copying here is not necessary. | ||
| HeaderString key_string; | ||
| key_string.setCopy(header.key().c_str(), header.key().size()); | ||
| HeaderString value_string; | ||
| value_string.setCopy(header.value().c_str(), header.value().size()); | ||
| HeaderMapImpl::HeaderMapImpl(const HeaderMap& rhs) : HeaderMapImpl() { copyFrom(rhs); } | ||
|
|
||
| static_cast<HeaderMapImpl*>(context)->addViaMove(std::move(key_string), | ||
| std::move(value_string)); | ||
| return HeaderMap::Iterate::Continue; | ||
| }, | ||
| this); | ||
| } | ||
| HeaderMapImpl::HeaderMapImpl(const HeaderMapImpl& rhs) | ||
| : HeaderMapImpl(static_cast<const HeaderMap&>(rhs)) {} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I'm confused. How is this different from what the compiler did before?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was synthesizing the default copy constructor, since we only supplied a constructor for the class we were deriving from AFAICT.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's something more going on here though, witness the build failures. Looking into this now. |
||
|
|
||
| HeaderMapImpl::HeaderMapImpl( | ||
| const std::initializer_list<std::pair<LowerCaseString, std::string>>& values) | ||
|
|
@@ -304,6 +304,22 @@ HeaderMapImpl::HeaderMapImpl( | |
| } | ||
| } | ||
|
|
||
| void HeaderMapImpl::copyFrom(const HeaderMap& header_map) { | ||
| header_map.iterate( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are defining a copy-ctor I think you should also define an assignment operator (and test it). https://en.cppreference.com/w/cpp/language/rule_of_three I see you are defining one in a test subclass, but it doesn't look like you've got one in HeaderMapImpl.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's fair. I will mvoe copy assignment to |
||
| [](const HeaderEntry& header, void* context) -> HeaderMap::Iterate { | ||
| // TODO(mattklein123) PERF: Avoid copying here if not necessary. | ||
| HeaderString key_string; | ||
| key_string.setCopy(header.key().c_str(), header.key().size()); | ||
| HeaderString value_string; | ||
| value_string.setCopy(header.value().c_str(), header.value().size()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Best practice is to use .data() rather than .c_str() here (x2), although I admit in practice I don't see how they can differ :)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no |
||
|
|
||
| static_cast<HeaderMapImpl*>(context)->addViaMove(std::move(key_string), | ||
| std::move(value_string)); | ||
| return HeaderMap::Iterate::Continue; | ||
| }, | ||
| this); | ||
| } | ||
|
|
||
| bool HeaderMapImpl::operator==(const HeaderMapImpl& rhs) const { | ||
| if (size() != rhs.size()) { | ||
| return false; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,8 +46,14 @@ class HeaderMapImpl : public HeaderMap { | |
| static void appendToHeader(HeaderString& header, absl::string_view data); | ||
|
|
||
| HeaderMapImpl(); | ||
| HeaderMapImpl(const std::initializer_list<std::pair<LowerCaseString, std::string>>& values); | ||
| explicit HeaderMapImpl( | ||
| const std::initializer_list<std::pair<LowerCaseString, std::string>>& values); | ||
| HeaderMapImpl(const HeaderMap& rhs); | ||
| // The above constructor for HeaderMap is not an actual copy constructor. This also prevent the | ||
| // implicit move constructor, moving HeaderMapImpl is unsafe (see HeaderList comments). | ||
| HeaderMapImpl(const HeaderMapImpl& rhs); | ||
| // Safe copy assignment; this is highly inefficient, don't use this except for tests. | ||
| HeaderMapImpl& operator=(const HeaderMapImpl& rhs); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry to have more comments on this, but my concern about these changes is that we can accidentally start copying without knowing about it. Is it not possible to make HeaderMapImpl NonCopyable but then add the copy/assignment constructors in TestHeaderMapImpl?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a fair point. Having copy-ctor and assign-operator illegal is fine, but it would be nice to have copyFrom() as an explicit call.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we can do this then. I'm honestly surprised what a mess this all is. If anyone wants ammunition in the language wars that points to C++ being a time suck and unreasonably complicated for even experienced programmers to work with, this is a prime example. |
||
|
|
||
| /** | ||
| * Add a header via full move. This is the expected high performance paths for codecs populating | ||
|
|
@@ -80,6 +86,8 @@ class HeaderMapImpl : public HeaderMap { | |
| size_t size() const override { return headers_.size(); } | ||
|
|
||
| protected: | ||
| void copyFrom(const HeaderMap& rhs); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment that these are protected because they are suspected to be slow, and should only be used in tests? |
||
|
|
||
| struct HeaderEntryImpl : public HeaderEntry, NonCopyable { | ||
| HeaderEntryImpl(const LowerCaseString& key); | ||
| HeaderEntryImpl(const LowerCaseString& key, HeaderString&& value); | ||
|
|
@@ -130,8 +138,15 @@ class HeaderMapImpl : public HeaderMap { | |
| /** | ||
| * List of HeaderEntryImpl that keeps the pseudo headers (key starting with ':') in the front | ||
| * of the list (as required by nghttp2) and otherwise maintains insertion order. | ||
| * | ||
| * Note: the internal iterators held in fields make this unsafe to copy and move, since the | ||
| * reference to end() is not preserved across a move (see Notes in | ||
| * https://en.cppreference.com/w/cpp/container/list/list). The NonCopyable will supress both copy | ||
| * and move constructors/assignment. | ||
| * TODO(htuch): Maybe we want this to movable one day; for now, our header map moves happen on | ||
| * HeaderMapPtr, so the performance impact should not be evident. | ||
| */ | ||
| class HeaderList { | ||
| class HeaderList : NonCopyable { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's fine to block move, but why not just block the implicit move constructor inside
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this change is needed anymore, but fine to keep. |
||
| public: | ||
| HeaderList() : pseudo_headers_end_(headers_.end()) {} | ||
|
|
||
|
|
||
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.
Is this change still needed? It's unclear to me why the compiler won't just pick the other one.
Uh oh!
There was an error while loading. Please reload this page.
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 think so; the implicit copy constructor is deleted, the above constructor for
HeaderMapisn't actually a copy 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.
I see. OK.