Minor optimization for strlen and memcpy calls for reading blend files
authorSv. Lockal <lockalsash@gmail.com>
Sun, 15 Dec 2013 14:06:04 +0000 (18:06 +0400)
committerSv. Lockal <lockalsash@gmail.com>
Sun, 15 Dec 2013 14:06:04 +0000 (18:06 +0400)
Summary:
This commit removes ~10000 strlen calls and ~100000 memcpy calls in blender (profiled with blender --background), ~10000 memcpy calls in makesdna.

There is no need to create null-terminated strings for atoi, because it converts only the initial portion of the string anyway.
Also it was noticed that DNA_elem_array_size and arraysize functions work only with full strings, so there is no point to calculate strlen.

Reviewers: campbellbarton

Reviewed By: campbellbarton

Differential Revision: http://developer.blender.org/D105

source/blender/makesdna/DNA_genfile.h
source/blender/makesdna/intern/dna_genfile.c
source/blender/makesdna/intern/makesdna.c
source/blender/makesrna/intern/rna_define.c

index 96d71288c62aeb196141dc931a4a0ff6aa6a3c3f..9a6c714469226546f484864b6059c20e7aadb2aa 100644 (file)
@@ -68,7 +68,7 @@ void DNA_struct_switch_endian(struct SDNA *oldsdna, int oldSDNAnr, char *data);
 char *DNA_struct_get_compareflags(struct SDNA *sdna, struct SDNA *newsdna);
 void *DNA_struct_reconstruct(struct SDNA *newsdna, struct SDNA *oldsdna, char *compflags, int oldSDNAnr, int blocks, void *data);
 
-int DNA_elem_array_size(const char *astr, int len);
+int DNA_elem_array_size(const char *str);
 int DNA_elem_offset(struct SDNA *sdna, const char *stype, const char *vartype, const char *name);
 
 bool DNA_struct_elem_find(struct SDNA *sdna, const char *stype, const char *vartype, const char *name);
index ed7f6f5d765f987de9dcbf624f57f6b4c3ec30a4..eca06242da0fbc38d25272d405262f9d68a10682 100644 (file)
@@ -178,19 +178,16 @@ static int le_int(int temp)
 /**
  * parses the "[n]" on the end of an array name and returns the number of array elements n.
  */
