Jelajahi Sumber

optimize speed for obj_getObjDirect

optimize speed for arg_append
pikastech 3 tahun lalu
induk
melakukan
3f9195f89d

+ 1 - 5
port/linux/CMakeLists.txt

@@ -7,11 +7,6 @@ ENDIF(PIKA_CONFIG_ENABLE)
 
 project(pikascript VERSION 0.1.0)
 
-# SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg")
-# SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg")
-# SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
-# SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg")
-
 SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fprofile-arcs -ftest-coverage")
 SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fprofile-arcs -ftest-coverage")
 SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fprofile-arcs -ftest-coverage -lgcov")
@@ -35,4 +30,5 @@ add_subdirectory(test)
 add_subdirectory(boot/demo06-pikamain)
 add_subdirectory(boot/banchmark)
 add_subdirectory(benchmark)
+add_subdirectory(performance)
 

+ 1 - 1
port/linux/ci_benchmark_times.sh

@@ -1,4 +1,4 @@
-cd Release && ninja -j8
+cd Release && ninja -j0
 cd ..
 
 RUN_TIMES=$1

+ 3 - 1
port/linux/prof.sh

@@ -1 +1,3 @@
-gprof Release/benchmark/pikascript_benchmark gmon.out > report.txt
+cd Release && ninja -j0 && cd ..
+Release/performance/pikascript_performance
+gprof Release/performance/pikascript_performance gmon.out > report.txt

+ 14 - 8
src/PikaObj.c

@@ -479,14 +479,19 @@ static PikaObj* __obj_getObjDirect(PikaObj* self,
         return NULL;
     }
     /* finded object, check type*/
-    ArgType type = args_getType(self->list, name);
+    Arg* arg_obj = args_getArg(self->list, name);
+    ArgType type = ARG_TYPE_NONE;
+    if (NULL == arg_obj) {
+        return NULL;
+    }
+    type = arg_getType(arg_obj);
     /* found meta Object */
     if (type == ARG_TYPE_OBJECT_META) {
         return __obj_initSubObj(self, name);
     }
     /* found Objcet */
     if (argType_isObject(type)) {
-        return args_getPtr(self->list, name);
+        return arg_getPtr(arg_obj);
     }
 #if !PIKA_NANO_ENABLE
     /* found class */
