blueloveTH 8 ヶ月 前
コミット
7d484f8fa3

+ 25 - 0
CMakeLists.txt

@@ -5,6 +5,7 @@ project(pocketpy)
 set(CMAKE_C_STANDARD 11)
 set(CMAKE_C_STANDARD_REQUIRED ON)
 
+include(FetchContent)
 include(CMakeOptions.txt)
 
 if(WIN32)
@@ -76,6 +77,25 @@ else()
     add_definitions(-DPK_ENABLE_CUSTOM_SNAME=0)
 endif()
 
+if(PK_ENABLE_MIMALLOC)
+    message(">> Fetching mimalloc")
+    FetchContent_Declare(
+        mimalloc
+        GIT_REPOSITORY https://github.com/microsoft/mimalloc.git
+        GIT_TAG        v3.1.5
+    )
+
+    set(MI_OVERRIDE OFF CACHE BOOL "" FORCE)
+    set(MI_BUILD_SHARED OFF CACHE BOOL "" FORCE)
+    set(MI_BUILD_STATIC ON CACHE BOOL "" FORCE)
+    set(MI_BUILD_TESTS OFF CACHE BOOL "" FORCE)
+    FetchContent_MakeAvailable(mimalloc)
+
+    add_definitions(-DPK_ENABLE_MIMALLOC=1)
+else()
+    add_definitions(-DPK_ENABLE_MIMALLOC=0)
+endif()
+
 
 if(PK_BUILD_MODULE_LZ4)
     add_subdirectory(3rd/lz4)
@@ -87,6 +107,7 @@ if(PK_BUILD_MODULE_LIBHV)
     add_definitions(-DPK_BUILD_MODULE_LIBHV)
 endif()
 
+
 if(PK_BUILD_SHARED_LIB)
     message(">> Building shared library")
     add_library(${PROJECT_NAME} SHARED ${POCKETPY_SRC})
@@ -149,4 +170,8 @@ endif()
 
 if(PK_BUILD_MODULE_LIBHV)
     target_link_libraries(${PROJECT_NAME} libhv_bindings)
+endif()
+
+if(PK_ENABLE_MIMALLOC)
+    target_link_libraries(${PROJECT_NAME} mimalloc::mimalloc)
 endif()

+ 1 - 0
CMakeOptions.txt

@@ -11,6 +11,7 @@ option(PK_ENABLE_THREADS "" ON)
 option(PK_ENABLE_DETERMINISM "" OFF)
 option(PK_ENABLE_WATCHDOG "" OFF)
 option(PK_ENABLE_CUSTOM_SNAME "" OFF)
+option(PK_ENABLE_MIMALLOC "" OFF)
 
 # modules
 option(PK_BUILD_MODULE_LZ4 "" OFF)

+ 1 - 0
include/pocketpy/common/chunkedvector.h

@@ -1,6 +1,7 @@
 #pragma once
 
 #include "pocketpy/common/vector.h"
