Skip to content

Commit 6ce2dcc

Browse files
committed
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: #142
1 parent cfeeff9 commit 6ce2dcc

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ endif()
111111

112112
xoption(BUILD_EXAMPLES "Build examples" OFF)
113113
xoption(BUILD_STATIC_QJS_EXE "Build a static qjs executable" OFF)
114+
xoption(BUILD_CLI_WITH_MIMALLOC "Build the qjs executable with mimalloc" OFF)
114115
xoption(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF)
115116
xoption(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF)
116117
xoption(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF)
@@ -254,7 +255,11 @@ endif()
254255
if(NOT WIN32)
255256
set_target_properties(qjs_exe PROPERTIES ENABLE_EXPORTS TRUE)
256257
endif()
257-
258+
if(BUILD_CLI_WITH_MIMALLOC)
259+
find_package(mimalloc REQUIRED)
260+
target_compile_definitions(qjs_exe PRIVATE QJS_USE_MIMALLOC)
261+
target_link_libraries(qjs_exe mimalloc-static)
262+
endif()
258263

259264
# Test262 runner
260265
#

qjs.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
#include "cutils.h"
3939
#include "quickjs-libc.h"
4040

41+
#ifdef QJS_USE_MIMALLOC
42+
#include <mimalloc.h>
43+
#endif
44+
4145
extern const uint8_t qjsc_repl[];
4246
extern const uint32_t qjsc_repl_size;
4347

@@ -230,7 +234,6 @@ static void *js_trace_calloc(void *opaque, size_t count, size_t size)
230234

231235
static void *js_trace_malloc(void *opaque, size_t size)
232236
{
233-
(void) opaque;
234237
void *ptr;
235238
ptr = malloc(size);
236239
js_trace_malloc_printf(opaque, "A %zd -> %p\n", size, ptr);
@@ -261,6 +264,38 @@ static const JSMallocFunctions trace_mf = {
261264
js__malloc_usable_size
262265
};
263266

267+
#ifdef QJS_USE_MIMALLOC
268+
static void *js_mi_calloc(void *opaque, size_t count, size_t size)
269+
{
270+
return mi_calloc(count, size);
271+
}
272+
273+
static void *js_mi_malloc(void *opaque, size_t size)
274+
{
275+
return mi_malloc(size);
276+
}
277+
278+
static void js_mi_free(void *opaque, void *ptr)
279+
{
280+
if (!ptr)
281+
return;
282+
mi_free(ptr);
283+
}
284+
285+
static void *js_mi_realloc(void *opaque, void *ptr, size_t size)
286+
{
287+
return mi_realloc(ptr, size);
288+
}
289+
290+
static const JSMallocFunctions mi_mf = {
291+
js_mi_calloc,
292+
js_mi_malloc,
293+
js_mi_free,
294+
js_mi_realloc,
295+
mi_malloc_usable_size
296+
};
297+
#endif
298+
264299
#define PROG_NAME "qjs"
265300

266301
void help(void)
@@ -438,7 +473,11 @@ int main(int argc, char **argv)
438473
js_trace_malloc_init(&trace_data);
439474
rt = JS_NewRuntime2(&trace_mf, &trace_data);
440475
} else {
476+
#ifdef QJS_USE_MIMALLOC
477+
rt = JS_NewRuntime2(&mi_mf, NULL);
478+
#else
441479
rt = JS_NewRuntime();
480+
#endif
442481
}
443482
if (!rt) {
444483
fprintf(stderr, "qjs: cannot allocate JS runtime\n");

0 commit comments

Comments
 (0)