Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1nkX4f-0003WA-EK for mharc-grub-devel@gnu.org; Fri, 29 Apr 2022 16:18:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45962) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nkX4d-0003W0-Ef for grub-devel@gnu.org; Fri, 29 Apr 2022 16:18:43 -0400 Received: from mail.efficios.com ([167.114.26.124]:36514) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nkX4b-000801-Bj for grub-devel@gnu.org; Fri, 29 Apr 2022 16:18:42 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id 002BB297541 for ; Fri, 29 Apr 2022 16:18:13 -0400 (EDT) Received: from mail.efficios.com ([127.0.0.1]) by localhost (mail03.efficios.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id D6no-j92qQS4; Fri, 29 Apr 2022 16:18:13 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id 7E83329753F; Fri, 29 Apr 2022 16:18:13 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 7E83329753F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1651263493; bh=E956dB/BS8s3onPuXnPhlvfVT1jOff9L890Rqy88whg=; h=From:To:Date:Message-Id:MIME-Version; b=u5Tb0Dh+Fjd0twPtKNZ6hTOe3nodQ5za2IA0U61sTdhDJqXRWH9OS5eMlQW5+RMB8 qT3E3XoW9KVzfJeKitFgxefOqVln02eSYq2tm5PkLRNvJLhnFRWSUMRJ6qGazdAxRK lnewZo0elk2SPvl1yHXSZMQtKzOWon1EFHAXKNepvXlNpONx8TbaSsE2bMClmfJQD1 kvqqo425jz2xQ2zzehcHMNsA5SF/L1Ae/LTNTeQs+14UqyMouDp7vKq68Kbmr2+jti CSr71Zdql6+4cekiIAfUFoHjgAx+kGE89IRrA7v5INE43xcp4YvCURLIArY9TynDjX 6UhswzU5nEzeQ== X-Virus-Scanned: amavisd-new at efficios.com Received: from mail.efficios.com ([127.0.0.1]) by localhost (mail03.efficios.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 508VKMiB9w3h; Fri, 29 Apr 2022 16:18:13 -0400 (EDT) Received: from thinkos.internal.efficios.com (192-222-180-24.qc.cable.ebox.net [192.222.180.24]) by mail.efficios.com (Postfix) with ESMTPSA id 597DF29753E; Fri, 29 Apr 2022 16:18:13 -0400 (EDT) From: Mathieu Desnoyers To: grub-devel@gnu.org Cc: Mathieu Desnoyers Subject: [RFC PATCH] grub-mkconfig linux: Fix quadratic algorithm for sorting menu items Date: Fri, 29 Apr 2022 16:17:55 -0400 Message-Id: <20220429201755.621937-1-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass client-ip=167.114.26.124; envelope-from=mathieu.desnoyers@efficios.com; helo=mail.efficios.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 29 Apr 2022 20:18:43 -0000 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 really annoying 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" !=3D "x" ] ; do <--- outer loop linux=3D`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=3D`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. 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. Signed-off-by: Mathieu Desnoyers --- util/grub-mkconfig_lib.in | 18 ++++++++++++++++++ util/grub.d/10_linux.in | 12 ++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/util/grub-mkconfig_lib.in b/util/grub-mkconfig_lib.in index 301d1ac22..a50bbb478 100644 --- a/util/grub-mkconfig_lib.in +++ b/util/grub-mkconfig_lib.in @@ -218,6 +218,24 @@ version_sort () esac } =20 +version_reverse_sort () +{ + case $version_reverse_sort_sort_has_v in + yes) + LC_ALL=3DC sort -r -V;; + no) + LC_ALL=3DC sort -r -n;; + *) + if sort -r -V /dev/null 2>&1; then + version_reverse_sort_sort_has_v=3Dyes + LC_ALL=3DC sort -r -V + else + version_reverse_sort_sort_has_v=3Dno + LC_ALL=3DC sort -r -n + fi;; + esac +} + version_test_numeric () { version_test_numeric_a=3D"$1" diff --git a/util/grub.d/10_linux.in b/util/grub.d/10_linux.in index ca068038e..23d4bb741 100644 --- a/util/grub.d/10_linux.in +++ b/util/grub.d/10_linux.in @@ -195,9 +195,15 @@ title_correction_code=3D # yet, so it's empty. In a submenu it will be equal to '\t' (one tab). submenu_indentation=3D"" =20 +# 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=3D$(echo $list | tr ' ' '\n' | sed -e 's/$/ 2/' | se= d -e 's/.old 2/ 1/' | version_reverse_sort | sed 's/ 1$/.old/' | sed 's/ = 2$//') + is_top_level=3Dtrue -while [ "x$list" !=3D "x" ] ; do - linux=3D`version_find_latest $list` +for linux in $reverse_sorted_list; do gettext_printf "Found linux image: %s\n" "$linux" >&2 basename=3D`basename $linux` dirname=3D`dirname $linux` @@ -293,8 +299,6 @@ while [ "x$list" !=3D "x" ] ; do linux_entry "${OS}" "${version}" recovery \ "${GRUB_CMDLINE_LINUX_RECOVERY} ${GRUB_CMDLINE_LINUX}" fi - - list=3D`echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '` done =20 # If at least one kernel was found, then we need to --=20 2.30.2