git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
* [PATCH] bisect: loosen halfway() check for a large number of commits
@ 2020-10-22 10:38 SZEDER Gábor
  2020-10-22 10:40 ` SZEDER Gábor
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: SZEDER Gábor @ 2020-10-22 10:38 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, SZEDER Gábor

'git bisect start ...' and subsequent 'git bisect (good|bad)' commands
can take quite a while when the given/remaining revision range between
good and bad commits is big and contains a lot of merge commits, e.g.
in git.git:

  $ git rev-list --count v1.6.0..v2.28.0
  44284
  $ time git bisect start v2.28.0 v1.6.0
  Bisecting: 22141 revisions left to test after this (roughly 15 steps)
  [e197c21807dacadc8305250baa0b9228819189d4] unable_to_lock_die(): rename function from unable_to_lock_index_die()

  real    0m15.472s
  user    0m15.220s
  sys     0m0.255s

The majority of the runtime is spent in do_find_bisection(), where we
try to find a commit as close as possible to the halfway point between
the bad and good revisions, i.e. a commit from which the number of
reachable commits that are in the good-bad range is half the total
number of commits in that range.  So we count how many commits are
reachable in the good-bad range for each commit in that range, which
is quick and easy for a linear history, even over 300k commits in a
linear range are handled in ~0.3s on my machine.  Alas, handling merge
commits is non-trivial and quite expensive as the algorithm used seems
to be quadratic, causing the long runtime shown above.

Interestingly, look at what a big difference one additional commit
can make:

  $ git rev-list --count v1.6.0^..v2.28.0
  44285
  $ time git bisect start v2.28.0 v1.6.0^
  Bisecting: 22142 revisions left to test after this (roughly 15 steps)
  [565301e41670825ceedf75220f2918ae76831240] Sync with 2.1.2

  real  0m5.848s
  user  0m5.600s
  sys   0m0.252s

The difference is caused by one of the optimizations attempting to cut
down the runtime added in 1c4fea3a40 (git-rev-list --bisect:
optimization, 2007-03-21):

    Another small optimization is whenever we find a half-way commit
    (that is, a commit that can reach exactly half of the commits),
    we stop giving counts to remaining commits, as we will not find
    any better commit than we just found.

In this second 'git bisect start' command we happen to find a commit
exactly at the halfway point and can return early, but in the first
case there is no such commit, so we can't return early and end up
counting the number of reachable commits from all commits in the
good-bad range.

However, when we have thousands of commits it's not all that important
to find the _exact_ halfway point, a few commits more or less doesn't
make any real difference for the bisection.

So let's loosen the halfway check to consider commits within about
0.1% of the exact halfway point as halfway as well.  This will allow
us to return early on a bigger good-bad range, even when there is no
commit exactly at the halfway point, thereby reducing the runtime of
the first command above considerably, from ~15s to 4.901s.
Furthermore, even if there is a commit exactly at the halfway point,
we might still stumble upon a commit within that 0.1% range before
finding the exact halfway point, allowing us to return a bit earlier,
slightly reducing the runtime of the second command from 5.848s to
5.058s.  Note that this change doesn't affect good-bad ranges
containing ~2000 commits or less, because that 0.1% tolerance becomes
zero due to integer arithmetic; however, if the range is that small
then counting the reachable commits for all commits is already fast
enough anyway.

Naturally, this will likely change which commits get picked at each
bisection step, and, in turn, might change how many bisection steps
are necessary to find the first bad commit.  If the number of
necessary bisection steps were to increase often, then this change
could backfire, because building and testing at each step might take
much longer than the time spared.  OTOH, if the number of steps were
to decrease, then it would be a double win.

So I ran some tests to see how often that happens: picked random good
and bad starting revisions at least 50k commits apart and a random
first bad commit in between in git.git, and used 'git bisect run git
merge-base --is-ancestor HEAD $first_bad_commit' to check the number
of necessary bisection steps.  After repeating all this 1000 times
both with and without this patch I found that:

  - 146 cases needed one more bisection step than before, 149 cases
    needed one less step, while in the remaining 705 cases the number
    of steps didn't change.  So the number of bisection steps does
    indeed change in a non-negligible number of cases, but it seems
    that the average number of steps doesn't change in the long run.

  - The first 'git bisect start' command got over 3x faster in 456
    cases, so this "no commit at the exact halfway point" case seems
    to be common enough to care about.

[TODO:
  - Update comments at callsites mentioning "exact halfway".
  - Rename function to approx_halfway(), perhaps?]
---
 bisect.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/bisect.c b/bisect.c
index f5b1368128..1857ce4c75 100644
--- a/bisect.c
+++ b/bisect.c
@@ -105,6 +105,8 @@ static int count_interesting_parents(struct commit *commit, unsigned bisect_flag
 
 static inline int halfway(struct commit_list *p, int nr)
 {
+	int diff;
+
 	/*
 	 * Don't short-cut something we are not going to return!
 	 */
@@ -113,13 +115,22 @@ static inline int halfway(struct commit_list *p, int nr)
 	if (DEBUG_BISECT)
 		return 0;
 	/*
-	 * 2 and 3 are halfway of 5.
+	 * For small number of commits 2 and 3 are halfway of 5, and
 	 * 3 is halfway of 6 but 2 and 4 are not.
 	 */
-	switch (2 * weight(p) - nr) {
+	diff = 2 * weight(p) - nr;
+	switch (diff) {
 	case -1: case 0: case 1:
 		return 1;
 	default:
+		/*
+		 * For large number of commits we are not so strict, it's
+		 * good enough if it's within ~0.1% of the halfway point,
+		 * e.g. 5000 is exactly halfway of 10000, but we consider
+		 * the values [4996, 5004] as halfway as well.
+		 */
+		if (abs(diff) < nr / 1024)
+			return 1;
 		return 0;
 	}
 }
-- 
2.29.0.470.g6462f21d4e


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] bisect: loosen halfway() check for a large number of commits
  2020-10-22 10:38 [PATCH] bisect: loosen halfway() check for a large number of commits SZEDER Gábor
@ 2020-10-22 10:40 ` SZEDER Gábor
  2020-10-22 17:18 ` Junio C Hamano
  2020-11-12 16:19 ` [PATCH v2] " SZEDER Gábor
  2 siblings, 0 replies; 7+ messages in thread
