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:
William Wilgus 2025-02-07 12:54:02 -05:00 committed by William Wilgus
parent 4bde992ca3
commit b94b0d3bf4

View file

@ -166,53 +166,42 @@ char* scan_string(const char** document)
int scan_int(const char** document)
{
const char *cursor = *document, *end;
int length = 0;
char buffer[16];
int retval;
int i;
const char *cursor = *document;
int retval = 0;
int sign = 1;
while(isdigit(*cursor) || *cursor == COMMENTSYM || *cursor == '-')
while(true)
{
if(*cursor == COMMENTSYM)
{
skip_comment(&cursor);
if (retval > 0) /* || sign < 0 already read a number */
{
break;
}
continue;
}
length++;
cursor++;
}
if (length > 15)
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)
else if (*cursor == '-')
{
skip_comment(&cursor);
i--;
continue;
if (retval != 0) /* only allow negative prior to numbers */
break;
sign = -1;
}
buffer[i] = *cursor;
else if (isdigit(*cursor)) /* is digit*/
{
retval = (retval * 10) + (*cursor - '0');
}
else
break;
cursor++;
}
retval = atoi(buffer);
*document = cursor;
*document = end;
return retval;
return sign * retval;
}
int check_viewport(const char* document)
{
if(strlen(document) < 3)
return 0;
if(document[0] != TAGSYM)
return 0;
@ -224,5 +213,8 @@ int check_viewport(const char* document)
&& document[2] != 'i')
return 0;
if (document[3] == '\0')
return 0;
return 1;
}