Skip to content

Commit

Permalink
more tuning for BROTLI_EXPERIMENTAL + clean-on-oom
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 558771745
  • Loading branch information
eustas authored and Copybara-Service committed Aug 21, 2023
1 parent feb6d8b commit 2a5a088
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
11 changes: 10 additions & 1 deletion c/enc/encode.c
Expand Up @@ -1836,6 +1836,15 @@ BROTLI_BOOL BrotliEncoderAttachPreparedDictionary(BrotliEncoderState* state,
size_t BrotliEncoderEstimatePeakMemoryUsage(int quality, int lgwin,
size_t input_size) {
BrotliEncoderParams params;
size_t memory_manager_slots = 0;
size_t memory_manager_size = 0;
#if !defined(BROTLI_ENCODER_EXIT_ON_OOM)
memory_manager_slots = 256;
#if defined(BROTLI_EXPERIMENTAL)
memory_manager_slots = 32768;
#endif /* BROTLI_EXPERIMENTAL */
#endif /* BROTLI_ENCODER_EXIT_ON_OOM */
memory_manager_size = memory_manager_slots * sizeof(void*);
BrotliEncoderInitParams(&params);
params.quality = quality;
params.lgwin = lgwin;
Expand Down Expand Up @@ -1892,7 +1901,7 @@ size_t BrotliEncoderEstimatePeakMemoryUsage(int quality, int lgwin,
command_histograms * sizeof(HistogramCommand) +
distance_histograms * sizeof(HistogramDistance);
}
return (ringbuffer_size +
return (memory_manager_size + ringbuffer_size +
hash_size[0] + hash_size[1] + hash_size[2] + hash_size[3] +
cmdbuf_size +
outbuf_size +
Expand Down
8 changes: 3 additions & 5 deletions c/enc/memory.c
Expand Up @@ -20,11 +20,9 @@
extern "C" {
#endif

/* TODO(eustas): fine-tune for "many slots" case */
#define MAX_NEW_ALLOCATED 64
#define MAX_NEW_FREED 64
#define MAX_PERM_ALLOCATED \
(BROTLI_ENCODER_MEMORY_MANAGER_SLOTS - MAX_NEW_ALLOCATED - MAX_NEW_FREED)
#define MAX_NEW_ALLOCATED (BROTLI_ENCODER_MEMORY_MANAGER_SLOTS >> 2)
#define MAX_NEW_FREED (BROTLI_ENCODER_MEMORY_MANAGER_SLOTS >> 2)
#define MAX_PERM_ALLOCATED (BROTLI_ENCODER_MEMORY_MANAGER_SLOTS >> 1)

#define PERM_ALLOCATED_OFFSET 0
#define NEW_ALLOCATED_OFFSET MAX_PERM_ALLOCATED
Expand Down
2 changes: 1 addition & 1 deletion c/enc/memory.h
Expand Up @@ -26,7 +26,7 @@ extern "C" {

#if !defined(BROTLI_ENCODER_EXIT_ON_OOM)
#if defined(BROTLI_EXPERIMENTAL)
#define BROTLI_ENCODER_MEMORY_MANAGER_SLOTS 6144
#define BROTLI_ENCODER_MEMORY_MANAGER_SLOTS 32768
#else /* BROTLI_EXPERIMENTAL */
#define BROTLI_ENCODER_MEMORY_MANAGER_SLOTS 256
#endif /* BROTLI_EXPERIMENTAL */
Expand Down

0 comments on commit 2a5a088

Please sign in to comment.