Append to output instead of overwriting it in http2::HuffmanEncode().

No functional change.

PiperOrigin-RevId: 336085818
Change-Id: I84b8474b06df799cb6ae43d8a4f90a3b51c37e92
diff --git a/http2/hpack/huffman/hpack_huffman_encoder.cc b/http2/hpack/huffman/hpack_huffman_encoder.cc
index a5eeaf9..ce6009a 100644
--- a/http2/hpack/huffman/hpack_huffman_encoder.cc
+++ b/http2/hpack/huffman/hpack_huffman_encoder.cc
@@ -21,8 +21,7 @@
                    size_t encoded_size,
                    std::string* huffman) {
   DCHECK(huffman != nullptr);
-  huffman->clear();         // Note that this doesn't release memory.
-  huffman->reserve(encoded_size);
+  huffman->reserve(huffman->size() + encoded_size);
   uint64_t bit_buffer = 0;  // High-bit is next bit to output. Not clear if that
                             // is more performant than having the low-bit be the
                             // last to be output.
diff --git a/http2/hpack/huffman/hpack_huffman_encoder.h b/http2/hpack/huffman/hpack_huffman_encoder.h
index a6597db..c3ac1f6 100644
--- a/http2/hpack/huffman/hpack_huffman_encoder.h
+++ b/http2/hpack/huffman/hpack_huffman_encoder.h
@@ -22,9 +22,8 @@
 
 // Encode the plain text string |plain| with the Huffman encoding defined in the
 // HPACK RFC, 7541.  |encoded_size| is used to pre-allocate storage and it
-// should be the value returned by HuffmanSize().  |*huffman| does not have to
-// be empty, it is cleared at the beginning of this function.  This allows
-// reusing the same string object across multiple invocations.
+// should be the value returned by HuffmanSize().  Appends the result to
+// |*huffman|.
 QUICHE_EXPORT_PRIVATE void HuffmanEncode(quiche::QuicheStringPiece plain,
                                          size_t encoded_size,
                                          std::string* huffman);
diff --git a/http2/hpack/huffman/hpack_huffman_encoder_test.cc b/http2/hpack/huffman/hpack_huffman_encoder_test.cc
index a5ad105..9e7f7bb 100644
--- a/http2/hpack/huffman/hpack_huffman_encoder_test.cc
+++ b/http2/hpack/huffman/hpack_huffman_encoder_test.cc
@@ -115,6 +115,7 @@
   }
 }
 
+// Test that encoding appends to output without overwriting it.
 TEST_P(HuffmanEncoderTest, AppendToOutput) {
   size_t encoded_size = HuffmanSize("foo");
   std::string buffer;
@@ -123,14 +124,7 @@
 
   encoded_size = HuffmanSize("bar");
   Encode("bar", encoded_size, &buffer);
-
-  if (use_fast_encoder_) {
-    // HuffmanEncodeFast() appends to output allowing callers to eliminate copy.
-    EXPECT_EQ(Http2HexDecode("94e78c767f"), buffer);
-  } else {
-    // HuffmanEncode() clears output before encoding.
-    EXPECT_EQ(Http2HexDecode("8c767f"), buffer);
-  }
+  EXPECT_EQ(Http2HexDecode("94e78c767f"), buffer);
 }
 
 }  // namespace
diff --git a/quic/core/qpack/qpack_instruction_encoder.cc b/quic/core/qpack/qpack_instruction_encoder.cc
index 6a15db8..e7f5f4f 100644
--- a/quic/core/qpack/qpack_instruction_encoder.cc
+++ b/quic/core/qpack/qpack_instruction_encoder.cc
@@ -141,6 +141,7 @@
     DCHECK_EQ(0, byte_ & (1 << field_->param));
     byte_ |= (1 << field_->param);
 
+    huffman_encoded_string_.clear();
     http2::HuffmanEncode(string_to_write_, encoded_size,
                          &huffman_encoded_string_);
     string_to_write_ = huffman_encoded_string_;