Skip to content

Commit 3c8be4f

Browse files
committed
Set defaults to v2 defaults to reduce memory usage
1 parent 3024497 commit 3c8be4f

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

‎Objects/obmalloc.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ _PyMem_RawFree(void *Py_UNUSED(ctx), void *ptr)
9393
static bool count_blocks(const mi_heap_t *, const mi_heap_area_t *,
9494
void *, size_t, void *);
9595

96+
// mimalloc v3 delays returning freed memory to the OS for much longer than v2
97+
// did (1000 ms purge delay and 4x arena multiplier, vs 10 ms and 10x), which
98+
// noticeably increases peak RSS. Restore the v2 timings, which have no
99+
// measurable speed cost. MIMALLOC_PURGE_DELAY and MIMALLOC_ARENA_PURGE_MULT
100+
// still take precedence.
101+
static void
102+
set_mimalloc_defaults(void)
103+
{
104+
mi_option_set_default(mi_option_purge_delay, 10);
105+
mi_option_set_default(mi_option_arena_purge_mult, 10);
106+
}
107+
96108
#ifdef Py_GIL_DISABLED
97109
static _Py_thread_local struct _qsbr_shared *mimalloc_init_qsbr;
98110

@@ -180,6 +192,8 @@ mi_page_qsbr_should_delay(mi_page_qsbr_t *page, mi_heap_qsbr_t *heap,
180192
int
181193
_PyMem_InitMimalloc(PyInterpreterState *interp)
182194
{
195+
set_mimalloc_defaults();
196+
183197
size_t base_offset = offsetof(PyObject, ob_type);
184198
if (_PyMem_DebugEnabled()) {
185199
base_offset += 2 * sizeof(size_t);
@@ -849,6 +863,8 @@ set_up_allocators_unlocked(PyMemAllocatorName allocator)
849863
case PYMEM_ALLOCATOR_MIMALLOC:
850864
case PYMEM_ALLOCATOR_MIMALLOC_DEBUG:
851865
{
866+
set_mimalloc_defaults();
867+
852868
PyMemAllocatorEx malloc_alloc = MIMALLOC_RAWALLOC;
853869
set_allocator_unlocked(PYMEM_DOMAIN_RAW, &malloc_alloc);
854870

0 commit comments

Comments
 (0)