forked from len0rd/rockbox
Skin engine, optimize hot paths
callgrind identified check_viewport and scan_int as pretty hot code paths on startup and playback check_viewport uses strlen to check the string has at least 4 characters we can just check if str[3] != '\0' without walking potentially much further and no function call.. scan_int was building a buffer for atoi to parse when we can just do it in the loop directly Change-Id: Ie028980333cbed4c066d8ea547a89cf4fad76808
This commit is contained in:
parent
4bde992ca3
commit
b94b0d3bf4
1 changed files with 23 additions and 31 deletions
|
@ -166,53 +166,42 @@ char* scan_string(const char** document)
|
||||||
int scan_int(const char** document)
|
int scan_int(const char** document)
|
||||||
{
|
{
|
||||||
|
|
||||||
const char *cursor = *document, *end;
|
const char *cursor = *document;
|
||||||
int length = 0;
|
int retval = 0;
|
||||||
char buffer[16];
|
int sign = 1;
|
||||||
int retval;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
while(isdigit(*cursor) || *cursor == COMMENTSYM || *cursor == '-')
|
while(true)
|
||||||
{
|
{
|
||||||
if(*cursor == COMMENTSYM)
|
if(*cursor == COMMENTSYM)
|
||||||
{
|
{
|
||||||
skip_comment(&cursor);
|
skip_comment(&cursor);
|
||||||
|
if (retval > 0) /* || sign < 0 already read a number */
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
else if (*cursor == '-')
|
||||||
length++;
|
{
|
||||||
|
if (retval != 0) /* only allow negative prior to numbers */
|
||||||
|
break;
|
||||||
|
sign = -1;
|
||||||
|
}
|
||||||
|
else if (isdigit(*cursor)) /* is digit*/
|
||||||
|
{
|
||||||
|
retval = (retval * 10) + (*cursor - '0');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
break;
|
||||||
cursor++;
|
cursor++;
|
||||||
}
|
}
|
||||||
if (length > 15)
|
*document = cursor;
|
||||||
length = 15;
|
|
||||||
end = cursor;
|
|
||||||
/* Copying to the buffer while avoiding comments */
|
|
||||||
cursor = *document;
|
|
||||||
buffer[length] = '\0';
|
|
||||||
for(i = 0; i < length; i++)
|
|
||||||
{
|
|
||||||
if(*cursor == COMMENTSYM)
|
|
||||||
{
|
|
||||||
skip_comment(&cursor);
|
|
||||||
i--;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer[i] = *cursor;
|
return sign * retval;
|
||||||
cursor++;
|
|
||||||
|
|
||||||
}
|
|
||||||
retval = atoi(buffer);
|
|
||||||
|
|
||||||
*document = end;
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int check_viewport(const char* document)
|
int check_viewport(const char* document)
|
||||||
{
|
{
|
||||||
if(strlen(document) < 3)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if(document[0] != TAGSYM)
|
if(document[0] != TAGSYM)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -224,5 +213,8 @@ int check_viewport(const char* document)
|
||||||
&& document[2] != 'i')
|
&& document[2] != 'i')
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
if (document[3] == '\0')
|
||||||
|
return 0;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue