]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sparse-checkout: optimize string_list construction
authorAmisha Chhajed <amishhhaaaa@gmail.com>
Fri, 16 Jan 2026 16:50:03 +0000 (22:20 +0530)
committerJunio C Hamano <gitster@pobox.com>
Fri, 16 Jan 2026 19:02:55 +0000 (11:02 -0800)
Improve O(n^2) complexity to O(n log n) while building a sorted
'string_list' by constructing it unsorted then sorting it
followed by removing duplicates.

Signed-off-by: Amisha Chhajed <amishhhaaaa@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/sparse-checkout.c

index 15d51e60a865331c63be35523507a315646603aa..7dfb276bf01431b9430693b7b005a49671f20eb7 100644 (file)
@@ -91,10 +91,11 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix,
 
                hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
                        /* pe->pattern starts with "/", skip it */
-                       string_list_insert(&sl, pe->pattern + 1);
+                       string_list_append(&sl, pe->pattern + 1);
                }
 
                string_list_sort(&sl);
+               string_list_remove_duplicates(&sl, 0);
 
                for (i = 0; i < sl.nr; i++) {
                        quote_c_style(sl.items[i].string, NULL, stdout, 0);
@@ -289,7 +290,7 @@ static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
                if (!hashmap_contains_parent(&pl->recursive_hashmap,
                                             pe->pattern,
                                             &parent_pattern))
-                       string_list_insert(&sl, pe->pattern);
+                       string_list_append(&sl, pe->pattern);
        }
 
        string_list_sort(&sl);
@@ -311,7 +312,7 @@ static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
                if (!hashmap_contains_parent(&pl->recursive_hashmap,
                                             pe->pattern,
                                             &parent_pattern))
-                       string_list_insert(&sl, pe->pattern);
+                       string_list_append(&sl, pe->pattern);
        }
 
        strbuf_release(&parent_pattern);