Skip to content

Commit ec372f4

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 b6ee584 commit ec372f4

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

fw/hpack.c

Lines changed: 30 additions & 8 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,20 +308,22 @@ 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)) { \
315319
it->next = tfw_hpack_exp_hdr(req->pool, \
316-
length, it); \
320+
len, it); \
317321
if (!it->next) { \
318322
r = -ENOMEM; \
319323
goto out; \
320324
} \
321325
} else { \
322-
BUFFER_HDR_INIT(length, it); \
326+
BUFFER_HDR_INIT(len, it); \
323327
} \
324328
} \
325329
} while (0)
@@ -497,14 +501,28 @@ huffman_decode_tail(TfwHPack *__restrict hp, TfwHttpReq *__restrict req,
497501
unsigned int i;
498502
int r;
499503

504+
#define ADJUST_EXTRA_RSPACE(req) \
505+
do { \
506+
TfwMsgParseIter *it = &req->pit; \
507+
TfwStr *hdr = &it->hdr; \
508+
TfwStr *last = TFW_STR_LAST(hdr); \
509+
\
510+
hdr->len -= it->rspace; \
511+
if (!TFW_STR_PLAIN(hdr)) \
512+
last->len -= it->rspace; \
513+
it->rspace = 0; \
514+
} while (0)
515+
500516
for (;;) {
501517
int shift;
502518

503519
if (hp->curr == -HT_NBITS) {
504-
if (likely(offset == 0))
520+
if (likely(offset == 0)) {
521+
ADJUST_EXTRA_RSPACE(req);
505522
return T_OK;
506-
else
523+
} else {
507524
return T_COMPRESSION;
525+
}
508526
}
509527

510528
i = (hp->hctx << -hp->curr) & HT_NMASK;
@@ -532,7 +550,8 @@ huffman_decode_tail(TfwHPack *__restrict hp, TfwHttpReq *__restrict req,
532550
if (likely(offset == 0)) {
533551
if ((i ^ (HT_EOS_HIGH >> 1)) <
534552
(1U << -hp->curr)) {
535-
return 0;
553+
ADJUST_EXTRA_RSPACE(req);
554+
return T_OK;
536555
}
537556
}
538557
/*
@@ -558,10 +577,13 @@ huffman_decode_tail(TfwHPack *__restrict hp, TfwHttpReq *__restrict req,
558577
}
559578
if (likely(offset == 0)) {
560579
if ((i ^ (HT_EOS_HIGH >> 1)) < (1U << -hp->curr)) {
580+
ADJUST_EXTRA_RSPACE(req);
561581
return T_OK;
562582
}
563583
}
564584
return T_COMPRESSION;
585+
586+
#undef ADJUST_EXTRA_RSPACE
565587
}
566588

567589
static int

0 commit comments

Comments
 (0)