@@ -494,8 +499,7 @@ static PikaObj* __obj_getObjDirect(PikaObj* self,
         type == ARG_TYPE_METHOD_CONSTRUCTOR) {
         *pIsTemp = PIKA_TRUE;
         PikaObj* method_args_obj = New_TinyObj(NULL);
-        Arg* cls_obj_arg = obj_runMethodArg(self, method_args_obj,
-                                            args_getArg(self->list, name));
+        Arg* cls_obj_arg = obj_runMethodArg(self, method_args_obj, arg_obj);
         obj_deinit(method_args_obj);
         if (type == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) {
             obj_runNativeMethod(arg_getPtr(cls_obj_arg), "__init__", NULL);
@@ -505,7 +509,7 @@ static PikaObj* __obj_getObjDirect(PikaObj* self,
         return res;
     }
 #endif
-    return _arg_to_obj(args_getArg(self->list, name), pIsTemp);
+    return _arg_to_obj(arg_obj, pIsTemp);
 }
 
 static PikaObj* __obj_getObjWithKeepDeepth(PikaObj* self,
@@ -514,7 +518,7 @@ static PikaObj* __obj_getObjWithKeepDeepth(PikaObj* self,
                                            int32_t keepDeepth) {
     char objPath_buff[PIKA_PATH_BUFF_SIZE];
     char* objPath_ptr = objPath_buff;
-    __platform_memcpy(objPath_buff, objPath, strGetSize(objPath) + 1);
+    strcpy(objPath_buff, objPath);
     int32_t token_num = strGetTokenNum(objPath, '.');
     PikaObj* obj = self;
     for (int32_t i = 0; i < token_num - keepDeepth; i++) {
@@ -1083,7 +1087,8 @@ PikaObj* obj_importModuleWithByteCode(PikaObj* self,
     PikaObj* New_PikaStdLib_SysObj(Args * args);
     if (!obj_isArgExist((PikaObj*)__pikaMain, name)) {
         obj_newDirectObj((PikaObj*)__pikaMain, name, New_PikaStdLib_SysObj);
-        pikaVM_runByteCode(obj_getObj((PikaObj*)__pikaMain, name), (uint8_t*)byteCode);
+        pikaVM_runByteCode(obj_getObj((PikaObj*)__pikaMain, name),
+                           (uint8_t*)byteCode);
     }
     if (self != (PikaObj*)__pikaMain) {
         Arg* module_arg = obj_getArg((PikaObj*)__pikaMain, name);
@@ -1151,7 +1156,8 @@ int obj_runModule(PikaObj* self, char* module_name) {
 
 int obj_importModule(PikaObj* self, char* module_name) {
     /* import bytecode of the module */
-    uint8_t* bytecode = obj_getByteCodeFromModule((PikaObj*)__pikaMain, module_name);
+    uint8_t* bytecode =
+        obj_getByteCodeFromModule((PikaObj*)__pikaMain, module_name);
     if (NULL == bytecode) {
         return 1;
     }

+ 15 - 5
src/PikaVM.c

@@ -863,7 +863,9 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
         if (tuple != NULL && (arg_num - i > variable_arg_start)) {
             list_append(&tuple->super, call_arg);
             /* the append would copy the arg */
-            arg_deinit(call_arg);
+            if (NULL != call_arg) {
+                arg_deinit(call_arg);
+            }
             continue;
         }
 
@@ -1292,7 +1294,9 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
             arg_deinit(return_arg_init);
         }
         obj_deinit(sub_locals);
-        arg_deinit(method_arg);
+        if (NULL != method_arg) {
+            arg_deinit(method_arg);
+        }
     }
 
     /* transfer sysOut */
@@ -2094,8 +2098,12 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self,
         goto exit;
     }
 exit:
-    arg_deinit(op.a1);
-    arg_deinit(op.a2);
+    if (NULL != op.a1) {
+        arg_deinit(op.a1);
+    }
+    if (NULL != op.a2) {
+        arg_deinit(op.a2);
+    }
     if (NULL != op.res) {
         return op.res;
     }
@@ -2211,7 +2219,9 @@ static Arg* VM_instruction_handler_ASS(PikaObj* self,
     }
 exit:
     arg_deinit(arg1);
-    arg_deinit(arg2);
+    if (NULL != arg2) {
+        arg_deinit(arg2);
+    }
     return res;
 }
 

+ 1 - 1
src/PikaVersion.h

@@ -2,4 +2,4 @@
 #define PIKA_VERSION_MINOR       11
 #define PIKA_VERSION_MICRO       3
 
-#define PIKA_EDIT_TIME      "2022/10/08 22:15:51"
+#define PIKA_EDIT_TIME      "2022/10/09 13:40:35"

+ 41 - 24
src/dataArg.c

@@ -93,19 +93,21 @@ static Arg* _arg_set_hash(Arg* self,
     /* create arg if not exist */
     if (NULL == self || self->size < size) {
         self = _arg_cache_pop(size);
-        if (PIKA_ASSERT_ENABLE) {
-            extern PikaMemInfo pikaMemInfo;
-            pikaMemInfo.alloc_times++;
-            pikaMemInfo.alloc_times_cache++;
-        }
+        uint32_t heap_size = sizeof(Arg) + size;
+#if PIKA_ARG_CACHE_ENABLE
+        // if (heap_size < PIKA_ARG_CACHE_SIZE) {
+        //     heap_size = PIKA_ARG_CACHE_SIZE;
+        // }
+        extern PikaMemInfo pikaMemInfo;
+        pikaMemInfo.alloc_times++;
+        pikaMemInfo.alloc_times_cache++;
+#endif
         if (NULL == self) {
-            self = (Arg*)pikaMalloc(sizeof(Arg) + size);
-            if (PIKA_ASSERT_ENABLE) {
-                extern PikaMemInfo pikaMemInfo;
-                pikaMemInfo.alloc_times_cache--;
-            }
+            self = (Arg*)pikaMalloc(heap_size);
 #if PIKA_ARG_CACHE_ENABLE
-            self->heap_size = mem_align(sizeof(Arg) + size);
+            extern PikaMemInfo pikaMemInfo;
+            pikaMemInfo.alloc_times_cache--;
+            self->heap_size = mem_align(heap_size);
 #endif
         }
         self->size = size;
@@ -113,13 +115,14 @@ static Arg* _arg_set_hash(Arg* self,
         arg_setSerialized(self, PIKA_TRUE);
         // arg_setIsKeyword(self, PIKA_FALSE);
         arg_setNext(self, next);
-        __platform_memset(arg_getContent(self), 0,
-                          aline_by(size, sizeof(uint32_t)));
     }
     self->name_hash = nameHash;
     self->type = type;
     if (NULL != content) {
         __platform_memcpy(arg_getContent(self), content, size);
+    } else {
+        __platform_memset(arg_getContent(self), 0,
+                          aline_by(size, sizeof(uint32_t)));
     }
     pika_assert(self->flag < ARG_FLAG_MAX);
     return self;
@@ -416,17 +419,33 @@ Arg* arg_copy_noalloc(Arg* arg_src, Arg* arg_dict) {
 Arg* arg_append(Arg* self, void* new_content, size_t new_size) {
     uint8_t* old_content = arg_getContent(self);
     size_t old_size = arg_getContentSize(self);
+    Arg* new_arg = NULL;
+#if PIKA_ARG_CACHE_ENABLE
     /* create arg_out */
-    Arg* arg_out = arg_setContent(NULL, NULL, old_size + new_size);
-    arg_setType(arg_out, arg_getType(self));
-    arg_setNameHash(arg_out, arg_getNameHash(self));
-    /* copy old content */
-    __platform_memcpy(arg_getContent(arg_out), old_content, old_size);
+    if (self->heap_size > mem_align(sizeof(Arg) + old_size + new_size)) {
+        new_arg = self;
+        new_arg->size = old_size + new_size;
+        extern PikaMemInfo pikaMemInfo;
+        pikaMemInfo.heapUsed += mem_align(sizeof(Arg) + old_size + new_size) -
+                                mem_align(sizeof(Arg) + old_size);
+    }
+#endif
+    if (NULL == new_arg) {
+        new_arg = arg_setContent(NULL, NULL, old_size + new_size);
+    }
+    arg_setType(new_arg, arg_getType(self));
+    arg_setNameHash(new_arg, arg_getNameHash(self));
+    if (self != new_arg) {
+        /* copy old content */
+        __platform_memcpy(arg_getContent(new_arg), old_content, old_size);
+    }
     /* copy new content */
-    __platform_memcpy(arg_getContent(arg_out) + old_size, new_content,
+    __platform_memcpy(arg_getContent(new_arg) + old_size, new_content,
                       new_size);
-    arg_deinit(self);
-    return arg_out;
+    if (self != new_arg) {
+        arg_deinit(self);
+    }
+    return new_arg;
 }
 
 void* arg_getHeapStruct(Arg* self) {
@@ -487,9 +506,7 @@ exit:
 }
 
 void arg_deinit(Arg* self) {
-    if (NULL == self) {
-        return;
-    }
+    pika_assert(NULL != self);
     /* deinit arg pointed heap */
     arg_deinitHeap(self);
     if (!arg_isSerialized(self)) {

+ 6 - 2
src/pika_config_valid.h

@@ -90,8 +90,12 @@
 
     #elif PIKA_OPTIMIZE == PIKA_OPTIMIZE_SPEED
         #ifndef PIKA_METHOD_CACHE_ENABLE
-            #define PIKA_METHOD_CACHE_ENABLE 1
-            #define PIKA_ARG_CACHE_ENABLE 1
+            #ifndef PIKA_METHOD_CACHE_ENABLE
+                #define PIKA_METHOD_CACHE_ENABLE 1
+            #endif
+            #ifndef PIKA_ARG_CACHE_ENABLE
+                #define PIKA_ARG_CACHE_ENABLE 1
+            #endif
         #endif
     #endif
     

+ 0 - 34
test/VM-test.cpp

@@ -2051,37 +2051,3 @@ TEST(vm, benchmark) {
     obj_deinit(pikaMain);
     EXPECT_EQ(pikaMemNow(), 0);
 }
-
-TEST(vm, for_print_1k) {
-    /* init */
-    pikaMemInfo.heapUsedMax = 0;
-    PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
-    extern unsigned char pikaModules_py_a[];
-    obj_linkLibrary(pikaMain, pikaModules_py_a);
-    /* run */
-    __platform_printf("BEGIN\r\n");
-    /* clang-format off */
-    PIKA_PYTHON(
-    for i in range(1000):
-        print(i)
-    )
-    /* clang-format on */
-    const uint8_t bytes[] = {
-        0x30, 0x00, /* instruct array size */
-        0x20, 0x85, 0x01, 0x00, 0x10, 0x02, 0x06, 0x00, 0x00, 0x02, 0x0c, 0x00,
-        0x00, 0x04, 0x11, 0x00, 0x00, 0x82, 0x15, 0x00, 0x00, 0x04, 0x22, 0x00,
-        0x00, 0x0d, 0x22, 0x00, 0x00, 0x07, 0x24, 0x00, 0x11, 0x81, 0x22, 0x00,
-        0x01, 0x02, 0x26, 0x00, 0x00, 0x86, 0x2c, 0x00, 0x00, 0x8c, 0x11, 0x00,
-        /* instruct array */
-        0x2f, 0x00, /* const pool size */
-        0x00, 0x31, 0x30, 0x30, 0x30, 0x00, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x00,
-        0x69, 0x74, 0x65, 0x72, 0x00, 0x24, 0x6c, 0x30, 0x00, 0x24, 0x6c, 0x30,
-        0x2e, 0x5f, 0x5f, 0x6e, 0x65, 0x78, 0x74, 0x5f, 0x5f, 0x00, 0x69, 0x00,
-        0x32, 0x00, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x00, 0x2d, 0x31,
-        0x00, /* const pool */
-    };
-    pikaVM_runByteCode(pikaMain, (uint8_t*)bytes);
-    /* deinit */
-    obj_deinit(pikaMain);
-    EXPECT_EQ(pikaMemNow(), 0);
-}

+ 1 - 1
test/main.cpp

@@ -10,8 +10,8 @@ int main(int argc, char** argv) {
     ::testing::InitGoogleTest(&argc, argv);
     int res = RUN_ALL_TESTS();
     mem_pool_deinit();
-    extern PikaMemInfo pikaMemInfo;
 #if PIKA_ARG_CACHE_ENABLE
+    extern PikaMemInfo pikaMemInfo;
     printf("[ Info]: alloc times: %d, cached times: %d (%0.2f%%)\r\n",
            pikaMemInfo.alloc_times, pikaMemInfo.alloc_times_cache,
            ((float)pikaMemInfo.alloc_times_cache /