]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: Switch to insertion sort
authorJan Janssen <medhefgo@web.de>
Wed, 5 Jan 2022 09:33:00 +0000 (10:33 +0100)
committerJan Janssen <medhefgo@web.de>
Thu, 6 Jan 2022 13:16:34 +0000 (14:16 +0100)
We can do a little better than bubble sort without ramping up the
code complexity.

src/boot/efi/boot.c
src/boot/efi/util.c

index fac31abc44bbf7f52e6509ded5a8718f5de4b755..b8779c8b28a2d6dd5a6bdc741ff3361373c71fa9 100644 (file)
@@ -1644,12 +1644,6 @@ static INTN config_entry_compare(const ConfigEntry *a, const ConfigEntry *b) {
         return 0;
 }
 
-static void config_sort_entries(Config *config) {
-        assert(config);
-
-        sort_pointer_array((void**) config->entries, config->entry_count, (compare_pointer_func_t) config_entry_compare);
-}
-
 static UINTN config_entry_find(Config *config, const CHAR16 *needle) {
         assert(config);
 
@@ -2345,7 +2339,7 @@ static void config_load_all_entries(
         config_load_xbootldr(config, loaded_image->DeviceHandle);
 
         /* sort entries after version number */
-        config_sort_entries(config);
+        sort_pointer_array((void **) config->entries, config->entry_count, (compare_pointer_func_t) config_entry_compare);
 
         /* if we find some well-known loaders, add them to the end of the list */
         config_entry_add_osx(config);
index ab47e108989feb7efcc2f54ebd3d1024295a32f4..ab21d774a25c5dbfbd1f0fbd754b7f2a81c0299f 100644 (file)
@@ -529,21 +529,17 @@ void sort_pointer_array(
                 return;
 
         for (UINTN i = 1; i < n_members; i++) {
-                BOOLEAN more = FALSE;
+                UINTN k;
+                void *entry = array[i];
 
-                for (UINTN k = 0; k < n_members - i; k++) {
-                        void *entry;
+                for (k = i; k > 0; k--) {
+                        if (compare(array[k - 1], entry) <= 0)
+                                break;
 
-                        if (compare(array[k], array[k+1]) <= 0)
-                                continue;
-
-                        entry = array[k];
-                        array[k] = array[k+1];
-                        array[k+1] = entry;
-                        more = TRUE;
+                        array[k] = array[k - 1];
                 }
-                if (!more)
-                        break;
+
+                array[k] = entry;
         }
 }