From: "René Scharfe" <l.s.r@web.de>
To: Jeff King <peff@peff.net>
Cc: Derrick Stolee <stolee@gmail.com>,
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>,
git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
Derrick Stolee <dstolee@microsoft.com>
Subject: Re: [PATCH 15/16] commit-reach: make can_all_from_reach... linear
Date: Fri, 5 Oct 2018 20:48:27 +0200 [thread overview]
Message-ID: <dca35e44-a763-bcf0-f457-b8dab53815cf@web.de> (raw)
In-Reply-To: <20181005165157.GC11254@sigill.intra.peff.net>
Am 05.10.2018 um 18:51 schrieb Jeff King:
> On Fri, Oct 05, 2018 at 12:59:02AM +0200, René Scharfe wrote:
>
>> We could also do something like this to reduce the amount of manual
>> casting, but do we want to? (Macro at the bottom, three semi-random
>> examples at the top.)
>> [...]
>> diff --git a/git-compat-util.h b/git-compat-util.h
>> index 5f2e90932f..f9e78d69a2 100644
>> --- a/git-compat-util.h
>> +++ b/git-compat-util.h
>> @@ -1066,6 +1066,18 @@ static inline void sane_qsort(void *base, size_t nmemb, size_t size,
>> qsort(base, nmemb, size, compar);
>> }
>>
>> +#define DEFINE_SORT(name, elemtype, one, two, code) \
>> +static int name##_compare(const void *one##_v_, const void *two##_v_) \
>> +{ \
>> + elemtype const *one = one##_v_; \
>> + elemtype const *two = two##_v_; \
>> + code; \
>> +} \
>> +static void name(elemtype *array, size_t n) \
>> +{ \
>> + QSORT(array, n, name##_compare); \
>> +}
>
> Interesting. When I saw the callers of this macro, I first thought you
> were just removing the casts from the comparison function, but the real
> value here is the matching QSORT() wrapper which provides the type
> safety.
Indeed.
> I'm not wild about declaring functions inside macros, just because it
> makes tools like ctags like useful (but I have certainly been guilty of
> it myself). I'd also worry that taking "code" as a macro parameter might
> not scale (what happens if the code has a comma in it?)
It works fine, as long as the comma is surrounded by parentheses, so
function calls with more than one parameter are fine without any change.
> I think we can address that last part by switching the definition order.
> Like:
>
> #define DEFINE_SORT(name, elemtype, one, two) \
> static int name##_compare(const void *, const void *); \
> static void name(elemtype *array, size_t n) \
> { \
> QSORT(array, n, name##_compare); \
> } \
> static int name##_compare(const void *one##_v_, const void *two##_v_) \
> { \
> elemtype const *one = one##_v_; \
> elemtype const *two = two##_v_; \
>
> And then expecting the caller to do:
>
> DEFINE_SORT(foo, struct foo, a, b)
> /* code goes here */
> }
>
> The unbalanced braces are nasty, though (and likely to screw up editor
> formatting, highlighting, etc).
Adding an extra pair of parentheses if needed is also not ideal, but has
less downsides, I think.
> I wonder if it would be possible to just declare the comparison function
> with its real types, and then teach QSORT() to do a type check. That
> would require typeof() at least, but it would be OK for the type-check
> to be available only to gcc/clang users, I think.
>
> I'm not quite sure what that type-check would look like, but I was
> thinking something along the lines of (inside the QSORT macro):
>
> do {
> /* this will yield a type mismatch if fed the wrong function */
> int (*check)(const typeof(array), const typeof(array)) = compar;
> sane_qsort(array, n, sizeof(*array), n);
> } while (0)
>
> I have no idea if that even comes close to compiling, though.
If the comparison function has proper types then we need to declare a
version with void pointer parameters as well to give to qsort(3). I
think using cast function pointers is undefined. Perhaps like this?
---
bisect.c | 11 +++++------
commit-graph.c | 8 ++++----
commit-reach.c | 12 +++++++-----
git-compat-util.h | 14 ++++++++++++++
4 files changed, 30 insertions(+), 15 deletions(-)
diff --git a/bisect.c b/bisect.c
index e8b17cf7e1..1fc6278c6b 100644
--- a/bisect.c
+++ b/bisect.c
@@ -192,17 +192,16 @@ struct commit_dist {
int distance;
};
-static int compare_commit_dist(const void *a_, const void *b_)
+static int compare_commit_dist(const struct commit_dist *a,
+ const struct commit_dist *b)
{
- struct commit_dist *a, *b;
-
- a = (struct commit_dist *)a_;
- b = (struct commit_dist *)b_;
if (a->distance != b->distance)
return b->distance - a->distance; /* desc sort */
return oidcmp(&a->commit->object.oid, &b->commit->object.oid);
}
+DEFINE_SORT(sort_by_commit_dist, struct commit_dist *, compare_commit_dist)
+
static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
{
struct commit_list *p;
@@ -223,7 +222,7 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n
array[cnt].distance = distance;
cnt++;
}
- QSORT(array, cnt, compare_commit_dist);
+ sort_by_commit_dist(array, cnt);
for (p = list, i = 0; i < cnt; i++) {
struct object *obj = &(array[i].commit->object);
diff --git a/commit-graph.c b/commit-graph.c
index 7f4519ec3b..07d302fefd 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -550,13 +550,13 @@ static void write_graph_chunk_large_edges(struct hashfile *f,
}
}
-static int commit_compare(const void *_a, const void *_b)
+static int commit_compare(const struct object_id *a, const struct object_id *b)
{
- const struct object_id *a = (const struct object_id *)_a;
- const struct object_id *b = (const struct object_id *)_b;
return oidcmp(a, b);
}
+DEFINE_SORT(sort_oids, struct object_id *, commit_compare)
+
struct packed_commit_list {
struct commit **list;
int nr;
@@ -780,7 +780,7 @@ void write_commit_graph(const char *obj_dir,
close_reachable(&oids);
- QSORT(oids.list, oids.nr, commit_compare);
+ sort_oids(oids.list, oids.nr);
count_distinct = 1;
for (i = 1; i < oids.nr; i++) {
diff --git a/commit-reach.c b/commit-reach.c
index 2f5e592d16..496c4201af 100644
--- a/commit-reach.c
+++ b/commit-reach.c
@@ -527,11 +527,11 @@ int commit_contains(struct ref_filter *filter, struct commit *commit,
return is_descendant_of(commit, list);
}
-static int compare_commits_by_gen(const void *_a, const void *_b)
+static int compare_commits_by_gen(const struct commit * const *ap,
+ const struct commit * const *bp)
{
- const struct commit *a = *(const struct commit * const *)_a;
- const struct commit *b = *(const struct commit * const *)_b;
-
+ const struct commit *a = *ap;
+ const struct commit *b = *bp;
if (a->generation < b->generation)
return -1;
if (a->generation > b->generation)
@@ -539,6 +539,8 @@ static int compare_commits_by_gen(const void *_a, const void *_b)
return 0;
}
+DEFINE_SORT(sort_commits_by_gen, struct commit **, compare_commits_by_gen)
+
int can_all_from_reach_with_flag(struct object_array *from,
unsigned int with_flag,
unsigned int assign_flag,
@@ -580,7 +582,7 @@ int can_all_from_reach_with_flag(struct object_array *from,
nr_commits++;
}
- QSORT(list, nr_commits, compare_commits_by_gen);
+ sort_commits_by_gen(list, nr_commits);
for (i = 0; i < nr_commits; i++) {
/* DFS from list[i] */
diff --git a/git-compat-util.h b/git-compat-util.h
index 5f2e90932f..2462173790 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -1066,6 +1066,20 @@ static inline void sane_qsort(void *base, size_t nmemb, size_t size,
qsort(base, nmemb, size, compar);
}
+#define DEFINE_SORT(name, type, compare) \
+static int compare##_void(const void *one, const void *two) \
+{ \
+ return compare(one, two); \
+} \
+static void name(type base, size_t nmemb) \
+{ \
+ const type dummy = NULL; \
+ if (nmemb > 1) \
+ qsort(base, nmemb, sizeof(base[0]), compare##_void); \
+ else if (0) \
+ compare(dummy, dummy); \
+}
+
#ifndef HAVE_ISO_QSORT_S
int git_qsort_s(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *, void *), void *ctx);
--
2.19.0
next prev parent reply other threads:[~2018-10-05 18:48 UTC|newest]
Thread overview: 118+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-16 13:00 [PATCH 00/16] Consolidate reachability logic Derrick Stolee via GitGitGadget
2018-06-19 20:25 ` [PATCH 04/16] upload-pack: make reachable() more generic Derrick Stolee via GitGitGadget
2018-06-19 20:35 ` [PATCH 05/16] upload-pack: refactor ok_to_give_up() Derrick Stolee via GitGitGadget
2018-06-25 17:16 ` [PATCH 01/16] commit-reach: move walk methods from commit.c Derrick Stolee via GitGitGadget
2018-07-16 18:57 ` Stefan Beller
2018-07-16 21:31 ` Jonathan Tan
2018-06-25 17:35 ` [PATCH 02/16] commit-reach: move ref_newer from remote.c Derrick Stolee via GitGitGadget
2018-07-16 19:10 ` Stefan Beller
2018-06-25 18:01 ` [PATCH 03/16] commit-reach: move commit_contains from ref-filter Derrick Stolee via GitGitGadget
2018-07-16 19:14 ` Stefan Beller
2018-06-28 12:31 ` [PATCH 15/16] commit-reach: make can_all_from_reach... linear Derrick Stolee via GitGitGadget
2018-07-16 22:37 ` Stefan Beller
2018-07-17 1:16 ` Jonathan Tan
2018-10-01 19:16 ` René Scharfe
2018-10-01 19:26 ` Derrick Stolee
2018-10-01 20:37 ` René Scharfe
2018-10-04 22:59 ` René Scharfe
2018-10-05 12:15 ` Derrick Stolee
2018-10-05 16:51 ` Jeff King
2018-10-05 18:48 ` René Scharfe [this message]
2018-10-05 19:08 ` Jeff King
2018-10-05 19:36 ` René Scharfe
2018-10-05 19:42 ` Jeff King
2018-10-14 14:29 ` René Scharfe
2018-10-15 15:31 ` Derrick Stolee
2018-10-15 16:26 ` René Scharfe
2018-10-16 23:09 ` Junio C Hamano
2018-10-17 8:33 ` Jeff King
2020-11-18 2:16 ` Jonathan Nieder
2020-11-18 6:54 ` Jeff King
2020-11-18 17:47 ` René Scharfe
2018-10-05 19:12 ` Ævar Arnfjörð Bjarmason
2018-10-05 19:28 ` Jeff King
2018-10-05 19:42 ` Ævar Arnfjörð Bjarmason
2018-10-05 19:44 ` Jeff King
2018-07-12 20:47 ` [PATCH 06/16] upload-pack: generalize commit date cutoff Derrick Stolee via GitGitGadget
2018-07-16 19:38 ` Stefan Beller
2018-07-18 16:04 ` Derrick Stolee
2018-07-12 20:52 ` [PATCH 07/16] commit-reach: move can_all_from_reach_with_flags Derrick Stolee via GitGitGadget
2018-07-16 22:37 ` Jonathan Tan
2018-07-13 14:06 ` [PATCH 08/16] test-reach: create new test tool for ref_newer Derrick Stolee via GitGitGadget
2018-07-16 23:00 ` Jonathan Tan
2018-07-18 16:14 ` Derrick Stolee
2018-07-13 14:28 ` [PATCH 09/16] test-reach: test in_merge_bases Derrick Stolee via GitGitGadget
2018-07-13 14:38 ` [PATCH 10/16] test-reach: test is_descendant_of Derrick Stolee via GitGitGadget
2018-07-13 14:51 ` [PATCH 11/16] test-reach: test get_merge_bases_many Derrick Stolee via GitGitGadget
2018-07-16 21:24 ` Stefan Beller
2018-07-16 23:08 ` Jonathan Tan
2018-07-13 16:51 ` [PATCH 12/16] test-reach: test reduce_heads Derrick Stolee via GitGitGadget
2018-07-16 21:30 ` Stefan Beller
2018-07-16 21:59 ` Eric Sunshine
2018-07-13 17:22 ` [PATCH 13/16] test-reach: test can_all_from_reach_with_flags Derrick Stolee via GitGitGadget
2018-07-16 21:54 ` Stefan Beller
2018-07-18 16:54 ` Derrick Stolee
2018-07-17 0:10 ` Jonathan Tan
2018-07-13 18:37 ` [PATCH 14/16] commit-reach: replace ref_newer logic Derrick Stolee via GitGitGadget
2018-07-16 22:16 ` Stefan Beller
2018-07-13 19:25 ` [PATCH 16/16] commit-reach: use can_all_from_reach Derrick Stolee via GitGitGadget
2018-07-16 22:47 ` Stefan Beller
2018-07-16 13:54 ` [PATCH 00/16] Consolidate reachability logic Ramsay Jones
2018-07-16 16:18 ` Jeff King
2018-07-16 18:40 ` Eric Sunshine
2018-07-16 18:56 ` Jeff King
2018-07-16 18:59 ` Eric Sunshine
2018-07-18 12:32 ` Johannes Schindelin
2018-07-18 12:23 ` Johannes Schindelin
2018-07-18 19:21 ` Jeff King
2018-07-19 16:34 ` Johannes Schindelin
2018-07-16 17:26 ` Stefan Beller
2018-07-16 18:44 ` Eric Sunshine
2018-07-16 18:47 ` Derrick Stolee
2018-07-18 12:28 ` Johannes Schindelin
2018-07-18 15:01 ` Duy Nguyen
2018-07-18 17:01 ` Junio C Hamano
2018-07-18 17:11 ` Derrick Stolee
2018-07-19 16:37 ` Johannes Schindelin
2018-07-19 16:32 ` Johannes Schindelin
2018-07-20 16:33 ` [PATCH v2 00/18] " Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 01/18] commit-reach: move walk methods from commit.c Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 02/18] commit.h: remove method declarations Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 03/18] commit-reach: move ref_newer from remote.c Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 04/18] commit-reach: move commit_contains from ref-filter Derrick Stolee
2018-08-28 21:24 ` Jonathan Nieder
2018-08-28 21:33 ` Derrick Stolee
2018-08-28 21:36 ` [PATCH] commit-reach: correct accidental #include of C file Jonathan Nieder
2018-08-28 21:39 ` Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 05/18] upload-pack: make reachable() more generic Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 06/18] upload-pack: refactor ok_to_give_up() Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 07/18] upload-pack: generalize commit date cutoff Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 08/18] commit-reach: move can_all_from_reach_with_flags Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 09/18] test-reach: create new test tool for ref_newer Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 10/18] test-reach: test in_merge_bases Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 11/18] test-reach: test is_descendant_of Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 12/18] test-reach: test get_merge_bases_many Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 13/18] test-reach: test reduce_heads Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 14/18] test-reach: test can_all_from_reach_with_flags Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 15/18] test-reach: test commit_contains Derrick Stolee
2018-07-23 20:35 ` Jonathan Tan
2018-07-25 18:08 ` Junio C Hamano
2018-07-25 18:30 ` Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 16/18] commit-reach: replace ref_newer logic Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 17/18] commit-reach: make can_all_from_reach... linear Derrick Stolee
2018-07-23 20:41 ` Jonathan Tan
2018-08-01 20:41 ` Derrick Stolee
2018-09-12 4:14 ` Jeff King
2018-09-12 4:29 ` Jeff King
2018-09-12 13:08 ` Derrick Stolee
2018-07-20 16:33 ` [PATCH v2 18/18] commit-reach: use can_all_from_reach Derrick Stolee
2018-07-20 17:10 ` [PATCH v2 00/18] Consolidate reachability logic Stefan Beller
2018-07-20 17:15 ` Derrick Stolee
2018-07-20 22:16 ` Stefan Beller
2018-08-01 20:33 ` Derrick Stolee
2018-07-20 17:18 ` Derrick Stolee
2018-07-20 18:09 ` Eric Sunshine
2018-07-20 19:14 ` Derrick Stolee
2018-07-20 17:41 ` Duy Nguyen
2018-07-20 19:09 ` Derrick Stolee
2018-07-20 22:45 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=dca35e44-a763-bcf0-f457-b8dab53815cf@web.de \
--to=l.s.r@web.de \
--cc=dstolee@microsoft.com \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
--cc=stolee@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).