Skip to content

Commit

Permalink
Add ability to compile the CLI with mimalloc
Browse files Browse the repository at this point in the history
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: #142
  • Loading branch information
saghul committed Sep 20, 2024
1 parent cfeeff9 commit 6ce2dcc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
#
Expand Down
41 changes: 40 additions & 1 deletion qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
#include "cutils.h"
#include "quickjs-libc.h"

#ifdef QJS_USE_MIMALLOC
#include <mimalloc.h>
#endif

extern const uint8_t qjsc_repl[];
extern const uint32_t qjsc_repl_size;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 6ce2dcc

Please sign in to comment.