threads / patch / 45414

patchreceive-pack: simplify run_update_post_hook()

Subject: [PATCH] receive-pack: simplify run_update_post_hook()

## tl;dr

6 messages between Mar 17, 2017 and Mar 20, 2017. Diffs are folded; open one to read it.

replies: 5people: 4as markdown or json

René Scharfe· Mar 17, 2017, 22:02 UTC · lore

Instead of counting the arguments to see if there are any and then building the full command use a single loop and add the hook command just before the first argument. This reduces duplication and overall code size.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 builtin/receive-pack.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
Show changes to builtin/receive-pack.c +5 −8
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 83492af05f..fb2a090a0c 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1128,25 +1128,22 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 static void run_update_post_hook(struct command *commands)
 {
 	struct command *cmd;
-	int argc;
 	struct child_process proc = CHILD_PROCESS_INIT;
 	const char *hook;
 
 	hook = find_hook("post-update");
-	for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
-		if (cmd->error_string || cmd->did_not_exist)
-			continue;
-		argc++;
-	}
-	if (!argc || !hook)
+	if (!hook)
 		return;
 
-	argv_array_push(&proc.args, hook);
 	for (cmd = commands; cmd; cmd = cmd->next) {
 		if (cmd->error_string || cmd->did_not_exist)
 			continue;
+		if (!proc.args.argc)
+			argv_array_push(&proc.args, hook);
 		argv_array_push(&proc.args, cmd->ref_name);
 	}
+	if (!proc.args.argc)
+		return;
 
 	proc.no_stdin = 1;
 	proc.stdout_to_stderr = 1;
-- 
2.12.0
Jeff King· Mar 17, 2017, 22:23 UTC · re: René Scharfe · lore

Re: [PATCH] receive-pack: simplify run_update_post_hook()

On Fri, Mar 17, 2017 at 11:02:13PM +0100, René Scharfe wrote:
> Instead of counting the arguments to see if there are any and then
> building the full command use a single loop and add the hook command
> just before the first argument.  This reduces duplication and overall
> code size.
Yeah, I agree one loop is nicer.
Show 10 quoted lines
> -	argv_array_push(&proc.args, hook);
>  	for (cmd = commands; cmd; cmd = cmd->next) {
>  		if (cmd->error_string || cmd->did_not_exist)
>  			continue;
> +		if (!proc.args.argc)
> +			argv_array_push(&proc.args, hook);
>  		argv_array_push(&proc.args, cmd->ref_name);
>  	}
> +	if (!proc.args.argc)
> +		return;

It looks at first like the result leaks, because you have to realize that the push will modify proc.args.argc. I wonder if:

  argv_array_push(&proc.args, hook);
  for (cmd = commands; cmd; cmd = cmd->next) {
	if (!cmd->error_string && !cmd->did_not_exist)
		argv_array_push(&proc.args, cmd->ref_name);
  }
  if (proc.args.argc == 1) {
	argv_array_clear(&proc.args);
	return;
  }

would be more obvious (at the cost of a pointless malloc in the corner case. I can live with it either way.

-Peff
René Scharfe· Mar 17, 2017, 23:21 UTC · re: Jeff King · lore

Re: [PATCH] receive-pack: simplify run_update_post_hook()

Am 17.03.2017 um 23:23 schrieb Jeff King:
Show 36 quoted lines
> On Fri, Mar 17, 2017 at 11:02:13PM +0100, René Scharfe wrote:
>
>> Instead of counting the arguments to see if there are any and then
>> building the full command use a single loop and add the hook command
>> just before the first argument.  This reduces duplication and overall
>> code size.
>
> Yeah, I agree one loop is nicer.
>
>> -	argv_array_push(&proc.args, hook);
>>  	for (cmd = commands; cmd; cmd = cmd->next) {
>>  		if (cmd->error_string || cmd->did_not_exist)
>>  			continue;
>> +		if (!proc.args.argc)
>> +			argv_array_push(&proc.args, hook);
>>  		argv_array_push(&proc.args, cmd->ref_name);
>>  	}
>> +	if (!proc.args.argc)
>> +		return;
>
> It looks at first like the result leaks, because you have to realize
> that the push will modify proc.args.argc. I wonder if:
>
>   argv_array_push(&proc.args, hook);
>   for (cmd = commands; cmd; cmd = cmd->next) {
> 	if (!cmd->error_string && !cmd->did_not_exist)
> 		argv_array_push(&proc.args, cmd->ref_name);
>   }
>
>   if (proc.args.argc == 1) {
> 	argv_array_clear(&proc.args);
> 	return;
>   }
>
> would be more obvious (at the cost of a pointless malloc in the corner
> case. I can live with it either way.

Sure, that's even simpler. I don't know how often the no-args branch would be taken and if the extra allocation would even matter -- that's why I tried to avoid it -- but probably the answers are not often and not much. The only test case that hits it is for the deletion of a non-existent ref.

René
Junio C Hamano· Mar 18, 2017, 17:12 UTC · re: Jeff King · lore

Re: [PATCH] receive-pack: simplify run_update_post_hook()

Jeff King <peff@peff.net> writes:
Show 22 quoted lines
> On Fri, Mar 17, 2017 at 11:02:13PM +0100, René Scharfe wrote:
>
>> Instead of counting the arguments to see if there are any and then
>> building the full command use a single loop and add the hook command
>> just before the first argument.  This reduces duplication and overall
>> code size.
>
> Yeah, I agree one loop is nicer.
>
>> -	argv_array_push(&proc.args, hook);
>>  	for (cmd = commands; cmd; cmd = cmd->next) {
>>  		if (cmd->error_string || cmd->did_not_exist)
>>  			continue;
>> +		if (!proc.args.argc)
>> +			argv_array_push(&proc.args, hook);
>>  		argv_array_push(&proc.args, cmd->ref_name);
>>  	}
>> +	if (!proc.args.argc)
>> +		return;
>
> It looks at first like the result leaks, because you have to realize
> that the push will modify proc.args.argc. 

Hmph, I needed to read the original twice to imagine how a paranoid person can fear leaks. The return condition says "if args array is empty, just return" and the thing being empty is an enough indication to think nothing is leaking, at least for me.

Having said that, I'd admit that the "always push hook and then clean up before returning if it turns out there is nothing to call the hook for" is what I would have wrote if I were doing this, but I'm inclined to think that is not because I would have thought of both versions and picked the better one, but because I wouldn't have noticed the "optimization opportunity" René spotted here (not that I think an extra alloc would matter).

I'll queue the patch as-is, at least for now.
Thanks.
Show 17 quoted lines
> I wonder if:
>
>   argv_array_push(&proc.args, hook);
>   for (cmd = commands; cmd; cmd = cmd->next) {
> 	if (!cmd->error_string && !cmd->did_not_exist)
> 		argv_array_push(&proc.args, cmd->ref_name);
>   }
>
>   if (proc.args.argc == 1) {
> 	argv_array_clear(&proc.args);
> 	return;
>   }
>
> would be more obvious (at the cost of a pointless malloc in the corner
> case. I can live with it either way.
>
> -Peff
Jeff King· Mar 20, 2017, 03:35 UTC · re: Junio C Hamano · lore

Re: [PATCH] receive-pack: simplify run_update_post_hook()

On Sat, Mar 18, 2017 at 10:12:07AM -0700, Junio C Hamano wrote:
Show 18 quoted lines
> >> -	argv_array_push(&proc.args, hook);
> >>  	for (cmd = commands; cmd; cmd = cmd->next) {
> >>  		if (cmd->error_string || cmd->did_not_exist)
> >>  			continue;
> >> +		if (!proc.args.argc)
> >> +			argv_array_push(&proc.args, hook);
> >>  		argv_array_push(&proc.args, cmd->ref_name);
> >>  	}
> >> +	if (!proc.args.argc)
> >> +		return;
> >
> > It looks at first like the result leaks, because you have to realize
> > that the push will modify proc.args.argc. 
> 
> Hmph, I needed to read the original twice to imagine how a paranoid
> person can fear leaks.  The return condition says "if args array is
> empty, just return" and the thing being empty is an enough indication
> to think nothing is leaking, at least for me.
Yeah, I think I just read it funny the first time. I'm OK with it as-is.
-Peff
Jonathan Nieder· Mar 17, 2017, 22:50 UTC · re: René Scharfe · lore

Re: [PATCH] receive-pack: simplify run_update_post_hook()

René Scharfe wrote:
Show 9 quoted lines
> Instead of counting the arguments to see if there are any and then
> building the full command use a single loop and add the hook command
> just before the first argument.  This reduces duplication and overall
> code size.
>
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
>  builtin/receive-pack.c | 13 +++++--------
>  1 file changed, 5 insertions(+), 8 deletions(-)
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

← back to recent threads