Skip to content

Commit

Permalink
Resize Huffman string before appending characters
Browse files Browse the repository at this point in the history
Summary: We're pushing back onto an fbstring character-by-character, when we could instead reserve an appropriate size beforehand and then do the appending. I found the ratio `3/2` empirically, it seems to be a good upper bound.

Differential Revision: D60291076

fbshipit-source-id: 982363420c495d1f082e827375fec9626944d5d7
  • Loading branch information
Aman Sharma authored and facebook-github-bot committed Aug 7, 2024
1 parent ddf21d2 commit 6b918de
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions proxygen/lib/http/codec/compress/Huffman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
#include <proxygen/lib/http/codec/compress/Huffman.h>

#include <folly/Indestructible.h>
#include <folly/container/Reserve.h>
#include <folly/portability/Sockets.h>

using std::pair;

namespace proxygen { namespace huffman {

// These constants were decided upon empirically
constexpr static uint32_t kHuffmanDecodeSpaceNumerator = 3;
constexpr static uint32_t kHuffmanDecodeSpaceDenominator = 2;

HuffTree::HuffTree(const uint32_t* codes, const uint8_t* bits)
: codes_(codes), bits_(bits) {
buildTree();
Expand All @@ -28,6 +33,9 @@ HuffTree::HuffTree(const HuffTree& tree)
bool HuffTree::decode(const uint8_t* buf,
uint32_t size,
folly::fbstring& literal) const {
folly::grow_capacity_by(
literal,
(size * kHuffmanDecodeSpaceNumerator) / kHuffmanDecodeSpaceDenominator);
const SuperHuffNode* snode = &table_[0];
uint32_t w = 0;
uint32_t wbits = 0;
Expand Down

0 comments on commit 6b918de

Please sign in to comment.