Compare commits

...

4 Commits

Author SHA1 Message Date
blueloveTH
2f296994ae Update chunkedvector.c 2025-07-01 16:49:40 +08:00
blueloveTH
1194497b5c fix cmake 2025-07-01 16:04:33 +08:00
blueloveTH
152d6bbd49 test mimalloc
Update cmake_build.py

Update CMakeLists.txt

Update CMakeLists.txt
2025-07-01 15:54:39 +08:00
blueloveTH
7d484f8fa3 fix config.h 2025-07-01 15:13:46 +08:00
32 changed files with 92 additions and 52 deletions

View File

@ -162,7 +162,7 @@ jobs:
uses: jirutka/setup-alpine@v1
with:
arch: ${{ matrix.arch }}
packages: gcc g++ make cmake libc-dev linux-headers python3
packages: gcc g++ make cmake libc-dev linux-headers python3 git
- name: Build and Test
run: |
echo "Building for architecture: ${{ matrix.arch }}"

1
.gitignore vendored
View File

@ -40,3 +40,4 @@ docs/C-API/functions.md
cmake-build-*
tmp/

View File

@ -5,6 +5,7 @@ project(pocketpy)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
include(FetchContent)
include(CMakeOptions.txt)
if(WIN32)
@ -13,8 +14,7 @@ endif()
if(MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /utf-8 /jumptablerdata")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /experimental:c11atomics")
add_compile_options(/wd4267 /wd4244 /wd4146)
add_compile_options(/wd4267 /wd4244 /wd4146 /experimental:c11atomics)
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Ox")
@ -76,6 +76,28 @@ 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_NO_USE_CXX ON CACHE BOOL "" FORCE)
set(MI_BUILD_SHARED OFF CACHE BOOL "" FORCE)
set(MI_BUILD_OBJECT OFF CACHE BOOL "" FORCE)
set(MI_BUILD_STATIC ON CACHE BOOL "" FORCE)
set(MI_BUILD_TESTS OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(mimalloc)
include_directories(${mimalloc_SOURCE_DIR}/include)
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 +109,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})
@ -150,3 +173,7 @@ endif()
if(PK_BUILD_MODULE_LIBHV)
target_link_libraries(${PROJECT_NAME} libhv_bindings)
endif()
if(PK_ENABLE_MIMALLOC)
target_link_libraries(${PROJECT_NAME} mimalloc-static)
endif()

View File

@ -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)

View File

@ -20,7 +20,7 @@ assert config in ['Debug', 'Release', 'RelWithDebInfo']
os.chdir("build")
code = os.system(f"cmake .. -DPK_ENABLE_DETERMINISM=ON -DCMAKE_BUILD_TYPE={config} {extra_flags}")
code = os.system(f"cmake .. -DPK_ENABLE_MIMALLOC=ON -DPK_ENABLE_DETERMINISM=ON -DCMAKE_BUILD_TYPE={config} {extra_flags}")
assert code == 0
code = os.system(f"cmake --build . --config {config} -j 4")
assert code == 0

View File

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

View File

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

View File

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

View File

@ -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

View File

@ -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,

View File

@ -1,15 +1,12 @@
#include "pocketpy/common/chunkedvector.h"
#include <stdlib.h>
#include <string.h>
#include "pocketpy/common/utils.h"
#include "pocketpy/config.h"
#include "pocketpy/pocketpy.h"
#include <assert.h>
#if defined(_MSC_VER)
#include <intrin.h>
#endif
inline static int c11_bit_length(unsigned long x) {
PK_INLINE static int c11__bit_length(unsigned long x) {
#if(defined(__clang__) || defined(__GNUC__))
return x == 0 ? 0 : (int)sizeof(unsigned long) * 8 - __builtin_clzl(x);
#elif defined(_MSC_VER)
@ -39,7 +36,6 @@ void c11_chunkedvector__ctor(c11_chunkedvector* self, int elem_size, int initial
self->initial_chunks = initial_chunks;
void* chunks_data = PK_MALLOC(elem_size * ((1U << (unsigned int)initial_chunks) - 1));
for(int i = 0; i < initial_chunks; i++) {
// TODO: optimize?
c11_chunkedvector_chunk chunk = {.length = 0,
.capacity = 1U << i,
.data = (char*)chunks_data + elem_size * ((1U << i) - 1U)};
@ -59,10 +55,11 @@ void c11_chunkedvector__dtor(c11_chunkedvector* self) {
void* c11_chunkedvector__emplace(c11_chunkedvector* self) {
if(self->length == self->capacity) {
#ifndef NDEBUG
c11_chunkedvector_chunk last_chunk = c11_vector__back(c11_chunkedvector_chunk, &self->chunks);
assert(last_chunk.capacity == last_chunk.length);
#endif
#ifndef NDEBUG
c11_chunkedvector_chunk last_chunk =
c11_vector__back(c11_chunkedvector_chunk, &self->chunks);
assert(last_chunk.capacity == last_chunk.length);
#endif
c11_chunkedvector_chunk chunk = {
.length = 0,
.capacity = 1U << (unsigned int)self->chunks.length,
@ -70,9 +67,14 @@ void* c11_chunkedvector__emplace(c11_chunkedvector* self) {
self->capacity += chunk.capacity;
c11_vector__push(c11_chunkedvector_chunk, &self->chunks, chunk);
}
int last_chunk_index = c11_bit_length(self->length + 1) - 1;
#if 1
int last_chunk_index = c11__bit_length(self->length + 1) - 1;
c11_chunkedvector_chunk* last_chunk =
c11__at(c11_chunkedvector_chunk, &self->chunks, last_chunk_index);
#else
// This is not correct, because there is some pre-allocated chunks
c11_chunkedvector_chunk* last_chunk = &c11_vector__back(c11_chunkedvector_chunk, &self->chunks);
#endif
void* p = (char*)last_chunk->data + self->elem_size * last_chunk->length;
last_chunk->length++;
self->length++;
@ -80,7 +82,7 @@ void* c11_chunkedvector__emplace(c11_chunkedvector* self) {
}
void* c11_chunkedvector__at(c11_chunkedvector* self, int index) {
int chunk_index = c11_bit_length(index + 1) - 1;
int chunk_index = c11__bit_length(index + 1) - 1;
c11_chunkedvector_chunk* chunk = c11__at(c11_chunkedvector_chunk, &self->chunks, chunk_index);
return (char*)chunk->data + (index + 1 - (1U << (unsigned int)chunk_index)) * self->elem_size;
}

View File

@ -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) {

View File

@ -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;

View File

@ -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,

View File

@ -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>

View File

@ -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)); }

View File

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

View File

@ -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);

View File

@ -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;

View File

@ -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));

View File

@ -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);

View File

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

View File

@ -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) {

View File

@ -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);

View File

@ -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

View File

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

View File

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

View File

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

View File

@ -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);

View File

@ -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;

View File

@ -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; \

View File

@ -1,6 +1,5 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
@ -20,7 +19,7 @@ static char* read_file(const char* path) {
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, 0, SEEK_SET);
char* buffer = malloc(size + 1);
char* buffer = PK_MALLOC(size + 1);
size = fread(buffer, 1, size, file);
buffer[size] = 0;
return buffer;
@ -85,7 +84,7 @@ int main(int argc, char** argv) {
char* source = read_file(filename);
if(source) {
if(!py_exec(source, filename, EXEC_MODE, NULL)) py_printexc();
free(source);
PK_FREE(source);
}
}