From f6d630877c3b31a5091dff2fd615ddb92189a46c Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 18 Dec 2023 13:38:16 +1100 Subject: [PATCH] esp32: Add MICROPY_GC_INITIAL_HEAP_SIZE option and tune it. This gets back the old heap-size behaviour on ESP32, before auto-split-heap was introduced: after the heap is grown one time the size is 111936 bytes, with about 40k left for the IDF. That's enough to start WiFi and do a HTTPS request. Signed-off-by: Damien George --- ports/esp32/main.c | 8 ++------ ports/esp32/mpconfigport.h | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 8e5675c941..6f51b7236b 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -79,10 +79,6 @@ #define MP_TASK_STACK_LIMIT_MARGIN (1024) #endif -// Initial Python heap size. This starts small but adds new heap areas on -// demand due to settings MICROPY_GC_SPLIT_HEAP & MICROPY_GC_SPLIT_HEAP_AUTO -#define MP_TASK_HEAP_SIZE (64 * 1024) - int vprintf_null(const char *format, va_list ap) { // do nothing: this is used as a log target during raw repl mode return 0; @@ -120,13 +116,13 @@ void mp_task(void *pvParameter) { ESP_LOGE("esp_init", "can't create event loop: 0x%x\n", err); } - void *mp_task_heap = MP_PLAT_ALLOC_HEAP(MP_TASK_HEAP_SIZE); + void *mp_task_heap = MP_PLAT_ALLOC_HEAP(MICROPY_GC_INITIAL_HEAP_SIZE); soft_reset: // initialise the stack pointer for the main thread mp_stack_set_top((void *)sp); mp_stack_set_limit(MICROPY_TASK_STACK_SIZE - MP_TASK_STACK_LIMIT_MARGIN); - gc_init(mp_task_heap, mp_task_heap + MP_TASK_HEAP_SIZE); + gc_init(mp_task_heap, mp_task_heap + MICROPY_GC_INITIAL_HEAP_SIZE); mp_init(); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib)); readline_init0(); diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 5f5cd795e1..8937455992 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -26,6 +26,20 @@ // memory allocation policies #define MICROPY_ALLOC_PATH_MAX (128) +// Initial Python heap size. This starts small but adds new heap areas on demand due to +// the settings MICROPY_GC_SPLIT_HEAP and MICROPY_GC_SPLIT_HEAP_AUTO. The value is +// different for different MCUs and is chosen so they can grow the heap once (double it) +// and still have enough internal RAM to start WiFi and make a HTTPS request. +#ifndef MICROPY_GC_INITIAL_HEAP_SIZE +#if CONFIG_IDF_TARGET_ESP32 +#define MICROPY_GC_INITIAL_HEAP_SIZE (56 * 1024) +#elif CONFIG_IDF_TARGET_ESP32S2 && !CONFIG_SPIRAM +#define MICROPY_GC_INITIAL_HEAP_SIZE (36 * 1024) +#else +#define MICROPY_GC_INITIAL_HEAP_SIZE (64 * 1024) +#endif +#endif + // emitters #define MICROPY_PERSISTENT_CODE_LOAD (1) #if !CONFIG_IDF_TARGET_ESP32C3