From cb4a5ed736b706434e691a75bec94e75be2785f9 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Tue, 30 Aug 2022 18:11:04 +0200 Subject: [PATCH] Berry extend max bytes size to 512Kb when PSRAM is available --- lib/libesp32/berry/default/berry_conf.h | 8 ++++++++ lib/libesp32/berry/src/be_byteslib.c | 3 +-- lib/libesp32/berry/src/be_vm.c | 1 + lib/libesp32/berry/src/be_vm.h | 1 + tasmota/tasmota_xdrv_driver/xdrv_52_9_berry.ino | 4 ++++ 5 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/libesp32/berry/default/berry_conf.h b/lib/libesp32/berry/default/berry_conf.h index c70af7945..4d46eb3df 100644 --- a/lib/libesp32/berry/default/berry_conf.h +++ b/lib/libesp32/berry/default/berry_conf.h @@ -41,6 +41,14 @@ **/ #define BE_USE_SINGLE_FLOAT 1 // use `float` not `double` +/* Macro: BE_BYTES_MAX_SIZE + * Maximum size in bytes of a `bytes()` object. + * Putting too much pressure on the memory allocator can do + * harm, so we limit the maximum size. + * Default: 32kb + **/ +#define BE_BYTES_MAX_SIZE (32*1024) /* 32 kb default value */ + /* Macro: BE_USE_PRECOMPILED_OBJECT * Use precompiled objects to avoid creating these objects at * runtime. Enable this macro can greatly optimize RAM usage. diff --git a/lib/libesp32/berry/src/be_byteslib.c b/lib/libesp32/berry/src/be_byteslib.c index 11857963a..299cc2fcd 100644 --- a/lib/libesp32/berry/src/be_byteslib.c +++ b/lib/libesp32/berry/src/be_byteslib.c @@ -18,7 +18,6 @@ #include #define BYTES_DEFAULT_SIZE 28 // default pre-reserved size for buffer (keep 4 bytes for len/size) -#define BYTES_MAX_SIZE (32*1024) // max 32Kb #define BYTES_OVERHEAD 4 // bytes overhead to be added when allocating (used to store len and size) #define BYTES_HEADROOM 8 // keep a natural headroom of 8 bytes when resizing @@ -506,7 +505,7 @@ void m_write_attributes(bvm *vm, int rel_idx, const buf_impl * attr) void bytes_realloc(bvm *vm, buf_impl * attr, int32_t size) { if (!attr->fixed && size < 4) { size = 4; } - if (size > BYTES_MAX_SIZE) { size = BYTES_MAX_SIZE; } + if (size > vm->bytesmaxsize) { size = vm->bytesmaxsize; } size_t oldsize = attr->bufptr ? attr->size : 0; attr->bufptr = (uint8_t*) be_realloc(vm, attr->bufptr, oldsize, size); /* malloc */ attr->size = size; diff --git a/lib/libesp32/berry/src/be_vm.c b/lib/libesp32/berry/src/be_vm.c index de738fb4d..ffc55977a 100644 --- a/lib/libesp32/berry/src/be_vm.c +++ b/lib/libesp32/berry/src/be_vm.c @@ -477,6 +477,7 @@ BERRY_API bvm* be_vm_new(void) be_gc_setpause(vm, 1); be_loadlibs(vm); vm->compopt = 0; + vm->bytesmaxsize = BE_BYTES_MAX_SIZE; vm->obshook = NULL; vm->ctypefunc = NULL; #if BE_USE_PERF_COUNTERS diff --git a/lib/libesp32/berry/src/be_vm.h b/lib/libesp32/berry/src/be_vm.h index 816d10cd1..474e74a7d 100644 --- a/lib/libesp32/berry/src/be_vm.h +++ b/lib/libesp32/berry/src/be_vm.h @@ -105,6 +105,7 @@ struct bvm { struct bgc gc; bctypefunc ctypefunc; /* handler to ctype_func */ bbyte compopt; /* compilation options */ + uint32_t bytesmaxsize; /* max allowed size for bytes() object, default 32kb but can be increased */ bobshook obshook; bmicrosfnct microsfnct; /* fucntion to get time as a microsecond resolution */ #if BE_USE_PERF_COUNTERS diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_9_berry.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_9_berry.ino index 51d18abad..36ac8b4e0 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_9_berry.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_9_berry.ino @@ -316,6 +316,10 @@ void BerryInit(void) { comp_set_strict(berry.vm); /* Enable strict mode in Berry compiler, equivalent of `import strict` */ be_set_ctype_func_hanlder(berry.vm, be_call_ctype_func); + if (UsePSRAM()) { // if PSRAM is available, raise the max size to 512kb + berry.vm->bytesmaxsize = 512 * 1024; + } + be_load_custom_libs(berry.vm); // load classes and modules // Set the GC threshold to 3584 bytes to avoid the first useless GC