-int DNA_elem_array_size(const char *astr, int len)
+int DNA_elem_array_size(const char *str)
 {
        int a, mul = 1;
-       char str[100], *cp = NULL;
+       const char *cp = NULL;
 
-       memcpy(str, astr, len + 1);
-
-       for (a = 0; a < len; a++) {
+       for (a = 0; str[a]; a++) {
                if (str[a] == '[') {
                        cp = &(str[a + 1]);
                }
                else if (str[a] == ']' && cp) {
-                       str[a] = 0;
                        mul *= atoi(cp);
                }
        }
@@ -246,7 +243,7 @@ static int elementsize(const SDNA *sdna, short type, short name)
                /* has the name an extra length? (array) */
                mul = 1;
                if (cp[namelen - 1] == ']') {
-                       mul = DNA_elem_array_size(cp, namelen);
+                       mul = DNA_elem_array_size(cp);
                }
                
                len = sdna->pointerlen * mul;
@@ -255,7 +252,7 @@ static int elementsize(const SDNA *sdna, short type, short name)
                /* has the name an extra length? (array) */
                mul = 1;
                if (cp[namelen - 1] == ']') {
-                       mul = DNA_elem_array_size(cp, namelen);
+                       mul = DNA_elem_array_size(cp);
                }
                
                len = mul * sdna->typelens[type];
@@ -729,7 +726,7 @@ static void cast_elem(
 
        eSDNA_Type ctypenr, otypenr;
 
-       arrlen = DNA_elem_array_size(name, strlen(name));
+       arrlen = DNA_elem_array_size(name);
 
        if ( (otypenr = sdna_type_nr(otype)) == -1 ||
             (ctypenr = sdna_type_nr(ctype)) == -1)
@@ -820,7 +817,7 @@ static void cast_pointer(int curlen, int oldlen, const char *name, char *curdata
 #endif
        int arrlen;
        
-       arrlen = DNA_elem_array_size(name, strlen(name));
+       arrlen = DNA_elem_array_size(name);
        
        while (arrlen > 0) {
        
@@ -990,8 +987,8 @@ static void reconstruct_elem(
 
                        if (oname[countpos] == '[' && strncmp(name, oname, countpos) == 0) {  /* basis equal */
                                
-                               cursize = DNA_elem_array_size(name, strlen(name));
-                               oldsize = DNA_elem_array_size(oname, strlen(oname));
+                               cursize = DNA_elem_array_size(name);
+                               oldsize = DNA_elem_array_size(oname);
 
                                if (ispointer(name)) {  /* handle pointer or functionpointer */
                                        cast_pointer(newsdna->pointerlen, oldsdna->pointerlen,
@@ -1091,9 +1088,9 @@ static void reconstruct_struct(
                                curSDNAnr = DNA_struct_find_nr(newsdna, type);
                                
                                /* array! */
-                               mul = DNA_elem_array_size(name, strlen(name));
+                               mul = DNA_elem_array_size(name);
                                nameo = oldsdna->names[sppo[1]];
-                               mulo = DNA_elem_array_size(nameo, strlen(nameo));
+                               mulo = DNA_elem_array_size(nameo);
                                
                                eleno = elementsize(oldsdna, sppo[0], sppo[1]);
                                
@@ -1164,7 +1161,7 @@ void DNA_struct_switch_endian(SDNA *oldsdna, int oldSDNAnr, char *data)
                        if (cpo) {
                                oldSDNAnr = DNA_struct_find_nr(oldsdna, type);
                                
-                               mul = DNA_elem_array_size(name, strlen(name));
+                               mul = DNA_elem_array_size(name);
                                elena = elen / mul;
 
                                while (mul--) {
@@ -1178,7 +1175,7 @@ void DNA_struct_switch_endian(SDNA *oldsdna, int oldSDNAnr, char *data)
                        if (ispointer(name)) {
                                if (oldsdna->pointerlen == 8) {
                                        
-                                       mul = DNA_elem_array_size(name, strlen(name));
+                                       mul = DNA_elem_array_size(name);
                                        cpo = cur;
                                        while (mul--) {
                                                cval = cpo[0]; cpo[0] = cpo[7]; cpo[7] = cval;
@@ -1207,7 +1204,7 @@ void DNA_struct_switch_endian(SDNA *oldsdna, int oldSDNAnr, char *data)
                                        }
                                        
                                        if (skip == false) {
-                                               mul = DNA_elem_array_size(name, strlen(name));
+                                               mul = DNA_elem_array_size(name);
                                                cpo = cur;
                                                while (mul--) {
                                                        cval = cpo[0];
@@ -1223,7 +1220,7 @@ void DNA_struct_switch_endian(SDNA *oldsdna, int oldSDNAnr, char *data)
                                           spc[0] == SDNA_TYPE_FLOAT))
                                {
 
-                                       mul = DNA_elem_array_size(name, strlen(name));
+                                       mul = DNA_elem_array_size(name);
                                        cpo = cur;
                                        while (mul--) {
                                                cval = cpo[0];
@@ -1238,7 +1235,7 @@ void DNA_struct_switch_endian(SDNA *oldsdna, int oldSDNAnr, char *data)
                                else if ( (spc[0] == SDNA_TYPE_INT64) ||
                                          (spc[0] == SDNA_TYPE_UINT64))
                                {
-                                       mul = DNA_elem_array_size(name, strlen(name));
+                                       mul = DNA_elem_array_size(name);
                                        cpo = cur;
                                        while (mul--) {
                                                cval = cpo[0]; cpo[0] = cpo[7]; cpo[7] = cval;
index aac79245501124558896c3f6e673f5ca5c9aa8dc..39bf5ec437f1c337a8a10679c570d05e0ccd4c2a 100644 (file)
@@ -201,7 +201,7 @@ static int convert_include(char *filename);
 /**
  * Determine how many bytes are needed for an array.
  */ 
-static int arraysize(char *astr, int len);
+static int arraysize(const char *str);
 
 /**
  * Determine how many bytes are needed for each struct.
@@ -699,19 +699,16 @@ static int convert_include(char *filename)
        return 0;
 }
 
-static int arraysize(char *astr, int len)
+static int arraysize(const char *str)
 {
        int a, mul = 1;
-       char str[100], *cp = NULL;
-
-       memcpy(str, astr, len + 1);
+       const char *cp = NULL;
        
-       for (a = 0; a < len; a++) {
+       for (a = 0; str[a]; a++) {
                if (str[a] == '[') {
                        cp = &(str[a + 1]);
                }
                else if (str[a] == ']' && cp) {
-                       str[a] = 0;
                        /* if 'cp' is a preprocessor definition, it will evaluate to 0,
                         * the caller needs to check for this case and throw an error */
                        mul *= atoi(cp);
@@ -725,7 +722,7 @@ static int calculate_structlens(int firststruct)
 {
        int a, b, len_native, len_64, unknown = nr_structs, lastunknown, structtype, type, mul, namelen;
        short *sp, *structpoin;
-       char *cp;
+       const char *cp;
        int has_pointer, dna_error = 0;
                
        while (unknown) {
@@ -756,7 +753,7 @@ static int calculate_structlens(int firststruct)
                                                has_pointer = 1;
                                                /* has the name an extra length? (array) */
                                                mul = 1;
-                                               if (cp[namelen - 1] == ']') mul = arraysize(cp, namelen);
+                                               if (cp[namelen - 1] == ']') mul = arraysize(cp);
 
                                                if (mul == 0) {
                                                        printf("Zero array size found or could not parse %s: '%.*s'\n", types[structtype], namelen + 1, cp);
@@ -794,7 +791,7 @@ static int calculate_structlens(int firststruct)
                                        else if (typelens_native[type]) {
                                                /* has the name an extra length? (array) */
                                                mul = 1;
-                                               if (cp[namelen - 1] == ']') mul = arraysize(cp, namelen);
+                                               if (cp[namelen - 1] == ']') mul = arraysize(cp);
 
                                                if (mul == 0) {
                                                        printf("Zero array size found or could not parse %s: '%.*s'\n", types[structtype], namelen + 1, cp);
index fe38ce2d86a669ea0625dc63d9f69ddf7d4ea11a..3a5a1d30cd8e1d77416ae3386b7a479f60de2a19 100644 (file)
@@ -348,7 +348,7 @@ static int rna_find_sdna_member(SDNA *sdna, const char *structname, const char *
                        if (strstr(membername, "["))
                                smember->arraylength = 0;
                        else
-                               smember->arraylength = DNA_elem_array_size(smember->name, strlen(smember->name));
+                               smember->arraylength = DNA_elem_array_size(smember->name);
 
                        smember->pointerlevel = 0;
                        for (b = 0; dnaname[b] == '*'; b++)