From: SZEDER Gábor @ 2020-10-22 10:40 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano

This is only RFC, but forgot to mark it as such in Subject: line,
sorry.


On Thu, Oct 22, 2020 at 12:38:06PM +0200, SZEDER Gábor wrote:
> 'git bisect start ...' and subsequent 'git bisect (good|bad)' commands
> can take quite a while when the given/remaining revision range between
> good and bad commits is big and contains a lot of merge commits, e.g.
> in git.git:
> 
>   $ git rev-list --count v1.6.0..v2.28.0
>   44284
>   $ time git bisect start v2.28.0 v1.6.0
>   Bisecting: 22141 revisions left to test after this (roughly 15 steps)
>   [e197c21807dacadc8305250baa0b9228819189d4] unable_to_lock_die(): rename function from unable_to_lock_index_die()
> 
>   real    0m15.472s
>   user    0m15.220s
>   sys     0m0.255s
> 
> The majority of the runtime is spent in do_find_bisection(), where we
> try to find a commit as close as possible to the halfway point between
> the bad and good revisions, i.e. a commit from which the number of
> reachable commits that are in the good-bad range is half the total
> number of commits in that range.  So we count how many commits are
> reachable in the good-bad range for each commit in that range, which
> is quick and easy for a linear history, even over 300k commits in a
> linear range are handled in ~0.3s on my machine.  Alas, handling merge
> commits is non-trivial and quite expensive as the algorithm used seems
> to be quadratic, causing the long runtime shown above.
> 
> Interestingly, look at what a big difference one additional commit
> can make:
> 
>   $ git rev-list --count v1.6.0^..v2.28.0
>   44285
>   $ time git bisect start v2.28.0 v1.6.0^
>   Bisecting: 22142 revisions left to test after this (roughly 15 steps)
>   [565301e41670825ceedf75220f2918ae76831240] Sync with 2.1.2
> 
>   real  0m5.848s
>   user  0m5.600s
>   sys   0m0.252s
> 
> The difference is caused by one of the optimizations attempting to cut
> down the runtime added in 1c4fea3a40 (git-rev-list --bisect:
> optimization, 2007-03-21):
> 
>     Another small optimization is whenever we find a half-way commit
>     (that is, a commit that can reach exactly half of the commits),
>     we stop giving counts to remaining commits, as we will not find
>     any better commit than we just found.
> 
> In this second 'git bisect start' command we happen to find a commit
> exactly at the halfway point and can return early, but in the first
> case there is no such commit, so we can't return early and end up
> counting the number of reachable commits from all commits in the
> good-bad range.
> 
> However, when we have thousands of commits it's not all that important
> to find the _exact_ halfway point, a few commits more or less doesn't
> make any real difference for the bisection.
> 
> So let's loosen the halfway check to consider commits within about
> 0.1% of the exact halfway point as halfway as well.  This will allow
> us to return early on a bigger good-bad range, even when there is no
> commit exactly at the halfway point, thereby reducing the runtime of
> the first command above considerably, from ~15s to 4.901s.
> Furthermore, even if there is a commit exactly at the halfway point,
> we might still stumble upon a commit within that 0.1% range before
> finding the exact halfway point, allowing us to return a bit earlier,
> slightly reducing the runtime of the second command from 5.848s to
> 5.058s.  Note that this change doesn't affect good-bad ranges
> containing ~2000 commits or less, because that 0.1% tolerance becomes
> zero due to integer arithmetic; however, if the range is that small
> then counting the reachable commits for all commits is already fast
> enough anyway.
> 
> Naturally, this will likely change which commits get picked at each
> bisection step, and, in turn, might change how many bisection steps
> are necessary to find the first bad commit.  If the number of
> necessary bisection steps were to increase often, then this change
> could backfire, because building and testing at each step might take
> much longer than the time spared.  OTOH, if the number of steps were
> to decrease, then it would be a double win.
> 
> So I ran some tests to see how often that happens: picked random good
> and bad starting revisions at least 50k commits apart and a random
> first bad commit in between in git.git, and used 'git bisect run git
> merge-base --is-ancestor HEAD $first_bad_commit' to check the number
> of necessary bisection steps.  After repeating all this 1000 times
> both with and without this patch I found that:
> 
>   - 146 cases needed one more bisection step than before, 149 cases
>     needed one less step, while in the remaining 705 cases the number
>     of steps didn't change.  So the number of bisection steps does
>     indeed change in a non-negligible number of cases, but it seems
>     that the average number of steps doesn't change in the long run.
> 
>   - The first 'git bisect start' command got over 3x faster in 456
>     cases, so this "no commit at the exact halfway point" case seems
>     to be common enough to care about.
> 
> [TODO:
>   - Update comments at callsites mentioning "exact halfway".
>   - Rename function to approx_halfway(), perhaps?]
> ---
>  bisect.c | 15 +++++++++++++--
>  1 file changed, 13 insertions(+), 2 deletions(-)
> 
> diff --git a/bisect.c b/bisect.c
> index f5b1368128..1857ce4c75 100644
> --- a/bisect.c
> +++ b/bisect.c
> @@ -105,6 +105,8 @@ static int count_interesting_parents(struct commit *commit, unsigned bisect_flag
>  
>  static inline int halfway(struct commit_list *p, int nr)
>  {
> +	int diff;
> +
>  	/*
>  	 * Don't short-cut something we are not going to return!
>  	 */
> @@ -113,13 +115,22 @@ static inline int halfway(struct commit_list *p, int nr)
>  	if (DEBUG_BISECT)
>  		return 0;
>  	/*
> -	 * 2 and 3 are halfway of 5.
> +	 * For small number of commits 2 and 3 are halfway of 5, and
>  	 * 3 is halfway of 6 but 2 and 4 are not.
>  	 */
> -	switch (2 * weight(p) - nr) {
> +	diff = 2 * weight(p) - nr;
> +	switch (diff) {
>  	case -1: case 0: case 1:
>  		return 1;
>  	default:
> +		/*
> +		 * For large number of commits we are not so strict, it's
> +		 * good enough if it's within ~0.1% of the halfway point,
> +		 * e.g. 5000 is exactly halfway of 10000, but we consider
> +		 * the values [4996, 5004] as halfway as well.
> +		 */
> +		if (abs(diff) < nr / 1024)
> +			return 1;
>  		return 0;
>  	}
>  }
> -- 
> 2.29.0.470.g6462f21d4e
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] bisect: loosen halfway() check for a large number of commits
  2020-10-22 10:38 [PATCH] bisect: loosen halfway() check for a large number of commits SZEDER Gábor
  2020-10-22 10:40 ` SZEDER Gábor
@ 2020-10-22 17:18 ` Junio C Hamano
  2020-10-24  7:41   ` Christian Couder
  2020-11-12 16:19 ` [PATCH v2] " SZEDER Gábor
  2 siblings, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2020-10-22 17:18 UTC (permalink / raw)
  To: SZEDER Gábor; +Cc: git

SZEDER Gábor <szeder.dev@gmail.com> writes:

> However, when we have thousands of commits it's not all that important
> to find the _exact_ halfway point, a few commits more or less doesn't
> make any real difference for the bisection.

Cute idea.

> So I ran some tests to see how often that happens: picked random good
> and bad starting revisions at least 50k commits apart and a random
> first bad commit in between in git.git, and used 'git bisect run git
> merge-base --is-ancestor HEAD $first_bad_commit' to check the number
> of necessary bisection steps.  After repeating all this 1000 times
> both with and without this patch I found that:
>
>   - 146 cases needed one more bisection step than before, 149 cases
>     needed one less step, while in the remaining 705 cases the number
>     of steps didn't change.  So the number of bisection steps does
>     indeed change in a non-negligible number of cases, but it seems
>     that the average number of steps doesn't change in the long run.

It somehow is a bit surprising that there are cases that need fewer
steps, but I guess that is how rounding-error cuts both ways?

>   - The first 'git bisect start' command got over 3x faster in 456
>     cases, so this "no commit at the exact halfway point" case seems
>     to be common enough to care about.

In any case, I like the re-realization that the counting reachable
commits in a mergy history is costly (see comments before the
count_distance() function that we already knew it from the
beginning, though), and the general idea of speeding up the entire
thing by avoiding the cost we need to pay in the count_distance()
function, which my earlier 1c4fea3a (git-rev-list --bisect:
optimization, 2007-03-21) also did.

    Side note.  I've been waiting for all these years to see
    somebody new comes up and makes a fundamental change to
    count_distance() such that it no longer is costly---alas,
    that hasn't happened yet.

Mildly (only because such a bisection session over a long span is
rarer) excited to see this RFC completed ;-)






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] bisect: loosen halfway() check for a large number of commits
  2020-10-22 17:18 ` Junio C Hamano
@ 2020-10-24  7:41   ` Christian Couder
  2020-10-25 18:01     ` SZEDER Gábor
  0 siblings, 1 reply; 7+ messages in thread
From: Christian Couder @ 2020-10-24  7:41 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: SZEDER Gábor, git

On Thu, Oct 22, 2020 at 8:20 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> SZEDER Gábor <szeder.dev@gmail.com> writes:
>
> > However, when we have thousands of commits it's not all that important
> > to find the _exact_ halfway point, a few commits more or less doesn't
> > make any real difference for the bisection.
>
> Cute idea.

I like the idea too.

> > So I ran some tests to see how often that happens: picked random good
> > and bad starting revisions at least 50k commits apart and a random
> > first bad commit in between in git.git, and used 'git bisect run git
> > merge-base --is-ancestor HEAD $first_bad_commit' to check the number
> > of necessary bisection steps.  After repeating all this 1000 times
> > both with and without this patch I found that:
> >
> >   - 146 cases needed one more bisection step than before, 149 cases
> >     needed one less step, while in the remaining 705 cases the number
> >     of steps didn't change.  So the number of bisection steps does
> >     indeed change in a non-negligible number of cases, but it seems
> >     that the average number of steps doesn't change in the long run.
>
> It somehow is a bit surprising that there are cases that need fewer
> steps, but I guess that is how rounding-error cuts both ways?

When there are 50k commits span between the initial good and bad, I
don't expect to see any statistically significant result by trying it
1k times only. My guess is that you might start seeing something
significant only when the number of tries is a multiple of the span
between the initial good and bad.

There is some cost on average even if it's small (and gets smaller
when the span increases) of not using the best halfway commit, so the
overall gain depends on how long it takes (and possibly how much it
costs) to run the test script (or maybe to manually test).
Unfortunately without any hint from the user or without recording how
long the test script lasts (which doesn't cover manual testing) we
cannot know this cost of testing which could change a lot between use
cases.

> Mildly (only because such a bisection session over a long span is
> rarer) excited to see this RFC completed ;-)

In projects like the Linux kernel where there are around 10k commits
between 2 feature releases, such bisections over a long span might
actually happen quite often.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] bisect: loosen halfway() check for a large number of commits
  2020-10-24  7:41   ` Christian Couder
