diff options
author | Sv. Lockal <lockalsash@gmail.com> | 2013-12-15 18:06:04 +0400 |
---|---|---|
committer | Sv. Lockal <lockalsash@gmail.com> | 2013-12-15 18:06:04 +0400 |
commit | 2b0ba65c513b362617556551028360184f58ecc1 (patch) | |
tree | f24db0f5ab6b3b5a541cf6fab34d75363cae886f | |
parent | 4a141022c39cefd422e66772714f54ccb3ec7733 (diff) |
Minor optimization for strlen and memcpy calls for reading blend files
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
-rw-r--r-- | source/blender/makesdna/DNA_genfile.h | 2 | ||||
-rw-r--r-- | source/blender/makesdna/intern/dna_genfile.c | 35 | ||||
-rw-r--r-- | source/blender/makesdna/intern/makesdna.c | 17 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_define.c | 2 |
4 files changed, 25 insertions, 31 deletions
diff --git a/source/blender/makesdna/DNA_genfile.h b/source/blender/makesdna/DNA_genfile.h index 96d71288c62..9a6c7144692 100644 --- a/source/blender/makesdna/DNA_genfile.h +++ b/source/blender/makesdna/DNA_genfile.h @@ -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); diff --git a/source/blender/makesdna/intern/dna_genfile.c b/source/blender/makesdna/intern/dna_genfile.c index ed7f6f5d765..eca06242da0 100644 --- a/source/blender/makesdna/intern/dna_genfile.c +++ b/source/blender/makesdna/intern/dna_genfile.c @@ -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; diff --git a/source/blender/makesdna/intern/makesdna.c b/source/blender/makesdna/intern/makesdna.c index aac79245501..39bf5ec437f 100644 --- a/source/blender/makesdna/intern/makesdna.c +++ b/source/blender/makesdna/intern/makesdna.c @@ -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); diff --git a/source/blender/makesrna/intern/rna_define.c b/source/blender/makesrna/intern/rna_define.c index fe38ce2d86a..3a5a1d30cd8 100644 --- a/source/blender/makesrna/intern/rna_define.c +++ b/source/blender/makesrna/intern/rna_define.c @@ -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++) |