Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions source/common/http/header_map_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Member

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.

@htuch htuch Aug 13, 2018

Copy link
Copy Markdown
Member Author

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 HeaderMap isn't actually a copy constructor.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see. OK.

: HeaderMapImpl(static_cast<const HeaderMap&>(rhs)) {}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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)
Expand All @@ -304,6 +304,22 @@ HeaderMapImpl::HeaderMapImpl(
}
}

void HeaderMapImpl::copyFrom(const HeaderMap& header_map) {
header_map.iterate(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, that's fair. I will mvoe copy assignment to HeaderMapImpl.

[](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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There is no .data() on header.value().


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;
Expand Down
19 changes: 17 additions & 2 deletions source/common/http/header_map_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -80,6 +86,8 @@ class HeaderMapImpl : public HeaderMap {
size_t size() const override { return headers_.size(); }

protected:
void copyFrom(const HeaderMap& rhs);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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);
Expand Down Expand Up @@ -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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 NonCopyable? I realize it's called NonCopyable but I'm pretty sure in all cases where we use that we also don't want move?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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()) {}

Expand Down
15 changes: 15 additions & 0 deletions test/common/http/header_map_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -853,5 +853,20 @@ TEST(HeaderMapImplTest, PseudoHeaderOrder) {
}
}

// Validate that HeaderMapImpl copy construction and assignment works. This is a
// regression for where we were missing a valid copy constructor and had the
// default (dangerous) move semantics takeover.
TEST(HeaderMapImplTest, HeaderMapImplyCopy) {
HeaderMapImpl foo;
foo.addCopy(LowerCaseString("foo"), "bar");
auto headers = std::make_unique<HeaderMapImpl>(foo);
EXPECT_STREQ("bar", headers->get(LowerCaseString("foo"))->value().c_str());
HeaderMapImpl baz{{LowerCaseString("foo"), "baz"}};
baz = *headers;
EXPECT_STREQ("bar", baz.get(LowerCaseString("foo"))->value().c_str());
baz = baz;
EXPECT_STREQ("bar", baz.get(LowerCaseString("foo"))->value().c_str());
}

} // namespace Http
} // namespace Envoy