Simplifies and modernizes SpdyDataIR. * Removes the string_view version of SpdyDataIR::SetDataShallow(), which is unnecessary. * Removes SpdyDataIR::SetDataDeep(), which was only used in a constructor. * Replaces a std::unique_ptr<std::string> with std::optional<std::string>, for less pointer chasing. * Uses template SFINAE to simplify the set of public constructors with `data` arguments convertible to std::string. #cleanup Protected by refactoring and removing unused code; not protected. PiperOrigin-RevId: 693424871
diff --git a/quiche/http2/core/spdy_framer_test.cc b/quiche/http2/core/spdy_framer_test.cc index a8341f4..57dce06 100644 --- a/quiche/http2/core/spdy_framer_test.cc +++ b/quiche/http2/core/spdy_framer_test.cc
@@ -1539,7 +1539,7 @@ ABSL_ARRAYSIZE(kH2FrameData)); SpdyDataIR data_header_ir(/* stream_id = */ 1); - data_header_ir.SetDataShallow(bytes); + data_header_ir.SetDataShallow(strlen(bytes)); frame = framer_.SerializeDataFrameHeaderWithPaddingLengthField(data_header_ir); CompareCharArraysWithHexError(
diff --git a/quiche/http2/core/spdy_protocol.cc b/quiche/http2/core/spdy_protocol.cc index d0c3414..018b8c9 100644 --- a/quiche/http2/core/spdy_protocol.cc +++ b/quiche/http2/core/spdy_protocol.cc
@@ -314,21 +314,9 @@ SpdyFrameWithHeaderBlockIR::~SpdyFrameWithHeaderBlockIR() = default; -SpdyDataIR::SpdyDataIR(SpdyStreamId stream_id, absl::string_view data) - : SpdyFrameWithFinIR(stream_id), - data_(nullptr), - data_len_(0), - padded_(false), - padding_payload_len_(0) { - SetDataDeep(data); -} - -SpdyDataIR::SpdyDataIR(SpdyStreamId stream_id, const char* data) - : SpdyDataIR(stream_id, absl::string_view(data)) {} - SpdyDataIR::SpdyDataIR(SpdyStreamId stream_id, std::string data) : SpdyFrameWithFinIR(stream_id), - data_store_(std::make_unique<std::string>(std::move(data))), + data_store_(std::move(data)), data_(data_store_->data()), data_len_(data_store_->size()), padded_(false),
diff --git a/quiche/http2/core/spdy_protocol.h b/quiche/http2/core/spdy_protocol.h index cbb7d9e..cc2fc73 100644 --- a/quiche/http2/core/spdy_protocol.h +++ b/quiche/http2/core/spdy_protocol.h
@@ -15,6 +15,7 @@ #include <iosfwd> #include <map> #include <memory> +#include <optional> #include <string> #include <utility> #include <vector> @@ -523,16 +524,15 @@ class QUICHE_EXPORT SpdyDataIR : public SpdyFrameWithFinIR { public: - // Performs a deep copy on data. - SpdyDataIR(SpdyStreamId stream_id, absl::string_view data); - - // Performs a deep copy on data. - SpdyDataIR(SpdyStreamId stream_id, const char* data); - - // Moves data into data_store_. Makes a copy if passed a non-movable string. + // Makes a copy of `data` if passed a non-movable string. SpdyDataIR(SpdyStreamId stream_id, std::string data); - // Use in conjunction with SetDataShallow() for shallow-copy on data. + template <typename S> + SpdyDataIR(SpdyStreamId stream_id, S data) + : SpdyDataIR(stream_id, std::string(data)) {} + + // Use in conjunction with SetDataShallow() to set the DATA frame payload + // length. explicit SpdyDataIR(SpdyStreamId stream_id); SpdyDataIR(const SpdyDataIR&) = delete; SpdyDataIR& operator=(const SpdyDataIR&) = delete; @@ -554,24 +554,10 @@ padding_payload_len_ = padding_len - 1; } - // Deep-copy of data (keep private copy). - void SetDataDeep(absl::string_view data) { - data_store_ = std::make_unique<std::string>(data.data(), data.size()); - data_ = data_store_->data(); - data_len_ = data.size(); - } - - // Shallow-copy of data (do not keep private copy). - void SetDataShallow(absl::string_view data) { - data_store_.reset(); - data_ = data.data(); - data_len_ = data.size(); - } - // Use this method if we don't have a contiguous buffer and only // need a length. void SetDataShallow(size_t len) { - data_store_.reset(); + data_store_ = std::nullopt; data_ = nullptr; data_len_ = len; } @@ -586,7 +572,7 @@ private: // Used to store data that this SpdyDataIR should own. - std::unique_ptr<std::string> data_store_; + std::optional<std::string> data_store_; const char* data_; size_t data_len_;