@ 2020-10-25 18:01     ` SZEDER Gábor
  0 siblings, 0 replies; 7+ messages in thread
From: SZEDER Gábor @ 2020-10-25 18:01 UTC (permalink / raw)
  To: Christian Couder; +Cc: Junio C Hamano, git

On Sat, Oct 24, 2020 at 09:41:27AM +0200, Christian Couder wrote:
> On Thu, Oct 22, 2020 at 8:20 PM Junio C Hamano <gitster@pobox.com> wrote:
> >
> > SZEDER Gábor <szeder.dev@gmail.com> writes:
> >
> > > However, when we have thousands of commits it's not all that important
> > > to find the _exact_ halfway point, a few commits more or less doesn't
> > > make any real difference for the bisection.
> >
> > Cute idea.
> 
> I like the idea too.
> 
> > > So I ran some tests to see how often that happens: picked random good
> > > and bad starting revisions at least 50k commits apart and a random
> > > first bad commit in between in git.git, and used 'git bisect run git
> > > merge-base --is-ancestor HEAD $first_bad_commit' to check the number
> > > of necessary bisection steps.  After repeating all this 1000 times
> > > both with and without this patch I found that:
> > >
> > >   - 146 cases needed one more bisection step than before, 149 cases
> > >     needed one less step, while in the remaining 705 cases the number
> > >     of steps didn't change.  So the number of bisection steps does
> > >     indeed change in a non-negligible number of cases, but it seems
> > >     that the average number of steps doesn't change in the long run.
> >
> > It somehow is a bit surprising that there are cases that need fewer
> > steps, but I guess that is how rounding-error cuts both ways?
> 
> When there are 50k commits span between the initial good and bad, I
> don't expect to see any statistically significant result by trying it
> 1k times only. My guess is that you might start seeing something
> significant only when the number of tries is a multiple of the span
> between the initial good and bad.

Well, perhaps...  but statistically relevant or not, running those
1000 tests I reported about took over 6.5 hours, so that's all you'll
get from me :)

Btw, just for curiosity, running just _one_ similar test in linux.git
with the good-bad range containing ~830k commits took ~65 minutes, and
the runtime of 'git bisect start' went from ~38mins to ~12.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2] bisect: loosen halfway() check for a large number of commits
  2020-10-22 10:38 [PATCH] bisect: loosen halfway() check for a large number of commits SZEDER Gábor
  2020-10-22 10:40 ` SZEDER Gábor
  2020-10-22 17:18 ` Junio C Hamano
@ 2020-11-12 16:19 ` SZEDER Gábor
  2020-11-12 18:23   ` Junio C Hamano
  2 siblings, 1 reply; 7+ messages in thread
From: SZEDER Gábor @ 2020-11-12 16:19 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, SZEDER Gábor

'git bisect start ...' and subsequent 'git bisect (good|bad)' commands
can take quite a while when the given/remaining revision range between
good and bad commits is big and contains a lot of merge commits, e.g.
in git.git:

  $ git rev-list --count v1.6.0..v2.28.0
  44284
  $ time git bisect start v2.28.0 v1.6.0
  Bisecting: 22141 revisions left to test after this (roughly 15 steps)
  [e197c21807dacadc8305250baa0b9228819189d4] unable_to_lock_die(): rename function from unable_to_lock_index_die()

  real    0m15.472s
  user    0m15.220s
  sys     0m0.255s

The majority of the runtime is spent in do_find_bisection(), where we
try to find a commit as close as possible to the halfway point between
the bad and good revisions, i.e. a commit from which the number of
reachable commits that are in the good-bad range is half the total
number of commits in that range.  So we count how many commits are
reachable in the good-bad range for each commit in that range, which
is quick and easy for a linear history, even over 300k commits in a
linear range are handled in ~0.3s on my machine.  Alas, handling merge
commits is non-trivial and quite expensive as the algorithm used seems
to be quadratic, causing the long runtime shown above.

Interestingly, look at what a big difference one additional commit
can make:

  $ git rev-list --count v1.6.0^..v2.28.0
  44285
  $ time git bisect start v2.28.0 v1.6.0^
  Bisecting: 22142 revisions left to test after this (roughly 15 steps)
  [565301e41670825ceedf75220f2918ae76831240] Sync with 2.1.2

  real  0m5.848s
  user  0m5.600s
  sys   0m0.252s

The difference is caused by one of the optimizations attempting to cut
down the runtime added in 1c4fea3a40 (git-rev-list --bisect:
optimization, 2007-03-21):

    Another small optimization is whenever we find a half-way commit
    (that is, a commit that can reach exactly half of the commits),
    we stop giving counts to remaining commits, as we will not find
    any better commit than we just found.

In this second 'git bisect start' command we happen to find a commit
exactly at the halfway point and can return early, but in the first
case there is no such commit, so we can't return early and end up
counting the number of reachable commits from all commits in the
good-bad range.

However, when we have thousands of commits it's not all that important
to find the _exact_ halfway point, a few commits more or less doesn't
make any real difference for the bisection.

So let's loosen the check in the halfway() helper to consider commits
within about 0.1% of the exact halfway point as halfway as well, and
rename the function to approx_halfway() accordingly.  This will allow
us to return early on a bigger good-bad range, even when there is no
commit exactly at the halfway point, thereby reducing the runtime of
the first command above considerably, from ~15s to 4.901s.
Furthermore, even if there is a commit exactly at the halfway point,
we might still stumble upon a commit within that 0.1% range before
finding the exact halfway point, allowing us to return a bit earlier,
slightly reducing the runtime of the second command from 5.848s to
5.058s.  Note that this change doesn't affect good-bad ranges
containing ~2000 commits or less, because that 0.1% tolerance becomes
zero due to integer arithmetic; however, if the range is that small
then counting the reachable commits for all commits is already fast
enough anyway.

Naturally, this will likely change which commits get picked at each
bisection step, and, in turn, might change how many bisection steps
are necessary to find the first bad commit.  If the number of
necessary bisection steps were to increase often, then this change
could backfire, because building and testing at each step might take
much longer than the time spared.  OTOH, if the number of steps were
to decrease, then it would be a double win.

So I ran some tests to see how often that happens: picked random good
and bad starting revisions at least 50k commits apart and a random
first bad commit in between in git.git, and used 'git bisect run git
merge-base --is-ancestor HEAD $first_bad_commit' to check the number
of necessary bisection steps.  After repeating all this 1000 times
both with and without this patch I found that:

  - 146 cases needed one more bisection step than before, 149 cases
    needed one less step, while in the remaining 705 cases the number
    of steps didn't change.  So the number of bisection steps does
    indeed change in a non-negligible number of cases, but it seems
    that the average number of steps doesn't change in the long run.

  - The first 'git bisect start' command got over 3x faster in 456
    cases, so this "no commit at the exact halfway point" case seems
    to be common enough to care about.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
Range-diff:
1:  79ae48469f ! 1:  1a2f5135de bisect: loosen halfway() check for a large number of commits
    @@ Commit message
         to find the _exact_ halfway point, a few commits more or less doesn't
         make any real difference for the bisection.
     
    -    So let's loosen the halfway check to consider commits within about
    -    0.1% of the exact halfway point as halfway as well.  This will allow
    +    So let's loosen the check in the halfway() helper to consider commits
    +    within about 0.1% of the exact halfway point as halfway as well, and
    +    rename the function to approx_halfway() accordingly.  This will allow
         us to return early on a bigger good-bad range, even when there is no
         commit exactly at the halfway point, thereby reducing the runtime of
         the first command above considerably, from ~15s to 4.901s.
    @@ Commit message
             cases, so this "no commit at the exact halfway point" case seems
             to be common enough to care about.
     
    -    [TODO:
    -      - Update comments at callsites mentioning "exact halfway".
    -      - Rename function to approx_halfway(), perhaps?]
    +    Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
     
      ## bisect.c ##
     @@ bisect.c: static int count_interesting_parents(struct commit *commit, unsigned bisect_flag
    + 	return count;
    + }
      
    - static inline int halfway(struct commit_list *p, int nr)
    +-static inline int halfway(struct commit_list *p, int nr)
    ++static inline int approx_halfway(struct commit_list *p, int nr)
      {
     +	int diff;
     +
    @@ bisect.c: static inline int halfway(struct commit_list *p, int nr)
      		return 0;
      	}
      }
    +@@ bisect.c: static struct commit_list *do_find_bisection(struct commit_list *list,
    + 		weight_set(p, count_distance(p));
    + 		clear_distance(list);
    + 
    +-		/* Does it happen to be at exactly half-way? */
    +-		if (!(bisect_flags & FIND_BISECTION_ALL) && halfway(p, nr))
    ++		/* Does it happen to be at half-way? */
    ++		if (!(bisect_flags & FIND_BISECTION_ALL) &&
    ++		      approx_halfway(p, nr))
    + 			return p;
    + 		counted++;
    + 	}
    +@@ bisect.c: static struct commit_list *do_find_bisection(struct commit_list *list,
    + 			else
    + 				weight_set(p, weight(q));
    + 
    +-			/* Does it happen to be at exactly half-way? */
    +-			if (!(bisect_flags & FIND_BISECTION_ALL) && halfway(p, nr))
    ++			/* Does it happen to be at half-way? */
    ++			if (!(bisect_flags & FIND_BISECTION_ALL) &&
    ++			      approx_halfway(p, nr))
    + 				return p;
    + 		}
    + 	}

 bisect.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/bisect.c b/bisect.c
index f5b1368128..bedce28cb6 100644
--- a/bisect.c
+++ b/bisect.c
@@ -103,8 +103,10 @@ static int count_interesting_parents(struct commit *commit, unsigned bisect_flag
 	return count;
 }
 
-static inline int halfway(struct commit_list *p, int nr)
+static inline int approx_halfway(struct commit_list *p, int nr)
 {
+	int diff;
+
 	/*
 	 * Don't short-cut something we are not going to return!
 	 */
@@ -113,13 +115,22 @@ static inline int halfway(struct commit_list *p, int nr)
 	if (DEBUG_BISECT)
 		return 0;
 	/*
-	 * 2 and 3 are halfway of 5.
+	 * For small number of commits 2 and 3 are halfway of 5, and
 	 * 3 is halfway of 6 but 2 and 4 are not.
 	 */
-	switch (2 * weight(p) - nr) {
+	diff = 2 * weight(p) - nr;
+	switch (diff) {
 	case -1: case 0: case 1:
 		return 1;
 	default:
+		/*
+		 * For large number of commits we are not so strict, it's
+		 * good enough if it's within ~0.1% of the halfway point,
+		 * e.g. 5000 is exactly halfway of 10000, but we consider
+		 * the values [4996, 5004] as halfway as well.
+		 */
+		if (abs(diff) < nr / 1024)
+			return 1;
 		return 0;
 	}
 }
@@ -321,8 +332,9 @@ static struct commit_list *do_find_bisection(struct commit_list *list,
 		weight_set(p, count_distance(p));
 		clear_distance(list);
 
-		/* Does it happen to be at exactly half-way? */
-		if (!(bisect_flags & FIND_BISECTION_ALL) && halfway(p, nr))
+		/* Does it happen to be at half-way? */
+		if (!(bisect_flags & FIND_BISECTION_ALL) &&
+		      approx_halfway(p, nr))
 			return p;
 		counted++;
 	}
@@ -362,8 +374,9 @@ static struct commit_list *do_find_bisection(struct commit_list *list,
 			else
 				weight_set(p, weight(q));
 
-			/* Does it happen to be at exactly half-way? */
-			if (!(bisect_flags & FIND_BISECTION_ALL) && halfway(p, nr))
+			/* Does it happen to be at half-way? */
+			if (!(bisect_flags & FIND_BISECTION_ALL) &&
+			      approx_halfway(p, nr))
 				return p;
 		}
 	}
-- 
2.29.2.588.gcd0acd9177


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] bisect: loosen halfway() check for a large number of commits
  2020-11-12 16:19 ` [PATCH v2] " SZEDER Gábor
