Skip to content

Commit 5ebd947

Browse files
rscharfegitster
authored andcommitted
string-list: use QSORT_S in string_list_sort()
Pass the comparison function to cmp_items() via the context parameter of qsort_s() instead of using a global variable. That allows calling string_list_sort() from multiple parallel threads. Our qsort_s() in compat/ is slightly slower than qsort(1) from glibc 2.24 for sorting lots of lines: Test HEAD^ HEAD --------------------------------------------------------------------- 0071.2: sort(1) 0.10(0.22+0.01) 0.09(0.21+0.00) -10.0% 0071.3: string_list_sort() 0.16(0.15+0.01) 0.17(0.15+0.00) +6.3% GNU sort(1) version 8.26 is significantly faster because it uses multiple parallel threads; with the unportable option --parallel=1 it becomes slower: Test HEAD^ HEAD -------------------------------------------------------------------- 0071.2: sort(1) 0.21(0.18+0.01) 0.20(0.18+0.01) -4.8% 0071.3: string_list_sort() 0.16(0.13+0.02) 0.17(0.15+0.01) +6.3% There is some instability -- the numbers for the sort(1) check shouldn't be affected by this patch. Anyway, the performance of our qsort_s() implementation is apparently good enough, at least for this test. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 564e94e commit 5ebd947

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

string-list.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,18 @@ struct string_list_item *string_list_append(struct string_list *list,
211211
list->strdup_strings ? xstrdup(string) : (char *)string);
212212
}
213213

214-
/* Yuck */
215-
static compare_strings_fn compare_for_qsort;
216-
217-
/* Only call this from inside string_list_sort! */
218-
static int cmp_items(const void *a, const void *b)
214+
static int cmp_items(const void *a, const void *b, void *ctx)
219215
{
216+
compare_strings_fn cmp = ctx;
220217
const struct string_list_item *one = a;
221218
const struct string_list_item *two = b;
222-
return compare_for_qsort(one->string, two->string);
219+
return cmp(one->string, two->string);
223220
}
224221

225222
void string_list_sort(struct string_list *list)
226223
{
227-
compare_for_qsort = list->cmp ? list->cmp : strcmp;
228-
QSORT(list->items, list->nr, cmp_items);
224+
QSORT_S(list->items, list->nr, cmp_items,
225+
list->cmp ? list->cmp : strcmp);
229226
}
230227

231228
struct string_list_item *unsorted_string_list_lookup(struct string_list *list,

0 commit comments

Comments
 (0)