Rockbox open source high quality audio player as a Music Player Daemon
mpris rockbox mpd libadwaita audio rust zig deno
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

authored by

William Wilgus and committed by
William Wilgus
b94b0d3b 4bde992c

+23 -31
+23 -31
lib/skin_parser/skin_scan.c
··· 166 166 int scan_int(const char** document) 167 167 { 168 168 169 - const char *cursor = *document, *end; 170 - int length = 0; 171 - char buffer[16]; 172 - int retval; 173 - int i; 169 + const char *cursor = *document; 170 + int retval = 0; 171 + int sign = 1; 174 172 175 - while(isdigit(*cursor) || *cursor == COMMENTSYM || *cursor == '-') 173 + while(true) 176 174 { 177 175 if(*cursor == COMMENTSYM) 178 176 { 179 177 skip_comment(&cursor); 178 + if (retval > 0) /* || sign < 0 already read a number */ 179 + { 180 + break; 181 + } 180 182 continue; 181 183 } 182 - 183 - length++; 184 - cursor++; 185 - } 186 - if (length > 15) 187 - length = 15; 188 - end = cursor; 189 - /* Copying to the buffer while avoiding comments */ 190 - cursor = *document; 191 - buffer[length] = '\0'; 192 - for(i = 0; i < length; i++) 193 - { 194 - if(*cursor == COMMENTSYM) 184 + else if (*cursor == '-') 195 185 { 196 - skip_comment(&cursor); 197 - i--; 198 - continue; 186 + if (retval != 0) /* only allow negative prior to numbers */ 187 + break; 188 + sign = -1; 199 189 } 200 - 201 - buffer[i] = *cursor; 190 + else if (isdigit(*cursor)) /* is digit*/ 191 + { 192 + retval = (retval * 10) + (*cursor - '0'); 193 + } 194 + else 195 + break; 202 196 cursor++; 203 - 204 197 } 205 - retval = atoi(buffer); 198 + *document = cursor; 206 199 207 - *document = end; 208 - return retval; 200 + return sign * retval; 209 201 } 210 202 211 203 int check_viewport(const char* document) 212 204 { 213 - if(strlen(document) < 3) 214 - return 0; 215 - 216 205 if(document[0] != TAGSYM) 217 206 return 0; 218 207 ··· 222 211 if(document[2] != ARGLISTOPENSYM 223 212 && document[2] != 'l' 224 213 && document[2] != 'i') 214 + return 0; 215 + 216 + if (document[3] == '\0') 225 217 return 0; 226 218 227 219 return 1;