@ 2020-11-12 18:23   ` Junio C Hamano
  0 siblings, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2020-11-12 18:23 UTC (permalink / raw)
  To: SZEDER Gábor; +Cc: git

SZEDER Gábor <szeder.dev@gmail.com> writes:

> So let's loosen the check in the halfway() helper to consider commits
> within about 0.1% of the exact halfway point as halfway as well, and
> rename the function to approx_halfway() accordingly.  This will allow
> us to return early on a bigger good-bad range, even when there is no
> commit exactly at the halfway point, thereby reducing the runtime of
> the first command above considerably, from ~15s to 4.901s.

The optimization presented with this change would probably offer
more merge commits to be tested than a single-parent commit than the
original algorithm, simply because merges are inspected first before
single-parent commits so have better chance to be picked as "good
enough" among commits with similar goodness.

  Side note: This is merely an observation---I do not know if it is
  a good thing, a bad thing, or a neutral thing, but it would likely
  affect the end-user experience.

The optimization presented here gives probably more than enough
improvement, but it just occured to me when writing the entry to
explain the topic in the What's cooking report:

     "git bisect start/next" in a large span of history spends a lot of
     time trying to come up with exactly the half-way point; this can be
     optimized by stopping when we see a commit that is close enough to
     the half-way point.

The realization is that the optimization naturally will be affected
by the order the commits are visited.  If a commit that is close
enough to the half-way point happens to be visited earlier, it would
help terminate our search early.  And do_find_bisection() search
counts all merge commits before any commit on the linear ancestry
chain can be counted to optimize counting of commits on the linear
ancestry chain, which are expected to exist more than merge commits.

By sorting the "list" to somehow encourage commits near the half-way
appear early on it, we may raise the likelyhood that we'd find
good-enough commit early and terminate, no?  Perhaps sort by the
(absolute) distance between the committer timestamp of individual
commit and its median value, or something?

Thanks.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-11-12 18:23 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-22 10:38 [PATCH] bisect: loosen halfway() check for a large number of commits SZEDER Gábor
2020-10-22 10:40 ` SZEDER Gábor
2020-10-22 17:18 ` Junio C Hamano
2020-10-24  7:41   ` Christian Couder
2020-10-25 18:01     ` SZEDER Gábor
2020-11-12 16:19 ` [PATCH v2] " SZEDER Gábor
2020-11-12 18:23   ` Junio C Hamano

git@vger.kernel.org list mirror (unofficial, one of many)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 git git/ https://public-inbox.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git
	nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git