From de9f375a23645091c25a850cc67eb5521e903731 Mon Sep 17 00:00:00 2001 From: Christoph Heinrich Date: Tue, 25 Oct 2022 19:02:20 +0200 Subject: [PATCH] TOOLS/lua/autoload: optimize performance of natural sorting Formatting the string on each comparison is wasteful. Formatting strings beforehand and then comparing the already formatted ones leads to a huge performance gain. --- TOOLS/lua/autoload.lua | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/TOOLS/lua/autoload.lua b/TOOLS/lua/autoload.lua index f43272eee6..c2cac862e4 100644 --- a/TOOLS/lua/autoload.lua +++ b/TOOLS/lua/autoload.lua @@ -97,16 +97,21 @@ end -- alphanum sorting for humans in Lua -- http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua -function alphanumsort(o) +function alphanumsort(filenames) local function padnum(d) local dec, n = string.match(d, "(%.?)0*(.+)") return #dec > 0 and ("%.12f"):format(d) or ("%s%03d%s"):format(dec, #n, n) end - table.sort(o, function(a,b) - return tostring(a):lower():gsub("%.?%d+",padnum)..("%3d"):format(#b) - < tostring(b):lower():gsub("%.?%d+",padnum)..("%3d"):format(#a) + + local tuples = {} + for i, f in ipairs(filenames) do + tuples[i] = {f:lower():gsub("%.?%d+", padnum), f} + end + table.sort(tuples, function(a, b) + return a[1] == b[1] and #b[2] < #a[2] or a[1] < b[1] end) - return o + for i, tuple in ipairs(tuples) do filenames[i] = tuple[2] end + return filenames end local autoloaded = nil