Skip to content
Snippets Groups Projects
Commit f48cb14d authored by Adenilson Cavalcanti's avatar Adenilson Cavalcanti Committed by Copybara-Service
Browse files

[zlib] Increase inflate dynamic table sizes

First patch* based on Dougall Johnson zlib research**.

The basic idea is to use a bigger root table to avoid a branch
misprediction while performing a lookup when the searched
Huffman code is actually located in a nested sub-table.

The increase of memory use in the structure inflate_state will be
1920 bytes (i.e. 1332 - 852 = 480 x sizeof(code) = 480 x 4) per active inflate context which should be fine given that even lower spec boards generally have CPUs with a L1 cache size of 32KB.

Performance gains are dependent on input entropy features plus architecture, but are a net positive on both x86 (Haswell: 5.1%), Cortex big.LITTLE.Medium (X1, A55, A76) on 32bit (6.3%, 1.44%, 5.7%) and 64bit (8.3%, 2.4%, 6.7%) plus M1 (7.6%).

* https://github.com/dougallj/zlib-dougallj/commits/main
** https://dougallj.wordpress.com/2022/08/20/

Bug: 1354990
Change-Id: I1f784bfaf9a8108f657e3c9753030482e5026366
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3888219


Reviewed-by: default avatarChris Blume <cblume@chromium.org>
Commit-Queue: Adenilson Cavalcanti <cavalcantii@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1046251}
NOKEYCHECK=True
GitOrigin-RevId: b40b1f7a30b58aeb0d6e4906ae25c6e4734f3b1f
parent 05e137d3
No related branches found
No related tags found
No related merge requests found
......@@ -1043,11 +1043,11 @@ int flush;
}
/* build code tables -- note: do not change the lenbits or distbits
values here (9 and 6) without reading the comments in inftrees.h
values here (10 and 9) without reading the comments in inftrees.h
concerning the ENOUGH constants, which depend on those values */
state->next = state->codes;
state->lencode = (const code FAR *)(state->next);
state->lenbits = 9;
state->lenbits = 10;
ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
&(state->lenbits), state->work);
if (ret) {
......@@ -1056,7 +1056,7 @@ int flush;
break;
}
state->distcode = (const code FAR *)(state->next);
state->distbits = 6;
state->distbits = 9;
ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
&(state->next), &(state->distbits), state->work);
if (ret) {
......
......@@ -455,11 +455,11 @@ void FAR *out_desc;
}
/* build code tables -- note: do not change the lenbits or distbits
values here (9 and 6) without reading the comments in inftrees.h
values here (10 and 9) without reading the comments in inftrees.h
concerning the ENOUGH constants, which depend on those values */
state->next = state->codes;
state->lencode = (code const FAR *)(state->next);
state->lenbits = 9;
state->lenbits = 10;
ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
&(state->lenbits), state->work);
if (ret) {
......@@ -468,7 +468,7 @@ void FAR *out_desc;
break;
}
state->distcode = (code const FAR *)(state->next);
state->distbits = 6;
state->distbits = 9;
ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
&(state->next), &(state->distbits), state->work);
if (ret) {
......
......@@ -1032,11 +1032,11 @@ int flush;
}
/* build code tables -- note: do not change the lenbits or distbits
values here (9 and 6) without reading the comments in inftrees.h
values here (10 and 9) without reading the comments in inftrees.h
concerning the ENOUGH constants, which depend on those values */
state->next = state->codes;
state->lencode = (const code FAR *)(state->next);
state->lenbits = 9;
state->lenbits = 10;
ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
&(state->lenbits), state->work);
if (ret) {
......@@ -1045,7 +1045,7 @@ int flush;
break;
}
state->distcode = (const code FAR *)(state->next);
state->distbits = 6;
state->distbits = 9;
ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
&(state->next), &(state->distbits), state->work);
if (ret) {
......
......@@ -36,17 +36,17 @@ typedef struct {
*/
/* Maximum size of the dynamic table. The maximum number of code structures is
1444, which is the sum of 852 for literal/length codes and 592 for distance
1924, which is the sum of 1332 for literal/length codes and 592 for distance
codes. These values were found by exhaustive searches using the program
examples/enough.c found in the zlib distribution. The arguments to that
program are the number of symbols, the initial root table size, and the
maximum bit length of a code. "enough 286 9 15" for literal/length codes
returns returns 852, and "enough 30 6 15" for distance codes returns 592.
The initial root table size (9 or 6) is found in the fifth argument of the
maximum bit length of a code. "enough 286 10 15" for literal/length codes
returns returns 1332, and "enough 30 9 15" for distance codes returns 592.
The initial root table size (10 or 9) is found in the fifth argument of the
inflate_table() calls in inflate.c and infback.c. If the root table size is
changed, then these maximum sizes would be need to be recalculated and
updated. */
#define ENOUGH_LENS 852
#define ENOUGH_LENS 1332
#define ENOUGH_DISTS 592
#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment