grub-mkconfig linux: Fix quadratic algorithm for sorting menu items
PATCH v2 1/1 grub-mkconfig linux: Fix quadratic algorithm for sorting menu items
The current implementation of the 10_linux script implements its menu items sorting in bash with a quadratic algorithm, calling "sed", "sort", head, and grep to compare versions between individual lines, which is annoyingly slow for kernel developers who can easily end up with 50-100 kernels in /boot.
As an example, on a Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz, running:
/usr/sbin/grub-mkconfig > /dev/null
With 44 kernels in /boot, this command takes 10-15 seconds to complete. After this fix, the same command runs in 5 seconds.
With 116 kernels in /boot, this command takes 40 seconds to complete. After this fix, the same command runs in 8 seconds.
For reference, the quadratic algorithm here is:
while [ "x$list" != "x" ] ; do <--- outer loop
linux=version_find_latest $list
version_find_latest()
for i in "$@" ; do <--- inner loop
version_test_gt()
fork+exec sed
version_test_numeric()
version_sort
fork+exec sort
fork+exec head -n 1
fork+exec grep
list=echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '
tr
fgrep
tr
So all commands executed under version_test_gt() are executed O(n^2) times where n is the number of kernel images in /boot.
Here is the improved algorithm proposed:
- Prepare a list with all the relevant information for ordering by a single sort(1) execution. This is done by renaming ".old" suffixes by " 1" and by suffixing all other files with " 2", thus making sure the ".old" entries will follow the non-old entries in reverse-sorted-order.
- Call version_reverse_sort on the list (sort -r -V): A single execution of sort(1) will reverse-sort the list in O(n*log(n)) with a merge sort.
- Replace the " 1" suffixes by ".old", and remove the " 2" suffixes.
- Iterate on the reverse-sorted list to output each menu entry item.
Therefore, the algorithm proposed has O(n*log(n)) complexity compared to the prior O(n^2) complexity. Moreover, the constant time required for each list entry is much less because sorting is done within a single execution of sort(1) rather than requiring O(n^2) executions of sed(1), sort(1), head(1), and grep(1) in sub-shells.
I notice that the same quadratic sorting is done for other supported OSes, so I suspect similar gains can be obtained there, but I limit the scope of this patch to Linux because this is the platform on which I can test.
---
Changes since v1:
- Escape the dot from .old in the sed match pattern, thus ensuring it matches ".old" rather than "[any character]old".
- Use "sed" rather than "sed -e" everywhere for consistency.
- Document the new algorithm in the commit message.
--- util/grub-mkconfig_lib.in | 18 ++++++++++++++++++ util/grub.d/10_linux.in | 12 ++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-)
util/grub-mkconfig_lib.in +18 -0
@@ -218,6 +218,24 @@ version_sort () esac } +version_reverse_sort () +{ + case $version_reverse_sort_sort_has_v in + yes) + LC_ALL=C sort -r -V;; + no) + LC_ALL=C sort -r -n;; + *) + if sort -r -V </dev/null > /dev/null 2>&1; then + version_reverse_sort_sort_has_v=yes + LC_ALL=C sort -r -V + else + version_reverse_sort_sort_has_v=no + LC_ALL=C sort -r -n + fi;; + esac +} + version_test_numeric () { version_test_numeric_a="$1"
util/grub.d/10_linux.in +8 -4
@@ -195,9 +195,15 @@ title_correction_code= # yet, so it's empty. In a submenu it will be equal to '\t' (one tab). submenu_indentation="" +# Perform a reverse version sort on the entire list. +# Temporarily replace the '.old' suffix by ' 1' and append ' 2' for all +# other files to order the '.old' files after their non-old counterpart +# in reverse-sorted order. + +reverse_sorted_list=$(echo $list | tr ' ' '\n' | sed 's/$/ 2/' | sed 's/\.old 2/ 1/' | version_reverse_sort | sed 's/ 1$/.old/' | sed 's/ 2$//') + is_top_level=true -while [ "x$list" != "x" ] ; do - linux=`version_find_latest $list` +for linux in $reverse_sorted_list; do gettext_printf "Found linux image: %s\n" "$linux" >&2 basename=`basename $linux` dirname=`dirname $linux`
@@ -293,8 +299,6 @@ while [ "x$list" != "x" ] ; do linux_entry "${OS}" "${version}" recovery \ "${GRUB_CMDLINE_LINUX_RECOVERY} ${GRUB_CMDLINE_LINUX}" fi - - list=`echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '` done # If at least one kernel was found, then we need to
Signature · 1 line
2.30.2
util/grub-mkconfig_lib.in version_sort () · line 223
Quoted 3 lines · Mathieu Desnoyers
util/grub-mkconfig_lib.in +3
-0+version_reverse_sort () +{ + case $version_reverse_sort_sort_has_v in
Does this need to be its own variable, or could it just reuse version_sort_sort_has_v?
Quoted 1 line
+reverse_sorted_list=$(echo $list | tr ' ' '\n' | sed 's/$/ 2/' | sed 's/\.old 2/ 1/' |
version_reverse_sort| sed 's/ 1$/.old/' | sed 's/ 2$//')
Could the sed | sed pattern be collapsed into a single invocation using multiple -e switches?1
Be well,
--Robbie
Quoted 8 lines · ----- On May 4, 2022, at 10:43 AM, Robbie Harwood [email protected] wrote:
util/grub-mkconfig_lib.in version_sort () · line 223
Quoted 3 lines · Mathieu Desnoyers
util/grub-mkconfig_lib.in +3
-0+version_reverse_sort () +{ + case $version_reverse_sort_sort_has_v inDoes this need to be its own variable, or could it just reuse
version_sort_sort_has_v?
We could do that, assuming that both sort -r -V and sort -V always work (or don't) in the same way.
Then I would change the test in version_reverse_sort to:
if sort -V </dev/null > /dev/null 2>&1; then
There is no point in calling this check with "-r" if it sets version_sort_sort_has_v.
Quoted 6 lines
Quoted 3 lines
+reverse_sorted_list=$(echo $list | tr ' ' '\n' | sed 's/$/ 2/' | sed 's/\.old 2/ 1/' |
version_reverse_sort| sed 's/ 1$/.old/' | sed 's/ 2$//')Could the
sed | sedpattern be collapsed into a single invocation using multiple -e switches?1
I think we can straightforwardly turn "| sed 's/ 1$/.old/' | sed 's/ 2$//'" into "| sed -e 's/ 1$/.old/' -e 's/ 2$//'".
How to match that all lines ending with ".old" need to be replaced with a " 1" suffix, and all _other_ lines need to be suffixed with " 2" in a single sed invocation is unclear to me. Any idea ?
Thanks,
Mathieu
Quoted 3 lines
Be well,
--Robbie
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Quoted 29 lines · ----- On May 4, 2022, at 11:24 AM, Mathieu Desnoyers…
Quoted 8 lines · ----- On May 4, 2022, at 10:43 AM, Robbie Harwood [email protected] wrote:
util/grub-mkconfig_lib.in version_sort () · line 223
Quoted 3 lines · Mathieu Desnoyers
util/grub-mkconfig_lib.in +3
-0+version_reverse_sort () +{ + case $version_reverse_sort_sort_has_v inDoes this need to be its own variable, or could it just reuse
version_sort_sort_has_v?We could do that, assuming that both sort -r -V and sort -V always work (or don't)
in the same way.Then I would change the test in
version_reverse_sortto:if sort -V <
/dev/null>/dev/null2>&1; thenThere is no point in calling this check with "-r" if it sets
version_sort_sort_has_v.Quoted 6 lines
Quoted 3 lines
+reverse_sorted_list=$(echo $list | tr ' ' '\n' | sed 's/$/ 2/' | sed 's/\.old 2/ 1/' |
version_reverse_sort| sed 's/ 1$/.old/' | sed 's/ 2$//')Could the
sed | sedpattern be collapsed into a single invocation using multiple -e switches?1
Quoted 6 lines
I think we can straightforwardly turn "| sed 's/ 1$/.old/' | sed 's/ 2$//'" into "| sed -e 's/ 1$/.old/' -e 's/ 2$//'".
How to match that all lines ending with ".old" need to be replaced with a " 1" suffix, and all _other_ lines need to be suffixed with " 2" in a single sed invocation is unclear to me. Any idea ?
Actually with sed -e 's/$/ 2/' -e 's/\.old 2$/ 1/' the second -e applies on the result of the first, so I can just do that.
Thanks,
Mathieu
Quoted 13 lines
Thanks,
Mathieu
Quoted 3 lines
Be well,
--Robbie--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Quoted 35 lines · ----- On May 5, 2022, at 9:53 AM, Mathieu Desnoyers…
----- On May 4, 2022, at 11:24 AM, Mathieu Desnoyers
Quoted 32 lines · [email protected] wrote:
Quoted 8 lines · ----- On May 4, 2022, at 10:43 AM, Robbie Harwood [email protected] wrote:
util/grub-mkconfig_lib.in version_sort () · line 223
Quoted 3 lines · Mathieu Desnoyers
util/grub-mkconfig_lib.in +3
-0+version_reverse_sort () +{ + case $version_reverse_sort_sort_has_v inDoes this need to be its own variable, or could it just reuse
version_sort_sort_has_v?We could do that, assuming that both sort -r -V and sort -V always work (or don't)
in the same way.Then I would change the test in
version_reverse_sortto:if sort -V <
/dev/null>/dev/null2>&1; thenThere is no point in calling this check with "-r" if it sets
version_sort_sort_has_v.Quoted 6 lines
Quoted 3 lines
+reverse_sorted_list=$(echo $list | tr ' ' '\n' | sed 's/$/ 2/' | sed 's/\.old 2/ 1/' |
version_reverse_sort| sed 's/ 1$/.old/' | sed 's/ 2$//')Could the
sed | sedpattern be collapsed into a single invocation using multiple -e switches?1I think we can straightforwardly turn "| sed 's/ 1$/.old/' | sed 's/ 2$//'" into "| sed -e 's/ 1$/.old/' -e 's/ 2$//'".
Quoted 6 lines
Quoted 3 lines
How to match that all lines ending with ".old" need to be replaced with a " 1" suffix, and all _other_ lines need to be suffixed with " 2" in a single sed invocation is unclear to me. Any idea ?
Actually with sed -e 's/$/ 2/' -e 's/\.old 2$/ 1/' the second -e applies on the result of the first, so I can just do that.
And even easier to comprehend with a negated regex as address for the second expression:
sed -e 's/\.old$/ 1/' -e '/ 1$/! s/$/ 2/'
Thanks,
Mathieu
Quoted 23 lines
Thanks,
Mathieu
Quoted 6 lines
Thanks,
Mathieu
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
Be well,
--Robbie--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com