From d766c0a42a6877cc36d9018815b377577a00ee92 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 9 May 2005 23:54:26 +0000 Subject: (fts_sort): Optimize the common case where all pointers smell the same. --- lib/fts.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'lib/fts.c') diff --git a/lib/fts.c b/lib/fts.c index 281ed37ab..f82ef0fe3 100644 --- a/lib/fts.c +++ b/lib/fts.c @@ -1240,6 +1240,20 @@ fts_sort (FTS *sp, FTSENT *head, register size_t nitems) { register FTSENT **ap, *p; + /* On most modern hosts, void * and FTSENT ** have the same + run-time representation, and one can convert sp->fts_compar to + the type qsort expects without problem. Use the heuristic that + this is OK if the two pointer types are the same size, and if + converting FTSENT ** to long int is the same as converting + FTSENT ** to void * and then to long int. This heuristic isn't + valid in general but we don't know of any counterexamples. */ + FTSENT *dummy; + int (*compare) (void const *, void const *) = + ((sizeof &dummy == sizeof (void *) + && (long int) &dummy == (long int) (void *) &dummy) + ? (int (*) (void const *, void const *)) sp->fts_compar + : fts_compar); + /* * Construct an array of pointers to the structures and call qsort(3). * Reassemble the array in the order returned by qsort. If unable to @@ -1263,7 +1277,7 @@ fts_sort (FTS *sp, FTSENT *head, register size_t nitems) } for (ap = sp->fts_array, p = head; p; p = p->fts_link) *ap++ = p; - qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), fts_compar); + qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), compare); for (head = *(ap = sp->fts_array); --nitems; ++ap) ap[0]->fts_link = ap[1]; ap[0]->fts_link = NULL; -- cgit v1.2.3-54-g00ecf