tree.c optimize bookmark_play()

optimize the index search loop using mod()
instead of checking index and then scanning we can start at index
and roll over saving a compare in the process

Change-Id: Ie54fbf1ca0131db8914985b654248eeb8d725a82
This commit is contained in:
William Wilgus 2025-02-12 02:29:30 -05:00
parent 801260dd79
commit 922a2a4f3d

View file

@ -1194,13 +1194,12 @@ bool bookmark_play(char *resume_file, int index, unsigned long elapsed,
char* suffix = strrchr(resume_file, '.'); char* suffix = strrchr(resume_file, '.');
bool started = false; bool started = false;
if (suffix != NULL && if (suffix != NULL && !strncasecmp(suffix, ".m3u", sizeof(".m3u") - 1)) /* gets m3u8 too */
(!strcasecmp(suffix, ".m3u") || !strcasecmp(suffix, ".m3u8")))
{ {
/* Playlist playback */ /* Playlist playback */
char* slash; char* slash;
/* check that the file exists */ /* check that the file exists */
if(!file_exists(resume_file)) if (!file_exists(resume_file))
return false; return false;
slash = strrchr(resume_file,'/'); slash = strrchr(resume_file,'/');
@ -1217,8 +1216,6 @@ bool bookmark_play(char *resume_file, int index, unsigned long elapsed,
{ {
if (global_settings.playlist_shuffle) if (global_settings.playlist_shuffle)
playlist_shuffle(seed, -1); playlist_shuffle(seed, -1);
playlist_start(index, elapsed, offset);
started = true; started = true;
} }
*slash='/'; *slash='/';
@ -1238,45 +1235,37 @@ bool bookmark_play(char *resume_file, int index, unsigned long elapsed,
/* Check if the file is at the same spot in the directory, /* Check if the file is at the same spot in the directory,
else search for it */ else search for it */
peek_filename = playlist_peek(index, filename_buf, int amt = playlist_amount();
for ( i=0; i < amt; i++ )
{
int modidx = (i + index) % amt;
peek_filename = playlist_peek(modidx, filename_buf,
sizeof(filename_buf)); sizeof(filename_buf));
if (peek_filename == NULL) if (peek_filename == NULL)
{ {
if (index == 0) /* searched every entry didn't find a match */
return false;
/* playlist has shrunk, search from the top */ /* playlist has shrunk, search from the top */
i = 0;
amt = index;
index = 0; index = 0;
peek_filename = playlist_peek(index, filename_buf,
sizeof(filename_buf));
if (peek_filename == NULL)
return false;
} }
else if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
if (strcmp(strrchr(peek_filename, '/') + 1, filename))
{ {
for ( i=0; i < playlist_amount(); i++ ) started = true;
{ index = modidx;
peek_filename = playlist_peek(i, filename_buf,
sizeof(filename_buf));
if (peek_filename == NULL)
return false;
if (!strcmp(strrchr(peek_filename, '/') + 1, filename))
break; break;
} }
if (i < playlist_amount())
index = i;
else
return false;
} }
playlist_start(index, elapsed, offset);
started = true;
} }
} }
if (started) if (started)
{
playlist_start(index, elapsed, offset);
start_wps = true; start_wps = true;
}
return started; return started;
} }