diff options
author | Christoph Heinrich <christoph.heinrich@student.tugraz.at> | 2023-09-27 21:44:52 +0200 |
---|---|---|
committer | Dudemanguy <random342@airmail.cc> | 2023-11-07 01:36:59 +0000 |
commit | ff131d7a20c50a678f6af820adf8ab2e3493849d (patch) | |
tree | 642b4727182301bd4daa03e168be5f7f06187680 /player/lua | |
parent | bca0b20c098dbaca3d40d366698aa4c2cabb972b (diff) | |
download | mpv-ff131d7a20c50a678f6af820adf8ab2e3493849d.tar.bz2 mpv-ff131d7a20c50a678f6af820adf8ab2e3493849d.tar.xz |
console: optimize table generation
Showing all properties was terribly slow.
Instead of starting at one row and increasing the row count until it
fits, the column count can be increased until it doesn't fit anymore.
That alone already reduces the required iterations, but from the column
count an upper and lower bound for the row count can be calculated.
For large tables this dramatically reduces the amount of iterations.
Diffstat (limited to 'player/lua')
-rw-r--r-- | player/lua/console.lua | 52 |
1 files changed, 32 insertions, 20 deletions
diff --git a/player/lua/console.lua b/player/lua/console.lua index 2e6806b781..e5136c8928 100644 --- a/player/lua/console.lua +++ b/player/lua/console.lua @@ -166,29 +166,41 @@ function format_table(list, width_max, rows_max) end -- use as many columns as possible - for rows = 1, list_size do - local columns = math.ceil(list_size / rows) - column_widths = {} - width_total = 0 - - -- find out width of each column - for column = 1, columns do - local width = 0 - for row = 1, rows do - local i = row + (column - 1) * rows - if i > #list then break end - local item_width = list_widths[i] - if width < item_width then - width = item_width + for columns = 2, list_size do + local old_row_count = row_count + local rows_lower_bound = math.ceil(list_size / columns) + local rows_upper_bound = math.min(list_size, math.ceil(list_size / (columns - 1) - 1)) + for rows = rows_upper_bound, rows_lower_bound, -1 do + cw = {} + width_total = 0 + + -- find out width of each column + for column = 1, columns do + local width = 0 + for row = 1, rows do + local i = row + (column - 1) * rows + local item_width = list_widths[i] + if not item_width then break end + if width < item_width then + width = item_width + end + end + cw[column] = width + width_total = width_total + width + if width_total + (columns - 1) * spaces_min > width_max then + break end end - column_widths[column] = width - width_total = width_total + width - end - if width_total + columns * spaces_min <= width_max then - row_count = rows - column_count = columns + if width_total + (columns - 1) * spaces_min <= width_max then + row_count = rows + column_count = columns + column_widths = cw + else + break + end + end + if row_count == old_row_count then break end end |