diff mbox series

[RFC,1/7] fetch-pack: allow NULL negotiator->add_tip

Message ID 044a782863dd690ed4e430b3793538f701a850c6.1596590295.git.jonathantanmy@google.com (mailing list archive)
State New, archived
Headers show
Series Lazy fetch with subprocess | expand

Commit Message

Jonathan Tan Aug. 5, 2020, 1:20 a.m. UTC
In a subsequent patch, a null fetch negotiator will be introduced. This
negotiator, among other things, will not need any tip information and
will have a NULL add_tip.

Teach fetch-pack to allow this, and to take advantage of this by
avoiding a ref and alternate traversal when it can. To do this, this
patch combines all invocations of functions that invoke add_tip into one
function, and that function first checks whether add_tip is NULL.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
---
 fetch-pack.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

Comments

Junio C Hamano Aug. 5, 2020, 7:53 p.m. UTC | #1
Jonathan Tan <jonathantanmy@google.com> writes:

> In a subsequent patch, a null fetch negotiator will be introduced. This
> negotiator, among other things, will not need any tip information and
> will have a NULL add_tip.

After reading the original code that the patch touches, I know what
"a NULL add_tip" is, but it would have been nicer to readers if you
said "callback" or "method" or "function".

With or without the "if add_tip method is set to NULL, return
without doing anything" optimization, it does make sense to refactor
two existing places that do identical things.  

If all the current negotiators never have NULL in the .add_tip
field, however, I'd suspect that it would make it easier to
understand if this step is explained as refactoring duplicated code
into a single helper _without_ the early-return for optimization,
and then the step that introduces the negotiator that does want to
lack add_tip method to add the early return.  After all, if the null
negotiator added a pointer to a no-op function to its .add_tip field,
things would still work correctly without the early-return in the
new helper function, no?

> Teach fetch-pack to allow this, and to take advantage of this by
> avoiding a ref and alternate traversal when it can. To do this, this
> patch combines all invocations of functions that invoke add_tip into one
> function, and that function first checks whether add_tip is NULL.
>
> Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
> ---
>  fetch-pack.c | 20 +++++++++++++-------
>  1 file changed, 13 insertions(+), 7 deletions(-)



>
> diff --git a/fetch-pack.c b/fetch-pack.c
> index 80fb3bd899..6c786f5970 100644
> --- a/fetch-pack.c
> +++ b/fetch-pack.c
> @@ -240,6 +240,16 @@ static void mark_tips(struct fetch_negotiator *negotiator,
>  	return;
>  }
>  
> +static void add_tips_and_alternates(struct fetch_negotiator *negotiator,
> +				    const struct oid_array *negotiation_tips)
> +{
> +	if (!negotiator->add_tip)
> +		return;
> +
> +	mark_tips(negotiator, negotiation_tips);
> +	for_each_cached_alternate(negotiator, insert_one_alternate_object);
> +}
> +
>  static int find_common(struct fetch_negotiator *negotiator,
>  		       struct fetch_pack_args *args,
>  		       int fd[2], struct object_id *result_oid,
> @@ -262,10 +272,8 @@ static int find_common(struct fetch_negotiator *negotiator,
>  			   PACKET_READ_CHOMP_NEWLINE |
>  			   PACKET_READ_DIE_ON_ERR_PACKET);
>  
> -	if (!args->no_dependents) {
> -		mark_tips(negotiator, args->negotiation_tips);
> -		for_each_cached_alternate(negotiator, insert_one_alternate_object);
> -	}
> +	if (!args->no_dependents)
> +		add_tips_and_alternates(negotiator, args->negotiation_tips);
>  
>  	fetching = 0;
>  	for ( ; refs ; refs = refs->next) {
> @@ -1575,9 +1583,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
>  				else
>  					state = FETCH_SEND_REQUEST;
>  
> -				mark_tips(negotiator, args->negotiation_tips);
> -				for_each_cached_alternate(negotiator,
> -							  insert_one_alternate_object);
> +				add_tips_and_alternates(negotiator, args->negotiation_tips);
>  			} else {
>  				filter_refs(args, &ref, sought, nr_sought);
>  				state = FETCH_SEND_REQUEST;
Jonathan Tan Aug. 7, 2020, 8:53 p.m. UTC | #2
> Jonathan Tan <jonathantanmy@google.com> writes:
> 
> > In a subsequent patch, a null fetch negotiator will be introduced. This
> > negotiator, among other things, will not need any tip information and
> > will have a NULL add_tip.
> 
> After reading the original code that the patch touches, I know what
> "a NULL add_tip" is, but it would have been nicer to readers if you
> said "callback" or "method" or "function".

OK.

> With or without the "if add_tip method is set to NULL, return
> without doing anything" optimization, it does make sense to refactor
> two existing places that do identical things.  
> 
> If all the current negotiators never have NULL in the .add_tip
> field, however, I'd suspect that it would make it easier to
> understand if this step is explained as refactoring duplicated code
> into a single helper _without_ the early-return for optimization,
> and then the step that introduces the negotiator that does want to
> lack add_tip method to add the early return.

This makes sense.

> After all, if the null
> negotiator added a pointer to a no-op function to its .add_tip field,
> things would still work correctly without the early-return in the
> new helper function, no?

Right now no, because in a partial clone, I also want to
avoid any iterations over refs since whenever refs are iterated, their
targets are checked for existence (see the call to
ref_resolves_to_object() in packed_ref_iterator_advance() in
packed-refs.c). However after looking at the code again, I see that
for_each_rawref() exists, which might do what I want (coupled with our
own way of deref-fing tags in fetch-pack). I'll take another look and if
this works, I'll just avoid the whole null method thing.
diff mbox series

Patch

diff --git a/fetch-pack.c b/fetch-pack.c
index 80fb3bd899..6c786f5970 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -240,6 +240,16 @@  static void mark_tips(struct fetch_negotiator *negotiator,
 	return;
 }
 
+static void add_tips_and_alternates(struct fetch_negotiator *negotiator,
+				    const struct oid_array *negotiation_tips)
+{
+	if (!negotiator->add_tip)
+		return;
+
+	mark_tips(negotiator, negotiation_tips);
+	for_each_cached_alternate(negotiator, insert_one_alternate_object);
+}
+
 static int find_common(struct fetch_negotiator *negotiator,
 		       struct fetch_pack_args *args,
 		       int fd[2], struct object_id *result_oid,
@@ -262,10 +272,8 @@  static int find_common(struct fetch_negotiator *negotiator,
 			   PACKET_READ_CHOMP_NEWLINE |
 			   PACKET_READ_DIE_ON_ERR_PACKET);
 
-	if (!args->no_dependents) {
-		mark_tips(negotiator, args->negotiation_tips);
-		for_each_cached_alternate(negotiator, insert_one_alternate_object);
-	}
+	if (!args->no_dependents)
+		add_tips_and_alternates(negotiator, args->negotiation_tips);
 
 	fetching = 0;
 	for ( ; refs ; refs = refs->next) {
@@ -1575,9 +1583,7 @@  static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 				else
 					state = FETCH_SEND_REQUEST;
 
-				mark_tips(negotiator, args->negotiation_tips);
-				for_each_cached_alternate(negotiator,
-							  insert_one_alternate_object);
+				add_tips_and_alternates(negotiator, args->negotiation_tips);
 			} else {
 				filter_refs(args, &ref, sought, nr_sought);
 				state = FETCH_SEND_REQUEST;