Skip to content

Commit eea6e50

Browse files
committed
core: use mimalloc
1 parent 36c4989 commit eea6e50

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

CMakeLists.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ macro(cpr_option OPTION_NAME OPTION_TEXT OPTION_DEFAULT)
4545
endmacro()
4646

4747
cpr_option(BUILD_WITH_ASAN "If ON, build with the address sanitizer enabled" OFF)
48+
cpr_option(BUILD_WITH_MIMALLOC "If ON, build with mimalloc (default)" ON)
4849
cpr_option(USE_EXTERNAL_FFI "Specify to use external ffi dependency" OFF)
4950

5051
add_subdirectory(deps/quickjs EXCLUDE_FROM_ALL)
@@ -57,6 +58,15 @@ add_subdirectory(deps/sqlite3 EXCLUDE_FROM_ALL)
5758
set(BUILD_WASI "simple" CACHE STRING "WASI implementation")
5859
add_subdirectory(deps/wasm3 EXCLUDE_FROM_ALL)
5960

61+
if(BUILD_WITH_MIMALLOC)
62+
option(MI_OVERRIDE "" OFF)
63+
option(MI_BUILD_SHARED "" OFF)
64+
option(MI_BUILD_STATIC "" ON)
65+
option(MI_BUILD_OBJECT "" OFF)
66+
option(MI_BUILD_TESTS "" OFF)
67+
add_subdirectory(deps/mimalloc EXCLUDE_FROM_ALL)
68+
endif()
69+
6070
find_package(CURL REQUIRED)
6171

6272
add_executable(tjs
@@ -141,7 +151,7 @@ string(TOLOWER ${CMAKE_SYSTEM_NAME} TJS_PLATFORM)
141151
target_compile_options(tjs PRIVATE ${tjs_cflags})
142152
target_compile_definitions(tjs PRIVATE TJS__PLATFORM="${TJS_PLATFORM}")
143153
target_include_directories(tjs PRIVATE ${CURL_INCLUDE_DIRS})
144-
target_link_libraries(tjs qjs uv_a m3 sqlite3 m pthread ${CURL_LIBRARIES})
154+
target_link_libraries(tjs qjs uv_a m3 sqlite3 m pthread mimalloc-static ${CURL_LIBRARIES})
145155

146156
if (BUILD_WITH_ASAN)
147157
target_compile_options(tjs PRIVATE -fsanitize=address)

src/vm.c

+65-1
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,75 @@
2525
#include "private.h"
2626
#include "tjs.h"
2727

28+
#include <mimalloc.h>
2829
#include <string.h>
2930

3031

3132
#define TJS__DEFAULT_STACK_SIZE 1048576
3233

34+
static void *tjs__malloc(JSMallocState *s, size_t size)
35+
{
36+
void *ptr;
37+
38+
/* Do not allocate zero bytes: behavior is platform dependent */
39+
assert(size != 0);
40+
41+
if (unlikely(s->malloc_size + size > s->malloc_limit))
42+
return NULL;
43+
44+
ptr = mi_malloc(size);
45+
if (!ptr)
46+
return NULL;
47+
48+
s->malloc_count++;
49+
s->malloc_size += mi_malloc_usable_size(ptr);
50+
return ptr;
51+
}
52+
53+
static void tjs__free(JSMallocState *s, void *ptr)
54+
{
55+
if (!ptr)
56+
return;
57+
58+
s->malloc_count--;
59+
s->malloc_size -= mi_malloc_usable_size(ptr);
60+
mi_free(ptr);
61+
}
62+
63+
static void *tjs__realloc(JSMallocState *s, void *ptr, size_t size)
64+
{
65+
size_t old_size;
66+
67+
if (!ptr) {
68+
if (size == 0)
69+
return NULL;
70+
return tjs__malloc(s, size);
71+
}
72+
old_size = mi_malloc_usable_size(ptr);
73+
if (size == 0) {
74+
s->malloc_count--;
75+
s->malloc_size -= old_size;
76+
mi_free(ptr);
77+
return NULL;
78+
}
79+
if (s->malloc_size + size - old_size > s->malloc_limit)
80+
return NULL;
81+
82+
ptr = mi_realloc(ptr, size);
83+
if (!ptr)
84+
return NULL;
85+
86+
s->malloc_size += mi_malloc_usable_size(ptr) - old_size;
87+
return ptr;
88+
}
89+
90+
static const JSMallocFunctions tjs_mf = {
91+
tjs__malloc,
92+
tjs__free,
93+
tjs__realloc,
94+
mi_malloc_usable_size
95+
};
96+
3397
/* core */
3498
extern const uint8_t tjs__core[];
3599
extern const uint32_t tjs__core_size;
@@ -161,7 +225,7 @@ TJSRuntime *TJS_NewRuntimeInternal(bool is_worker, TJSRunOptions *options) {
161225

162226
memcpy(&qrt->options, options, sizeof(*options));
163227

164-
qrt->rt = JS_NewRuntime();
228+
qrt->rt = JS_NewRuntime2(&tjs_mf, NULL);
165229
CHECK_NOT_NULL(qrt->rt);
166230

167231
qrt->ctx = JS_NewContext(qrt->rt);

0 commit comments

Comments
 (0)