optimize speed for string funs

optimize str sppeed

add prof.sh

not -pg in default
This commit is contained in:
pikastech 2022-10-06 12:22:13 +08:00
parent 31d4b91df3
commit f5efb11a75
16 changed files with 78 additions and 64 deletions

View File

@ -251,7 +251,7 @@ PikaObj* PikaStdData_String_split(PikaObj* self, char* s) {
int token_num = strCountSign(str, sign) + 1;
for (int i = 0; i < token_num; i++) {
char* token = strsPopToken(&buffs, str, sign);
char* token = strsPopToken(&buffs, &str, sign);
/* 用 arg_set<type> 的 api 创建 arg */
Arg* token_arg = arg_newStr(token);
/* 添加到 list 对象 */

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
// "--gtest_filter=vm.i_pp"
"--gtest_filter=vm.*"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -7,6 +7,11 @@ ENDIF(PIKA_CONFIG_ENABLE)
project(pikascript VERSION 0.1.0)
# SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pg")
# SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -pg")
# SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -pg")
# SET(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -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")

View File

@ -251,7 +251,7 @@ PikaObj* PikaStdData_String_split(PikaObj* self, char* s) {
int token_num = strCountSign(str, sign) + 1;
for (int i = 0; i < token_num; i++) {
char* token = strsPopToken(&buffs, str, sign);
char* token = strsPopToken(&buffs, &str, sign);
/* 用 arg_set<type> 的 api 创建 arg */
Arg* token_arg = arg_newStr(token);
/* 添加到 list 对象 */

1
port/linux/prof.sh Normal file
View File

@ -0,0 +1 @@
gprof build/test/pikascript_test gmon.out > report.txt

View File

@ -505,12 +505,12 @@ static PikaObj* __obj_getObjWithKeepDeepth(PikaObj* self,
PIKA_BOOL* pIsTemp,
int32_t keepDeepth) {
char objPath_buff[PIKA_PATH_BUFF_SIZE];
char* objPath_ptr = objPath_buff;
__platform_memcpy(objPath_buff, objPath, strGetSize(objPath) + 1);
char token_buff[PIKA_NAME_BUFF_SIZE] = {0};
int32_t token_num = strGetTokenNum(objPath, '.');
PikaObj* obj = self;
for (int32_t i = 0; i < token_num - keepDeepth; i++) {
char* token = strPopToken(token_buff, objPath_buff, '.');
char* token = strPopFirstToken(&objPath_ptr, '.');
obj = __obj_getObjDirect(obj, token, pIsTemp);
if (obj == NULL) {
goto exit;
@ -555,11 +555,10 @@ char* methodArg_getTypeList(Arg* method_arg, char* buffs, size_t size) {
char* methodArg_getName(Arg* method_arg, char* buffs, size_t size) {
char* method_dec = strCopy(buffs, methodArg_getDec(method_arg));
char res[PIKA_NAME_BUFF_SIZE] = {0};
if (strGetSize(method_dec) > size) {
return NULL;
}
strPopToken(res, method_dec, '(');
char* res = strPopFirstToken(&method_dec, '(');
strCopy(buffs, res);
return buffs;
}

View File

@ -49,7 +49,7 @@ void Cursor_beforeIter(struct Cursor* cs);
void Cursor_iterStart(struct Cursor* cs);
void Cursor_iterEnd(struct Cursor* cs);
char* Cursor_popToken(Args* buffs, char** tokens, char* devide);
char* Parser_popToken(Args* buffs_p, char* tokens);
char* Parser_popToken(Args* buffs_p, char** tokens);
uint16_t Tokens_getSize(char* tokens) {
if (strEqu("", tokens)) {
@ -337,7 +337,7 @@ char* Lexer_printTokens(Args* outBuffs, char* tokens) {
/* process */
uint16_t token_size = Tokens_getSize(tokens);
for (uint16_t i = 0; i < token_size; i++) {
char* token = Parser_popToken(&buffs, tokens);
char* token = Parser_popToken(&buffs, &tokens);
if (token[0] == TOKEN_operator) {
printOut = strsAppend(&buffs, printOut, "{opt}");
printOut = strsAppend(&buffs, printOut, token + 1);
@ -742,7 +742,7 @@ char* Lexer_parseLine(Args* outBuffs, char* stmt) {
return tokens;
}
char* Parser_popToken(Args* buffs_p, char* tokens) {
char* Parser_popToken(Args* buffs_p, char** tokens) {
return strsPopToken(buffs_p, tokens, 0x1F);
}
@ -762,7 +762,7 @@ uint8_t Parser_isContainToken(char* tokens,
uint8_t res = 0;
uint16_t token_size = Tokens_getSize(tokens);
for (int i = 0; i < token_size; i++) {
char* token = Parser_popToken(&buffs, tokens_buff);
char* token = Parser_popToken(&buffs, &tokens_buff);
if (token_type == Token_getType(token)) {
if (strEqu(Token_getPyload(token), pyload)) {
res = 1;
@ -858,7 +858,7 @@ void Cursor_iterStart(struct Cursor* cs) {
/* token1 is the last token */
cs->token1.token = strsCopy(cs->iter_buffs, arg_getStr(cs->last_token));
/* token2 is the next token */
cs->token2.token = Parser_popToken(cs->iter_buffs, cs->tokens);
cs->token2.token = Parser_popToken(cs->iter_buffs, &cs->tokens);
/* store last token */
arg_deinit(cs->last_token);
cs->last_token = arg_newStr(cs->token2.token);
@ -933,8 +933,8 @@ void Cursor_beforeIter(struct Cursor* cs) {
if (cs->result != PIKA_RES_OK) {
return;
}
Parser_popToken(cs->buffs_p, cs->tokens);
cs->last_token = arg_newStr(Parser_popToken(cs->buffs_p, cs->tokens));
Parser_popToken(cs->buffs_p, &cs->tokens);
cs->last_token = arg_newStr(Parser_popToken(cs->buffs_p, &cs->tokens));
}
char* Cursor_popToken(Args* buffs, char** tokens, char* devide) {
@ -1707,12 +1707,12 @@ char* _defGetDefault(Args* outBuffs, char** dec_out) {
char* arg_list = strsCut(&buffs, dec_str, '(', ')');
int arg_num = strCountSign(arg_list, ',') + 1;
for (int i = 0; i < arg_num; i++) {
char* arg_str = strsPopToken(&buffs, arg_list, ',');
char* arg_str = strsPopToken(&buffs, &arg_list, ',');
int is_default = 0;
if (strIsContain(arg_str, '=')) {
default_arg = arg_strAppend(default_arg, arg_str);
default_arg = arg_strAppend(default_arg, ",");
arg_str = strsPopToken(&buffs, arg_str, '=');
arg_str = strsPopToken(&buffs, &arg_str, '=');
is_default = 1;
}
dec_arg = arg_strAppend(dec_arg, arg_str);
@ -1844,10 +1844,10 @@ AST* AST_parseLine_withBlockStack_withBlockDeepth(char* line,
ast = NULL;
goto exit;
}
char* arg_in = strsPopToken(list_buffs, line_buff, ' ');
char* arg_in = strsPopToken(list_buffs, &line_buff, ' ');
AST_setNodeAttr(ast, "arg_in", arg_in);
strsPopToken(list_buffs, line_buff, ' ');
char* list_in = strsPopToken(list_buffs, line_buff, ':');
strsPopToken(list_buffs, &line_buff, ' ');
char* list_in = strsPopToken(list_buffs, &line_buff, ':');
list_in = strsAppend(list_buffs, "iter(", list_in);
list_in = strsAppend(list_buffs, list_in, ")");
list_in = strsCopy(&buffs, list_in);
@ -1898,7 +1898,7 @@ AST* AST_parseLine_withBlockStack_withBlockDeepth(char* line,
}
if (strIsStartWith(line_start, "return ")) {
char* lineBuff = strsCopy(&buffs, line_start);
strsPopToken(&buffs, lineBuff, ' ');
strsPopToken(&buffs, &lineBuff, ' ');
stmt = lineBuff;
stmt = Suger_multiReturn(&buffs, stmt);
AST_setNodeAttr(ast, "return", "");
@ -1914,7 +1914,7 @@ AST* AST_parseLine_withBlockStack_withBlockDeepth(char* line,
if (strIsStartWith(line_start, "raise ")) {
AST_setNodeAttr(ast, "raise", "");
char* lineBuff = strsCopy(&buffs, line_start);
strsPopToken(&buffs, lineBuff, ' ');
strsPopToken(&buffs, &lineBuff, ' ');
stmt = lineBuff;
if (strEqu("", stmt)) {
stmt = "RuntimeError";
@ -2237,7 +2237,7 @@ char* Parser_LineToAsm(Args* buffs_p, char* line, Stack* blockStack) {
*/
line_num = strCountSign(line, '\n') + 1;
for (int i = 0; i < line_num; i++) {
char* single_line = strsPopToken(buffs_p, line, '\n');
char* single_line = strsPopToken(buffs_p, &line, '\n');
/* parse tokens to AST */
ast = AST_parseLine_withBlockStack(single_line, blockStack);
/* gen ASM from AST */
@ -2748,7 +2748,7 @@ char* AST_genAsm(AST* ast, Args* outBuffs) {
if (NULL != defaultStmts) {
int stmt_num = strGetTokenNum(defaultStmts, ',');
for (int i = 0; i < stmt_num; i++) {
char* stmt = strsPopToken(&buffs, defaultStmts, ',');
char* stmt = strsPopToken(&buffs, &defaultStmts, ',');
char* arg_name = strsGetFirstToken(&buffs, stmt, '=');
pikaAsm = ASM_addBlockDeepth(ast, &buffs, pikaAsm, 1);
pikaAsm = strsAppend(&buffs, pikaAsm, "0 EST ");

View File

@ -1453,9 +1453,8 @@ static Arg* VM_instruction_handler_OUT(PikaObj* self,
Arg* global_list_arg = arg_newStr(global_list);
char* global_list_buff = arg_getStr(global_list_arg);
/* for each arg arg in global_list */
char token_buff[PIKA_NAME_BUFF_SIZE] = {0};
for (int i = 0; i < strCountSign(global_list, ',') + 1; i++) {
char* global_arg = strPopToken(token_buff, global_list_buff, ',');
char* global_arg = strPopFirstToken(&global_list_buff, ',');
/* matched global arg, context set to global */
if (strEqu(global_arg, arg_path)) {
context = vm->globals;

View File

@ -59,13 +59,13 @@ static Arg* _arg_set_hash(Arg* self,
self->size = size;
self->flag = 0;
arg_setSerialized(self, PIKA_TRUE);
arg_setIsKeyword(self, PIKA_FALSE);
// 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;
__platform_memset(arg_getContent(self), 0,
aline_by(size, sizeof(uint32_t)));
if (NULL != content) {
__platform_memcpy(arg_getContent(self), content, size);
}

View File

@ -738,13 +738,13 @@ char* strsFormatList(Args* out_buffs, char* fmt, PikaList* list) {
Args buffs = {0};
char* res = NULL;
char* fmt_buff = strsCopy(&buffs, fmt);
char* fmt_item = strsPopToken(&buffs, fmt_buff, '%');
char* fmt_item = strsPopToken(&buffs, &fmt_buff, '%');
Arg* res_buff = arg_newStr(fmt_item);
for (size_t i = 0; i < list_getSize(list); i++) {
Args buffs_item = {0};
Arg* arg = list_getArg(list, i);
char* fmt_item = strsPopToken(&buffs_item, fmt_buff, '%');
char* fmt_item = strsPopToken(&buffs_item, &fmt_buff, '%');
fmt_item = strsAppend(&buffs_item, "%", fmt_item);
char* str_format = strsFormatArg(&buffs_item, fmt_item, arg);
if (NULL == str_format) {

View File

@ -91,12 +91,12 @@ char* strAppendWithSize(char* strOut, char* pData, int32_t Size) {
}
int32_t strCountSign(char* strIn, char sign) {
int32_t count = 0;
size_t len = strGetSize(strIn);
for (uint32_t i = 0; i < len; i++) {
if (sign == strIn[i]) {
int count = 0;
while (*strIn) {
if (*strIn == sign) {
count++;
}
strIn++;
}
return count;
}
@ -150,30 +150,18 @@ char* strGetLastToken(char* strOut, char* strIn, char sign) {
return strOut;
}
char* strPopToken(char* strOut, char* strIn, char sign) {
int32_t getSign = 0;
int32_t iPoped = 0;
int32_t iOut = 0;
int32_t size = strGetSize(strIn);
int32_t i = 0;
for (i = 0; i < size; i++) {
if (getSign) {
strIn[iPoped] = strIn[i];
iPoped++;
continue;
}
if (strIn[i] != sign) {
strOut[iOut++] = strIn[i];
continue;
}
if (strIn[i] == sign) {
getSign = 1;
continue;
}
char* strPopFirstToken(char** strIn, char sign) {
char* strIn_ = *strIn;
char* pos = strchr(strIn_, sign);
if (pos != NULL) {
/* found the first sign */
*pos = 0;
*strIn = pos + 1;
return strIn_;
}
strOut[iOut] = 0;
strIn[iPoped] = 0;
return strOut;
/* no found */
*strIn = strchr(strIn_, 0);
return strIn_;
}
char* strGetFirstToken(char* strOut, char* strIn, char sign) {

View File

@ -47,7 +47,7 @@ char* strDeleteChar(char* strOut, char* strIn, char ch);
char* strRemovePrefix(char* inputStr, char* prefix, char* outputStr);
/* token */
int32_t strGetToken(char* string, char** argv, char sign);
char* strPopToken(char* strOut, char* strIn, char sign);
char* strPopFirstToken(char** strIn, char sign);
int32_t strCountSign(char* strIn, char sign);
int32_t strGetTokenNum(char* strIn, char sign);
char* strGetFirstToken(char* strOut, char* strIn, char sign);

View File

@ -94,10 +94,8 @@ char* strsGetFirstToken(Args* buffs_p, char* strIn, char sign) {
return strGetFirstToken(args_getBuff(buffs_p, size), strIn, sign);
}
char* strsPopToken(Args* buffs_p, char* tokens, char sign) {
int32_t size = strGetSize(tokens);
char* buff = args_getBuff(buffs_p, size);
return strPopToken(buff, tokens, sign);
char* strsPopToken(Args* buffs_p, char** tokens, char sign) {
return strsCopy(buffs_p, strPopFirstToken(tokens, sign));
}
char* strsCopy(Args* buffs_p, char* source) {

View File

@ -31,7 +31,7 @@
Args* New_strBuff(void);
char* strsGetFirstToken(Args* buffs, char* strIn, char sign);
char* strsGetLastToken(Args* buffs, char* arg_Path, char sign);
char* strsPopToken(Args* buffs, char* tokens, char sign);
char* strsPopToken(Args* buffs, char** tokens, char sign);
char* strsCopy(Args* buffs, char* source);
char* strsDeleteChar(Args* buff, char* strIn, char ch);
char* strsCut(Args* buffs, char* strIn, char startSign, char endSign);

View File

@ -270,3 +270,27 @@ TEST(string, utf8_1) {
EXPECT_EQ(pikaMemNow(), 0);
}
#endif
TEST(string, popfirsttoken) {
char test[] = "a,b,c, d";
char* p = test;
char* token = strPopFirstToken(&p, ',');
EXPECT_STREQ(token, "a");
EXPECT_STREQ(p, "b,c, d");
}
TEST(string, popfirsttoken_no) {
char test[] = "a,b,c, d";
char* p = test;
char* token = strPopFirstToken(&p, '.');
EXPECT_STREQ(token, "a,b,c, d");
EXPECT_STREQ(p, "");
}
TEST(string, popfirsttoken_void) {
char test[] = "";
char* p = test;
char* token = strPopFirstToken(&p, '.');
EXPECT_STREQ(token, "");
EXPECT_STREQ(p, "");
}

View File

@ -48,7 +48,7 @@ TEST(strs, analizeDef) {
int argNum = strCountSign(typeList, ',') + 1;
char* typeListBuff = strsCopy(buffs, typeList);
for (int i = 0; i < argNum; i++) {
char* typeDeclareation = strsPopToken(buffs, typeListBuff, ',');
char* typeDeclareation = strsPopToken(buffs, &typeListBuff, ',');
printInfo("typeDeclareation", typeDeclareation);
char* argName = strsGetFirstToken(buffs, typeDeclareation, ':');
printInfo("argName", argName);