Skip to content

Commit 05ca2f2

Browse files
Allocate extra space during huffman decoding
According information from ChatGPT huffman encoding gives compression benefit about 20 - 60 %. According the article from the task #1411 huffman gives benefit about 20 %. We decide to allocate extra 50 % space during huffan decoding to prevent extra allocations (make only one allocation).
1 parent b150f73 commit 05ca2f2

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

fw/hpack.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,14 @@ do { \
289289
do { \
290290
WARN_ON_ONCE(!TFW_STR_EMPTY(&it->hdr)); \
291291
if (state & HPACK_FLAGS_HUFFMAN_NAME) { \
292-
BUFFER_GET(length, it); \
292+
unsigned long len = length + (length >> 1); \
293+
\
294+
BUFFER_GET(len, it); \
293295
if (!it->pos) { \
294296
r = -ENOMEM; \
295297
goto out; \
296298
} \
297-
BUFFER_HDR_INIT(length, it); \
299+
BUFFER_HDR_INIT(len, it); \
298300
} \
299301
} while (0)
300302

@@ -306,13 +308,15 @@ do { \
306308
? it->parsed_hdr->nchunks \
307309
: 1; \
308310
if (state & HPACK_FLAGS_HUFFMAN_VALUE) { \
309-
BUFFER_GET(length, it); \
311+
unsigned long len = length + (length >> 1); \
312+
\
313+
BUFFER_GET(len, it); \
310314
if (!it->pos) { \
311315
r = -ENOMEM; \
312316
goto out; \
313317
} \
314318
if (!TFW_STR_EMPTY(&it->hdr)) { \
315-
r = tfw_hpack_exp_hdr(req->pool, length, \
319+
r = tfw_hpack_exp_hdr(req->pool, len, \
316320
it); \
317321
if (unlikely(r)) \
318322
return r; \
@@ -502,14 +506,28 @@ huffman_decode_tail(TfwHPack *__restrict hp, TfwHttpReq *__restrict req,
502506
unsigned int i;
503507
int r;
504508

509+
#define ADJUST_EXTRA_RSPACE(req) \
510+
do { \
511+
TfwMsgParseIter *it = &req->pit; \
512+
TfwStr *hdr = &it->hdr; \
513+
TfwStr *last = TFW_STR_LAST(hdr); \
514+
\
515+
hdr->len -= it->rspace; \
516+
if (!TFW_STR_PLAIN(hdr)) \
517+
last->len -= it->rspace; \
518+
it->rspace = 0; \
519+
} while (0)
520+
505521
for (;;) {
506522
int shift;
507523

508524
if (hp->curr == -HT_NBITS) {
509-
if (likely(offset == 0))
525+
if (likely(offset == 0)) {
526+
ADJUST_EXTRA_RSPACE(req);
510527
return T_OK;
511-
else
528+
} else {
512529
return T_COMPRESSION;
530+
}
513531
}
514532

515533
i = (hp->hctx << -hp->curr) & HT_NMASK;
@@ -537,7 +555,8 @@ huffman_decode_tail(TfwHPack *__restrict hp, TfwHttpReq *__restrict req,
537555
if (likely(offset == 0)) {
538556
if ((i ^ (HT_EOS_HIGH >> 1)) <
539557
(1U << -hp->curr)) {
540-
return 0;
558+
ADJUST_EXTRA_RSPACE(req);
559+
return T_OK;
541560
}
542561
}
543562
/*
@@ -563,10 +582,13 @@ huffman_decode_tail(TfwHPack *__restrict hp, TfwHttpReq *__restrict req,
563582
}
564583
if (likely(offset == 0)) {
565584
if ((i ^ (HT_EOS_HIGH >> 1)) < (1U << -hp->curr)) {
585+
ADJUST_EXTRA_RSPACE(req);
566586
return T_OK;
567587
}
568588
}
569589
return T_COMPRESSION;
590+
591+
#undef ADJUST_EXTRA_RSPACE
570592
}
571593

572594
static int

0 commit comments

Comments
 (0)