+
 typedef struct c11_chunkedvector_chunk {
     int length;
     int capacity;

+ 1 - 0
include/pocketpy/common/utils.h

@@ -1,6 +1,7 @@
 #pragma once
 
 #include <stdio.h>
+#include <assert.h>
 
 #define PK_REGION(name) 1
 

+ 0 - 2
include/pocketpy/common/vector.h

@@ -1,7 +1,5 @@
 #pragma once
 
-#include <stdlib.h>
-#include <assert.h>
 #include <string.h>
 #include <stdbool.h>
 

+ 26 - 13
include/pocketpy/config.h

@@ -27,24 +27,15 @@
 #define PK_ENABLE_CUSTOM_SNAME      0                
 #endif
 
+#ifndef PK_ENABLE_MIMALLOC          // can be overridden by cmake
+#define PK_ENABLE_MIMALLOC          0                
+#endif
+
 // GC min threshold
 #ifndef PK_GC_MIN_THRESHOLD         // can be overridden by cmake
     #define PK_GC_MIN_THRESHOLD     32768
 #endif
 
-// Memory allocation functions
-#ifndef PK_MALLOC
-    #ifndef __cplusplus
-        #define PK_MALLOC(size)             malloc(size)
-        #define PK_REALLOC(ptr, size)       realloc(ptr, size)
-        #define PK_FREE(ptr)                free(ptr)
-    #else
-        #define PK_MALLOC(size)             std::malloc(size)
-        #define PK_REALLOC(ptr, size)       std::realloc(ptr, size)
-        #define PK_FREE(ptr)                std::free(ptr)
-    #endif
-#endif
-
 // This is the maximum size of the value stack in py_TValue units
 // The actual size in bytes equals `sizeof(py_TValue) * PK_VM_STACK_SIZE`
 #ifndef PK_VM_STACK_SIZE            // can be overridden by cmake
@@ -90,3 +81,25 @@
 #else
     #define PK_THREAD_LOCAL
 #endif
+
+// Memory allocation functions
+#ifndef PK_MALLOC
+    #if PK_ENABLE_MIMALLOC
+        #include <mimalloc.h>
+        #define PK_MALLOC(size)                 mi_malloc(size)
+        #define PK_REALLOC(ptr, size)           mi_realloc(ptr, size)
+        #define PK_FREE(ptr)                    mi_free(ptr)
+    #else
+        #ifndef __cplusplus
+            #include <stdlib.h>
+            #define PK_MALLOC(size)             malloc(size)
+            #define PK_REALLOC(ptr, size)       realloc(ptr, size)
+            #define PK_FREE(ptr)                free(ptr)
+        #else
+            #include <cstdlib>
+            #define PK_MALLOC(size)             std::malloc(size)
+            #define PK_REALLOC(ptr, size)       std::realloc(ptr, size)
+            #define PK_FREE(ptr)                std::free(ptr)
+        #endif
+    #endif
+#endif

+ 0 - 1
src/common/algorithm.c

@@ -1,7 +1,6 @@
 #include "pocketpy/common/algorithm.h"
 #include "pocketpy/config.h"
 #include <string.h>
-#include <stdlib.h>
 
 static bool _stable_sort_merge(char* a,
                   char* a_end,

+ 1 - 4
src/common/chunkedvector.c

@@ -1,9 +1,6 @@
 #include "pocketpy/common/chunkedvector.h"
-
-#include <stdlib.h>
-#include <string.h>
-#include "pocketpy/common/utils.h"
 #include "pocketpy/config.h"
+#include <assert.h>
 
 #if defined(_MSC_VER)
 #include <intrin.h>

+ 1 - 2
src/common/memorypool.c

@@ -1,7 +1,6 @@
 #include "pocketpy/common/memorypool.h"
-#include "pocketpy/pocketpy.h"
+#include "pocketpy/config.h"
 
-#include <stdlib.h>
 #include <stdbool.h>
 
 void FixedMemoryPool__ctor(FixedMemoryPool* self, int BlockSize, int BlockCount) {

+ 1 - 0
src/common/name.c

@@ -4,6 +4,7 @@
 #include "pocketpy/common/str.h"
 #include "pocketpy/pocketpy.h"
 #include "pocketpy/common/threads.h"
+#include <assert.h>
 
 typedef struct NameBucket NameBucket;
 

+ 0 - 1
src/common/sourcedata.c

@@ -1,7 +1,6 @@
 #include "pocketpy/objects/sourcedata.h"
 #include "pocketpy/common/sstream.h"
 #include <ctype.h>
-#include <stdlib.h>
 #include <string.h>
 
 static void SourceData__ctor(struct SourceData* self,

+ 1 - 1
src/common/sstream.c

@@ -4,8 +4,8 @@
 #include "pocketpy/pocketpy.h"
 
 #include <stdarg.h>
-#include <stdio.h>
 #include <assert.h>
+#include <stdio.h>
 #include <ctype.h>
 #include <math.h>
 

+ 1 - 3
src/common/str.c

@@ -2,11 +2,9 @@
 #include "pocketpy/common/sstream.h"
 #include "pocketpy/common/utils.h"
 
-#include <assert.h>
 #include <string.h>
-#include <stdlib.h>
-#include <ctype.h>
 #include <stdio.h>
+#include <assert.h>
 
 c11_string* c11_string__new(const char* data) { return c11_string__new2(data, strlen(data)); }
 

+ 0 - 1
src/common/vector.c

@@ -1,6 +1,5 @@
 #include "pocketpy/common/vector.h"
 
-#include <stdlib.h>
 #include <string.h>
 #include "pocketpy/common/utils.h"
 #include "pocketpy/config.h"

+ 1 - 0
src/interpreter/ceval.c

@@ -7,6 +7,7 @@
 #include "pocketpy/pocketpy.h"
 #include "pocketpy/objects/error.h"
 #include <stdbool.h>
+#include <assert.h>
 #include <time.h>
 
 static bool stack_format_object(VM* self, c11_sv spec);

+ 1 - 0
src/interpreter/frame.c

@@ -5,6 +5,7 @@
 #include "pocketpy/objects/codeobject.h"
 #include "pocketpy/pocketpy.h"
 #include <stdbool.h>
+#include <assert.h>
 
 void ValueStack__ctor(ValueStack* self) {
     self->sp = self->begin;

+ 1 - 0
src/interpreter/generator.c

@@ -4,6 +4,7 @@
 #include "pocketpy/objects/base.h"
 #include "pocketpy/pocketpy.h"
 #include <stdbool.h>
+#include <assert.h>
 
 void pk_newgenerator(py_Ref out, py_Frame* frame, py_TValue* begin, py_TValue* end) {
     Generator* ud = py_newobject(out, tp_generator, 1, sizeof(Generator));

+ 1 - 0
src/interpreter/heap.c

@@ -3,6 +3,7 @@
 #include "pocketpy/interpreter/objectpool.h"
 #include "pocketpy/objects/base.h"
 #include "pocketpy/pocketpy.h"
+#include <assert.h>
 
 void ManagedHeap__ctor(ManagedHeap* self) {
     MultiPool__ctor(&self->small_objects);

+ 1 - 0
src/interpreter/line_profier.c

@@ -1,4 +1,5 @@
 #include "pocketpy/interpreter/line_profiler.h"
+#include <assert.h>
 
 void LineProfiler__ctor(LineProfiler* self) {
     c11_smallmap_p2i__ctor(&self->records);

+ 0 - 2
src/interpreter/objectpool.c

@@ -1,12 +1,10 @@
 #include "pocketpy/interpreter/objectpool.h"
 
-#include "pocketpy/config.h"
 #include "pocketpy/objects/object.h"
 #include "pocketpy/common/sstream.h"
 
 #include <assert.h>
 #include <stdbool.h>
-#include <stdlib.h>
 #include <string.h>
 
 static PoolArena* PoolArena__new(int block_size) {

+ 1 - 0
src/interpreter/typeinfo.c

@@ -1,4 +1,5 @@
 #include "pocketpy/interpreter/vm.h"
+#include <assert.h>
 
 py_ItemRef pk_tpfindname(py_TypeInfo* ti, py_Name name) {
     assert(ti != NULL);

+ 1 - 1
src/interpreter/vm.c

@@ -1,6 +1,5 @@
 #include "pocketpy/interpreter/vm.h"
 #include "pocketpy/common/memorypool.h"
-#include "pocketpy/common/sstream.h"
 #include "pocketpy/common/utils.h"
 #include "pocketpy/interpreter/generator.h"
 #include "pocketpy/interpreter/modules.h"
@@ -10,6 +9,7 @@
 #include "pocketpy/common/_generated.h"
 #include "pocketpy/pocketpy.h"
 #include <stdbool.h>
+#include <assert.h>
 
 static char* pk_default_importfile(const char* path) {
 #if PK_ENABLE_OS

+ 1 - 0
src/interpreter/vmx.c

@@ -1,4 +1,5 @@
 #include "pocketpy/interpreter/vm.h"
+#include <assert.h>
 
 void pk_print_stack(VM* self, py_Frame* frame, Bytecode byte) {
     return;

+ 0 - 1
src/modules/conio.c

@@ -1,5 +1,4 @@
 #include "pocketpy/pocketpy.h"
-#include <stdlib.h>
 
 #if PY_SYS_PLATFORM == 0
 

+ 0 - 1
src/modules/lz4.c

@@ -1,6 +1,5 @@
 #ifdef PK_BUILD_MODULE_LZ4
 
-#include <stdlib.h>
 #include <string.h>
 #include <assert.h>
 #include "pocketpy/pocketpy.h"

+ 0 - 2
src/modules/traceback.c

@@ -1,6 +1,4 @@
-#include "pocketpy/objects/base.h"
 #include "pocketpy/pocketpy.h"
-#include <stdlib.h>
 
 static bool traceback_format_exc(int argc, py_Ref argv) {
     PY_CHECK_ARGC(0);

+ 1 - 0
src/objects/codeobject.c

@@ -2,6 +2,7 @@
 #include "pocketpy/common/utils.h"
 #include "pocketpy/pocketpy.h"
 #include <stdint.h>
+#include <assert.h>
 
 void Bytecode__set_signed_arg(Bytecode* self, int arg) {
     self->arg = (int16_t)arg;

+ 0 - 1
src/objects/namedict.c

@@ -4,7 +4,6 @@
 #include <stdint.h>
 #include <string.h>
 #include <assert.h>
-#include <stdlib.h>
 
 #define HASH_PROBE_0(__k, ok, i)                                                                   \
     ok = false;                                                                                    \

+ 0 - 1
src2/main.c

@@ -1,6 +1,5 @@
 #include <stdbool.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <assert.h>
 #include <string.h>