more tuning for BROTLI_EXPERIMENTAL + clean-on-oom

PiperOrigin-RevId: 558771745
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 @@
 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;
@@ -1892,7 +1901,7 @@
                        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 @@
 
 #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 */