Skip to content

Commit 8e10eeb

Browse files
Brotlicopybara-github
authored andcommitted
Add params to brotli to manually enable or disable the SIMD hashers.
PiperOrigin-RevId: 959173169
1 parent 0d1f629 commit 8e10eeb

6 files changed

Lines changed: 75 additions & 4 deletions

File tree

c/common/platform.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,11 @@ BROTLI_UNUSED_FUNCTION void BrotliSuppressUnusedFunctions(void) {
661661

662662
/* The SIMD matchers are only faster at certain quality levels. */
663663
#if defined(_M_X64) && defined(BROTLI_TZCNT64)
664+
#define BROTLI_MAX_RECOMMENDED_SIMD_QUALITY 7
664665
#define BROTLI_MAX_SIMD_QUALITY 7
665666
#elif defined(BROTLI_TZCNT64)
666-
#define BROTLI_MAX_SIMD_QUALITY 6
667+
#define BROTLI_MAX_RECOMMENDED_SIMD_QUALITY 6
668+
#define BROTLI_MAX_SIMD_QUALITY 7
667669
#endif
668670

669671
#if defined(_MSC_VER)

c/enc/encode.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ BROTLI_BOOL BrotliEncoderSetParameter(
113113
state->params.max_base64_regions = value;
114114
return BROTLI_TRUE;
115115

116+
case BROTLI_PARAM_SIMD_HASHER:
117+
if (value > 2) return BROTLI_FALSE;
118+
state->params.simd_hasher = (BrotliEncoderSimdHasher)value;
119+
return BROTLI_TRUE;
120+
116121
default: return BROTLI_FALSE;
117122
}
118123
}
@@ -702,6 +707,7 @@ static void BrotliEncoderInitParams(BrotliEncoderParams* params) {
702707
BrotliInitSharedEncoderDictionary(&params->dictionary);
703708
params->base64_mode = (int)BROTLI_DEFAULT_BASE64_MODE;
704709
params->max_base64_regions = BROTLI_DEFAULT_MAX_BASE64_REGIONS;
710+
params->simd_hasher = BROTLI_DEFAULT_SIMD_HASHER;
705711
params->dist.distance_postfix_bits = 0;
706712
params->dist.num_direct_distance_codes = 0;
707713
params->dist.alphabet_size_max =

c/enc/params.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ typedef struct BrotliEncoderParams {
4343
SharedEncoderDictionary dictionary;
4444
int base64_mode;
4545
size_t max_base64_regions;
46+
BrotliEncoderSimdHasher simd_hasher;
4647
} BrotliEncoderParams;
4748

4849
#endif /* BROTLI_ENC_PARAMS_H_ */

c/enc/quality.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,22 @@ static BROTLI_INLINE size_t LiteralSpreeLengthForSparseSearch(
153153
154154
Where "q" is quality, "h" is hasher type, "b" is bucket bits,
155155
"l" is source len. */
156+
157+
#if defined(BROTLI_MAX_SIMD_QUALITY)
158+
static BROTLI_INLINE BROTLI_BOOL
159+
ShouldUseSimdHasher(const BrotliEncoderParams* params) {
160+
if (params->simd_hasher == BROTLI_SIMD_HASHER_DISABLE) {
161+
return BROTLI_FALSE;
162+
}
163+
int max_quality = BROTLI_MAX_RECOMMENDED_SIMD_QUALITY;
164+
165+
if (params->simd_hasher == BROTLI_SIMD_HASHER_ENABLE) {
166+
max_quality = BROTLI_MAX_SIMD_QUALITY;
167+
}
168+
return TO_BROTLI_BOOL(params->quality <= max_quality);
169+
}
170+
#endif
171+
156172
static BROTLI_INLINE void ChooseHasher(const BrotliEncoderParams* params,
157173
BrotliHasherParams* hparams) {
158174
if (params->quality > 9) {
@@ -165,7 +181,7 @@ static BROTLI_INLINE void ChooseHasher(const BrotliEncoderParams* params,
165181
hparams->type = params->quality < 7 ? 40 : params->quality < 9 ? 41 : 42;
166182
} else if (params->size_hint >= (1 << 20) && params->lgwin >= 19) {
167183
#if defined(BROTLI_MAX_SIMD_QUALITY)
168-
hparams->type = params->quality <= BROTLI_MAX_SIMD_QUALITY ? 68 : 6;
184+
hparams->type = ShouldUseSimdHasher(params) ? 68 : 6;
169185
#else
170186
hparams->type = 6;
171187
#endif
@@ -177,7 +193,7 @@ static BROTLI_INLINE void ChooseHasher(const BrotliEncoderParams* params,
177193
/* TODO(eustas): often previous setting (H6) is faster and denser; consider
178194
adding an option to use it. */
179195
#if defined(BROTLI_MAX_SIMD_QUALITY)
180-
hparams->type = params->quality <= BROTLI_MAX_SIMD_QUALITY ? 58 : 5;
196+
hparams->type = ShouldUseSimdHasher(params) ? 58 : 5;
181197
#else
182198
hparams->type = 5;
183199
#endif

c/include/brotli/encode.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ typedef enum BrotliEncoderBase64Mode {
7070

7171
#define BROTLI_DEFAULT_MAX_BASE64_REGIONS 16
7272

73+
/** Options for ::BROTLI_PARAM_SIMD_HASHER parameter. */
74+
typedef enum BrotliEncoderSimdHasher {
75+
/** Use SIMD hasher when recommended for the quality level. */
76+
BROTLI_SIMD_HASHER_DEFAULT = 0,
77+
/** Use SIMD hasher when supported up to quality 7. */
78+
BROTLI_SIMD_HASHER_ENABLE = 1,
79+
/** Never use SIMD hasher. */
80+
BROTLI_SIMD_HASHER_DISABLE = 2
81+
} BrotliEncoderSimdHasher;
82+
83+
#define BROTLI_DEFAULT_SIMD_HASHER BROTLI_SIMD_HASHER_DEFAULT
84+
7385
/** Default value for ::BROTLI_PARAM_QUALITY parameter. */
7486
#define BROTLI_DEFAULT_QUALITY 11
7587
/** Default value for ::BROTLI_PARAM_LGWIN parameter. */
@@ -242,7 +254,14 @@ typedef enum BrotliEncoderParameter {
242254
* Maximum number of Base64 regions to detect.
243255
* Default is 16.
244256
*/
245-
BROTLI_PARAM_MAX_BASE64_REGIONS = 11
257+
BROTLI_PARAM_MAX_BASE64_REGIONS = 11,
258+
/**
259+
* SIMD hasher usage mode.
260+
*
261+
* Controls whether the encoder uses SIMD hashers.
262+
* See ::BrotliEncoderSimdHasher for options.
263+
*/
264+
BROTLI_PARAM_SIMD_HASHER = 12
246265
} BrotliEncoderParameter;
247266

248267
/**

docs/encode.h.3

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ encode.h \- API for Brotli compression\&.
7171
.br
7272
.RI "\fIOptions to be used with \fBBrotliEncoderSetParameter\fP\&. \fP"
7373
.ti -1c
74+
.RI "typedef enum \fBBrotliEncoderSimdHasher\fP \fBBrotliEncoderSimdHasher\fP"
75+
.br
76+
.RI "\fIOptions for \fBBROTLI_PARAM_SIMD_HASHER\fP parameter\&. \fP"
77+
.ti -1c
7478
.RI "typedef struct BrotliEncoderStateStruct \fBBrotliEncoderState\fP"
7579
.br
7680
.RI "\fIOpaque structure that holds encoder state\&. \fP"
@@ -196,6 +200,10 @@ Operations that can be performed by streaming encoder\&.
196200

197201
.PP
198202
Options to be used with \fBBrotliEncoderSetParameter\fP\&.
203+
.SS "typedef enum \fBBrotliEncoderSimdHasher\fP \fBBrotliEncoderSimdHasher\fP"
204+
205+
.PP
206+
Options for \fBBROTLI_PARAM_SIMD_HASHER\fP parameter\&.
199207
.SS "typedef struct BrotliEncoderStateStruct \fBBrotliEncoderState\fP"
200208

201209
.PP
@@ -351,6 +359,25 @@ Base64 encoding mode\&. Controls how the encoder handles Base64 content\&. Curre
351359
.TP
352360
\fB\fIBROTLI_PARAM_MAX_BASE64_REGIONS \fP\fP
353361
Maximum number of Base64 regions to detect\&. Default is 16\&.
362+
.TP
363+
\fB\fIBROTLI_PARAM_SIMD_HASHER \fP\fP
364+
SIMD hasher usage mode\&. Controls whether the encoder uses SIMD hashers\&. See \fBBrotliEncoderSimdHasher\fP for options\&.
365+
.SS "enum \fBBrotliEncoderSimdHasher\fP"
366+
367+
.PP
368+
Options for \fBBROTLI_PARAM_SIMD_HASHER\fP parameter\&.
369+
.PP
370+
\fBEnumerator\fP
371+
.in +1c
372+
.TP
373+
\fB\fIBROTLI_SIMD_HASHER_DEFAULT \fP\fP
374+
Use SIMD hasher when recommended for the quality level\&.
375+
.TP
376+
\fB\fIBROTLI_SIMD_HASHER_ENABLE \fP\fP
377+
Use SIMD hasher when supported up to quality 7\&.
378+
.TP
379+
\fB\fIBROTLI_SIMD_HASHER_DISABLE \fP\fP
380+
Never use SIMD hasher\&.
354381
.SH "Function Documentation"
355382
.PP
356383
.SS "\fBBROTLI_BOOL\fP BrotliEncoderAttachPreparedDictionary (\fBBrotliEncoderState\fP * state, const BrotliEncoderPreparedDictionary * dictionary)"

0 commit comments

Comments
 (0)