c++ - Optimize this function? -


i have written code (uses v8 library). went through couple of times , feels it's way can write this. aim of function replace javascript .split() function; when using function limit doesn't include last part of array in returning array. eg:

var str = "test split string limit"; var out = str.split(' ', 2); 

the array out contain: [test, split]. want contain: [test, split, string limit].

i know there pure js ways find them hacky , possibly slower(?) single c++ bind call.

here's function:

/**  * explodes string limits tokens  * @param input  * @param delim  * @param limit  * @return  */ void asengine::engine::asstrtok(const v8::functioncallbackinfo<v8::value>& args) {     assert(3, args);      isolate* isolate = args.getisolate();      /* args */     string::utf8value a1(args[0]);     string::utf8value a2(args[1]);     local<uint32> a3 = args[2]->touint32();      std::string input  = std::string(*a1);     std::string delim  = std::string(*a2);     unsigned int limit = a3->int32value();      unsigned int inputlen = input.length();      // declare temporary array shove return later     std::vector<char*> tmp;     tmp.reserve(limit);      unsigned int delimlen = delim.length();     char* cp = (char*) malloc(inputlen);     char* cursor = cp + inputlen; // cursor     char* cpp = (char*) cp; // keep start of string      // copy haystack modifyable char ptr     memset(cp + inputlen, 0x00, 1);     memcpy(cp, input.c_str(), inputlen);      unsigned int arrayindex = 0;     for(unsigned int i=0;i<limit;i++)     {         if((cursor = strstr(cp, delim.c_str())) == null)         {             cursor = (char*) cpp + inputlen;             break;         }          for(int j=0;j<delimlen;j++)             *(cursor+j) = 0x00;          tmp.push_back(cp);          cp = cursor + delimlen;         arrayindex++;     }     if(*(cp) != '\0')     {         arrayindex++;         tmp.push_back(cp);     }      handle<array> rtn = array::new(args.getisolate(), arrayindex);      /* loop through temporary array , assign        variables v8 array */     for(unsigned int i=0;i<arrayindex;i++)     {         rtn->set(i, string::newfromutf8(             isolate, tmp[i], string::knormalstring, strlen(tmp[i])         ));     }      /* clean memory */     delete cpp;     cp      = null;     cpp     = null;     cursor  = null;     isolate = null;      /* set return */     args.getreturnvalue().set(rtn); } 

if wondering: variable cpp there can delete character pointer after done (as calling v8's string::newfromutf8() function copies string) , modify cp pointer during process of function.

before optimising, fix code correct.

char* cp = (char*) malloc(inputlen); ...   /* clean memory */ delete cpp; 

whilst in implementations, new , malloc same thing, other implementations not. so, if allocate malloc, use free free memory, not delete.

if want clever it, expect:

tmp.reserve(limit+1); 

will ensure have space remainder of string without further allocation in vector.

since cursor isn't used after loop, setting inside if breaks loop makes no sense.

    if((cursor = strstr(cp, delim.c_str())) == null)     {         cursor = (char*) cpp + inputlen;         break;     } 

you using casts (char *) in places don't need it, example:

char* cpp = (char*) cp; // keep start of string 

(cp char * already).

this:

memset(cp + inputlen, 0x00, 1); 

is same as:

cp[inputlen] = 0;  

but unless compiler inlines memset, faster.

likewsie:

*(cursor+j) = 0x00; 

can written:

cursor[j] = 0;  

however, assuming delimlen greater 1, away with:

    for(int j=0;j<delimlen;j++)         *(cursor+j) = 0x00; 

converted to:

  *cursor = 0;  

since new cp value skip beyond delimlen anyway.

these serve absolutely no purpose:

cp      = null; cpp     = null; cursor  = null; isolate = null; 

unfortunately, expect of time in function won't in of code i've commented on. in passing arguments , forth between calling js library , native c++ code. i'd surprised if gain on writing same code in js. (none of above make of difference when comes speed, it's correctness , "a small number of potentially wasted cycles if compiler rather daft").


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -