1
0
mirror of https://github.com/mpv-player/mpv synced 2025-02-16 20:27:23 +00:00

TOOLS/lua/autoload: avoid unnecessary playlist manipulation, performance

We used to sort the playlist with playlist-move after every loadfile.
Instead, append all files in order and call playlist-move once to move
the only entry we don't control the position of.
Don't fetch every playlist item separately, reuse native property.
We used to pick up on new files added to the directory, but only when
playing an entry at the edge of the playlist due to an early return.
New files are now added to the playlist on every file change.
This still works as expected and doesn't load duplicate files on
shuffled playlists or playlists with files manually added after autoload

33% faster on average for my test directory with 1371 files.
This commit is contained in:
Eva 2023-06-29 18:58:46 +02:00 committed by Dudemanguy
parent c8a54a8c23
commit 3ba446d0b0

View File

@ -68,12 +68,11 @@ if o.videos then EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_VIDEO) end
if o.audio then EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_AUDIO) end if o.audio then EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_AUDIO) end
if o.images then EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_IMAGES) end if o.images then EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_IMAGES) end
function add_files_at(index, files) function add_files(files)
index = index - 1
local oldcount = mp.get_property_number("playlist-count", 1) local oldcount = mp.get_property_number("playlist-count", 1)
for i = 1, #files do for i = 1, #files do
mp.commandv("loadfile", files[i], "append") mp.commandv("loadfile", files[i][1], "append")
mp.commandv("playlist-move", oldcount + i - 1, index + i - 1) mp.commandv("playlist-move", oldcount + i - 1, files[i][2])
end end
end end
@ -116,11 +115,10 @@ end
local autoloaded = nil local autoloaded = nil
function get_playlist_filenames() function get_playlist_filenames(playlist)
local filenames = {} local filenames = {}
for n = 0, pl_count - 1, 1 do for i = 1, #playlist do
local filename = mp.get_property('playlist/'..n..'/filename') local _, file = utils.split_path(playlist[i].filename)
local _, file = utils.split_path(filename)
filenames[file] = true filenames[file] = true
end end
return filenames return filenames
@ -190,32 +188,43 @@ function find_and_add_entries()
msg.trace("current file position in files: "..current) msg.trace("current file position in files: "..current)
local append = {[-1] = {}, [1] = {}} local append = {[-1] = {}, [1] = {}}
local filenames = get_playlist_filenames() local filenames = get_playlist_filenames(pl)
for direction = -1, 1, 2 do -- 2 iterations, with direction = -1 and +1 for direction = -1, 1, 2 do -- 2 iterations, with direction = -1 and +1
for i = 1, MAXENTRIES do for i = 1, MAXENTRIES do
local file = files[current + i * direction] local pos = current + i * direction
local file = files[pos]
if file == nil or file[1] == "." then if file == nil or file[1] == "." then
break break
end end
local filepath = dir .. file local filepath = dir .. file
-- skip files already in playlist -- skip files already in playlist
if filenames[file] then break end if not filenames[file] then
if direction == -1 then
if direction == -1 then
if pl_current == 1 then -- never add additional entries in the middle
msg.info("Prepending " .. file) msg.info("Prepending " .. file)
table.insert(append[-1], 1, filepath) table.insert(append[-1], 1, {filepath, pos - 1})
else
msg.info("Adding " .. file)
if pl_count > 1 then
table.insert(append[1], {filepath, pos - 1})
else
mp.commandv("loadfile", filepath, "append")
end
end end
else
msg.info("Adding " .. file)
table.insert(append[1], filepath)
end end
end end
if pl_count == 1 and direction == -1 and #append[-1] > 0 then
for i = 1, #append[-1] do
mp.commandv("loadfile", append[-1][i][1], "append")
end
mp.commandv("playlist-move", 0, current)
end
end end
add_files_at(pl_current + 1, append[1]) if pl_count > 1 then
add_files_at(pl_current, append[-1]) add_files(append[1])
add_files(append[-1])
end
end end
mp.register_event("start-file", find_and_add_entries) mp.register_event("start-file", find_and_add_entries)