From 6ce2dcc9386500e6140bfdc56885537a32e082bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Thu, 12 Sep 2024 14:24:30 +0200 Subject: [PATCH] Add ability to compile the CLI with mimalloc Some (unscientific) benchmark results: | Benchmark (Higher scores are better) | QuickJS | QuickJS (mimalloc) | |---------------------------------------|-------------------|--------------------| | Richards | 1217 | 1229 | | DeltaBlue | 1192 | 1297 | | Crypto | 1195 | 1191 | | RayTrace | 1477 | 2186 | | EarleyBoyer | 2441 | 3246 | | RegExp | 275 | 315 | | Splay | 2461 | 3765 | | NavierStokes | 2156 | 2119 | | Score | 1318 | 1553 | Running the V8 benchmark suite (version 7) on an M1 MacBook Pro. Fixes: https://github.com/quickjs-ng/quickjs/issues/142 --- CMakeLists.txt | 7 ++++++- qjs.c | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 31ea72f..dd98a58 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,6 +111,7 @@ endif() xoption(BUILD_EXAMPLES "Build examples" OFF) xoption(BUILD_STATIC_QJS_EXE "Build a static qjs executable" OFF) +xoption(BUILD_CLI_WITH_MIMALLOC "Build the qjs executable with mimalloc" OFF) xoption(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF) xoption(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF) xoption(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF) @@ -254,7 +255,11 @@ endif() if(NOT WIN32) set_target_properties(qjs_exe PROPERTIES ENABLE_EXPORTS TRUE) endif() - +if(BUILD_CLI_WITH_MIMALLOC) + find_package(mimalloc REQUIRED) + target_compile_definitions(qjs_exe PRIVATE QJS_USE_MIMALLOC) + target_link_libraries(qjs_exe mimalloc-static) +endif() # Test262 runner # diff --git a/qjs.c b/qjs.c index 6cbbd6c..89aadce 100644 --- a/qjs.c +++ b/qjs.c @@ -38,6 +38,10 @@ #include "cutils.h" #include "quickjs-libc.h" +#ifdef QJS_USE_MIMALLOC +#include +#endif + extern const uint8_t qjsc_repl[]; extern const uint32_t qjsc_repl_size; @@ -230,7 +234,6 @@ static void *js_trace_calloc(void *opaque, size_t count, size_t size) static void *js_trace_malloc(void *opaque, size_t size) { - (void) opaque; void *ptr; ptr = malloc(size); js_trace_malloc_printf(opaque, "A %zd -> %p\n", size, ptr); @@ -261,6 +264,38 @@ static const JSMallocFunctions trace_mf = { js__malloc_usable_size }; +#ifdef QJS_USE_MIMALLOC +static void *js_mi_calloc(void *opaque, size_t count, size_t size) +{ + return mi_calloc(count, size); +} + +static void *js_mi_malloc(void *opaque, size_t size) +{ + return mi_malloc(size); +} + +static void js_mi_free(void *opaque, void *ptr) +{ + if (!ptr) + return; + mi_free(ptr); +} + +static void *js_mi_realloc(void *opaque, void *ptr, size_t size) +{ + return mi_realloc(ptr, size); +} + +static const JSMallocFunctions mi_mf = { + js_mi_calloc, + js_mi_malloc, + js_mi_free, + js_mi_realloc, + mi_malloc_usable_size +}; +#endif + #define PROG_NAME "qjs" void help(void) @@ -438,7 +473,11 @@ int main(int argc, char **argv) js_trace_malloc_init(&trace_data); rt = JS_NewRuntime2(&trace_mf, &trace_data); } else { +#ifdef QJS_USE_MIMALLOC + rt = JS_NewRuntime2(&mi_mf, NULL); +#else rt = JS_NewRuntime(); +#endif } if (!rt) { fprintf(stderr, "qjs: cannot allocate JS runtime\n");