Use faster Huffman encoding for QPACK.

Protected by FLAGS_quic_reloadable_flag_quic_use_fast_huffman_encoder.

PiperOrigin-RevId: 336137775
Change-Id: If905e42444d1f4366b3022df1953b3caf12796a6
diff --git a/quic/core/qpack/qpack_instruction_encoder.cc b/quic/core/qpack/qpack_instruction_encoder.cc
index ee02278..055edd7 100644
--- a/quic/core/qpack/qpack_instruction_encoder.cc
+++ b/quic/core/qpack/qpack_instruction_encoder.cc
@@ -8,6 +8,7 @@
 
 #include "net/third_party/quiche/src/http2/hpack/huffman/hpack_huffman_encoder.h"
 #include "net/third_party/quiche/src/http2/hpack/varint/hpack_varint_encoder.h"
+#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
 #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
 #include "net/third_party/quiche/src/quic/platform/api/quic_string_utils.h"
 #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
@@ -19,7 +20,13 @@
       string_length_(0),
       byte_(0),
       state_(State::kOpcode),
-      instruction_(nullptr) {}
+      instruction_(nullptr),
+      use_fast_huffman_encoder_(
+          GetQuicReloadableFlag(quic_use_fast_huffman_encoder)) {
+  if (use_fast_huffman_encoder_) {
+    QUIC_RELOADABLE_FLAG_COUNT(quic_use_fast_huffman_encoder);
+  }
+}
 
 void QpackInstructionEncoder::Encode(
     const QpackInstructionWithValues& instruction_with_values,
@@ -164,7 +171,11 @@
   quiche::QuicheStringPiece string_to_write =
       (field_->type == QpackInstructionFieldType::kName) ? name : value;
   if (use_huffman_) {
-    http2::HuffmanEncode(string_to_write, string_length_, output);
+    if (use_fast_huffman_encoder_) {
+      http2::HuffmanEncodeFast(string_to_write, string_length_, output);
+    } else {
+      http2::HuffmanEncode(string_to_write, string_length_, output);
+    }
   } else {
     QuicStrAppend(output, string_to_write);
   }
diff --git a/quic/core/qpack/qpack_instruction_encoder.h b/quic/core/qpack/qpack_instruction_encoder.h
index 867b179..e3ae6c7 100644
--- a/quic/core/qpack/qpack_instruction_encoder.h
+++ b/quic/core/qpack/qpack_instruction_encoder.h
@@ -78,6 +78,9 @@
 
   // Field currently being decoded.
   QpackInstructionFields::const_iterator field_;
+
+  // Latched value of gfe2_reloadable_flag_quic_use_fast_huffman_encoder.
+  const bool use_fast_huffman_encoder_;
 };
 
 }  // namespace quic