From 2a5a088b03ba5fd3aab4f34338c84e2c61d82c49 Mon Sep 17 00:00:00 2001 From: Evgenii Kliuchnikov Date: Mon, 21 Aug 2023 06:35:47 -0700 Subject: [PATCH] more tuning for BROTLI_EXPERIMENTAL + clean-on-oom PiperOrigin-RevId: 558771745 --- c/enc/encode.c | 11 ++++++++++- c/enc/memory.c | 8 +++----- c/enc/memory.h | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/c/enc/encode.c b/c/enc/encode.c index 81ea40e..6ce8edd 100644 --- a/c/enc/encode.c +++ b/c/enc/encode.c @@ -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(¶ms); params.quality = quality; params.lgwin = lgwin; @@ -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 + diff --git a/c/enc/memory.c b/c/enc/memory.c index 8811821..bb5e364 100644 --- a/c/enc/memory.c +++ b/c/enc/memory.c @@ -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 diff --git a/c/enc/memory.h b/c/enc/memory.h index 9dbed55..a958fdd 100644 --- a/c/enc/memory.h +++ b/c/enc/memory.h @@ -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 */