diff mbox series

[v5,2/5] grub-mkconfig linux_xen: Fix quadratic algorithm for sorting menu items

Message ID 20220609185024.447922-3-mathieu.desnoyers@efficios.com (mailing list archive)
State New, archived
Headers show
Series None | expand

Commit Message

Mathieu Desnoyers June 9, 2022, 6:50 p.m. UTC
The current implementation of the 20_linux_xen 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 their boot partition.

This fix is ported from the 10_linux script, which has a similar
quadratic code pattern.

[ Note: this is untested. I would be grateful if anyone with a Xen
  environment could test it before it is merged. ]

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: xen-devel@lists.xenproject.org
---
Changes since v4:
- Combine sed -e '...' -e '...' into sed -e '...; ...'
---
 util/grub.d/20_linux_xen.in | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

Comments

Jason Andryuk June 10, 2022, 8 p.m. UTC | #1
On Thu, Jun 9, 2022 at 2:50 PM Mathieu Desnoyers
<mathieu.desnoyers@efficios.com> wrote:
>
> The current implementation of the 20_linux_xen 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 their boot partition.
>
> This fix is ported from the 10_linux script, which has a similar
> quadratic code pattern.
>
> [ Note: this is untested. I would be grateful if anyone with a Xen
>   environment could test it before it is merged. ]

Hi, Mathieu,

I tested by manually applying patch 2/5 on top of Fedora 36's
installed /etc/grub.d/20_linux_xen, and manually applying patch 1/5 to
/usr/share/grub/grub-mkconfig_lib.  It seems to generate grub.cfg
menuentry-ies in the correct order.

Note for patch 1/5, it's best practice to use "$@" with the double
quotes to prevent word splitting of arguments.  Doesn't really matter
for that function at this time though.

Regards,
Jason
Mathieu Desnoyers June 13, 2022, 2 p.m. UTC | #2
----- On Jun 10, 2022, at 4:00 PM, Jason Andryuk jandryuk@gmail.com wrote:

> On Thu, Jun 9, 2022 at 2:50 PM Mathieu Desnoyers
> <mathieu.desnoyers@efficios.com> wrote:
>>
>> The current implementation of the 20_linux_xen 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 their boot partition.
>>
>> This fix is ported from the 10_linux script, which has a similar
>> quadratic code pattern.
>>
>> [ Note: this is untested. I would be grateful if anyone with a Xen
>>   environment could test it before it is merged. ]
> 
> Hi, Mathieu,
> 
> I tested by manually applying patch 2/5 on top of Fedora 36's
> installed /etc/grub.d/20_linux_xen, and manually applying patch 1/5 to
> /usr/share/grub/grub-mkconfig_lib.  It seems to generate grub.cfg
> menuentry-ies in the correct order.

Noted. Added your Tested-by to patch 2/5. Thanks!

> 
> Note for patch 1/5, it's best practice to use "$@" with the double
> quotes to prevent word splitting of arguments.  Doesn't really matter
> for that function at this time though.

I'll update patch 1/5 with this change. It's best practice indeed. I notice
that there are quite a few other places in grub-mkconfig_lib.in that do
follow this best practice though.

Thanks,

Mathieu

> 
> Regards,
> Jason
diff mbox series

Patch

diff --git a/util/grub.d/20_linux_xen.in b/util/grub.d/20_linux_xen.in
index 51a983926..4382303c1 100644
--- a/util/grub.d/20_linux_xen.in
+++ b/util/grub.d/20_linux_xen.in
@@ -237,11 +237,17 @@  esac
 # 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 xen_list and linux_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_xen_list=$(echo ${xen_list} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | version_sort -r | sed -e 's/ 1$/.old/; s/ 2$//')
+reverse_sorted_linux_list=$(echo ${linux_list} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | version_sort -r | sed -e 's/ 1$/.old/; s/ 2$//')
+
 is_top_level=true
 
-while [ "x${xen_list}" != "x" ] ; do
-    list="${linux_list}"
-    current_xen=`version_find_latest $xen_list`
+for current_xen in ${reverse_sorted_xen_list}; do
     xen_basename=`basename ${current_xen}`
     xen_dirname=`dirname ${current_xen}`
     rel_xen_dirname=`make_system_path_relative_to_its_root $xen_dirname`
@@ -273,8 +279,7 @@  while [ "x${xen_list}" != "x" ] ; do
        fi
     done
 
-    while [ "x$list" != "x" ] ; do
-	linux=`version_find_latest $list`
+    for linux in ${reverse_sorted_linux_list}; do
 	gettext_printf "Found linux image: %s\n" "$linux" >&2
 	basename=`basename $linux`
 	dirname=`dirname $linux`
@@ -351,13 +356,10 @@  while [ "x${xen_list}" != "x" ] ; do
 	    linux_entry "${OS}" "${version}" "${xen_version}" recovery \
 		"${GRUB_CMDLINE_LINUX_RECOVERY} ${GRUB_CMDLINE_LINUX}" "${GRUB_CMDLINE_XEN}"
 	fi
-
-	list=`echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '`
     done
     if [ x"$is_top_level" != xtrue ]; then
 	echo '	}'
     fi
-    xen_list=`echo $xen_list | tr ' ' '\n' | fgrep -vx "$current_xen" | tr '\n' ' '`
 done
 
 # If at least one kernel was found, then we need to