From c0451c376802073838f629bff75b9a61a30362ca Mon Sep 17 00:00:00 2001 From: shennetsind Date: Thu, 5 Mar 2015 13:48:49 -0300 Subject: Memory Slasher: Script Parser Boost Noticed this improvement while working on the ultimate localization design. During my experiments I noticed an average 11% speed mod. This patch makes script (item/pet/npc) parsing 11% faster, while dropping map server memory usage by 2.7MB (its faster and uses less memory, signature Hercules achievement). Signed-off-by: shennetsind --- src/map/script.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'src/map/script.c') diff --git a/src/map/script.c b/src/map/script.c index c01e8c391..235b7b8d5 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -2427,9 +2427,11 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o script->syntax.translation_db = strdb_get(script->translation_db, script->parser_current_npc_name); } - script->buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char)); + if( !script->buf ) { + script->buf = (unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char)); + script->size = SCRIPT_BLOCK_SIZE; + } script->pos=0; - script->size=SCRIPT_BLOCK_SIZE; script->parse_nextline(true, NULL); // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here @@ -2443,10 +2445,7 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o if( script->error_report ) script->error(src,file,line,script->error_msg,script->error_pos); aFree( script->error_msg ); - aFree( script->buf ); script->pos = 0; - script->size = 0; - script->buf = NULL; for(i=LABEL_START;istr_num;i++) if(script->str_data[i].type == C_NOP) script->str_data[i].type = C_NAME; for(i=0; ibuf ); script->pos = 0; - script->size = 0; - script->buf = NULL; #ifdef ENABLE_CASE_CHECK script->local_casecheck.clear(); script->parser_current_src = NULL; @@ -2491,10 +2487,7 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o p = script->skip_space(p+1); if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) ) {// empty script and can return NULL - aFree( script->buf ); script->pos = 0; - script->size = 0; - script->buf = NULL; #ifdef ENABLE_CASE_CHECK script->local_casecheck.clear(); script->parser_current_src = NULL; @@ -2543,10 +2536,6 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o script->addc(C_NOP); - // trim code to size - script->size = script->pos; - RECREATE(script->buf,unsigned char,script->pos); - // default unknown references to variables for (i = LABEL_START; i < script->str_num; i++) { if (script->str_data[i].type == C_NOP) { @@ -2611,8 +2600,9 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o #endif CREATE(code,struct script_code,1); - code->script_buf = script->buf; - code->script_size = script->size; + code->script_buf = (unsigned char *)aMalloc(script->pos*sizeof(unsigned char)); + memcpy(code->script_buf, script->buf, script->pos); + code->script_size = script->pos; code->local.vars = NULL; code->local.arrays = NULL; #ifdef ENABLE_CASE_CHECK @@ -4970,6 +4960,13 @@ int script_translation_db_destroyer(DBKey key, DBData *data, va_list ap) { * **/ void script_parser_clean_leftovers(void) { + + if( script->buf ) + aFree(script->buf); + + script->buf = NULL; + script->size = 0; + if( script->translation_db ) { script->translation_db->destroy(script->translation_db,script->translation_db_destroyer); script->translation_db = NULL; -- cgit v1.2.3-60-g2f50