git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/2] Remove useless temporary integer in builtin/push.c

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 2, 2010, 18:51 UTC
Message-ID
<7vocdklueo.fsf@alter.siamese.dyndns.org>
In-Reply-To
<70ee84752cb7db08c65c608a12ed321dd2c26830.1280580026.git.jaredhance@gmail.com>
Jared Hance <jaredhance@gmail.com> writes:
> Creating a variable nr here to use throughout the function only to change
> refspec_nr to nr at the end, having not used refspec_nr the entire time,
> is rather pointless. Instead, simply increment refspec_nr.

That is something a compiler can notice and optimize out, so it byitself is not a good criteria to judge this change. The real issue is if the use of temporary makes the code easier to read or harder.

With the two patches squashed together to use ALLOC_GROW(), the result conforms to the pattern many codepaths use, and that makes it easier to read.

Will queue, with these two squashed into one commit.
Thanks.
Show 30 quoted lines
> Signed-off-by: Jared Hance <jaredhance@gmail.com>
> ---
>  builtin/push.c |    7 +++----
>  1 files changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/builtin/push.c b/builtin/push.c
> index f4358b9..79d8192 100644
> --- a/builtin/push.c
> +++ b/builtin/push.c
> @@ -25,10 +25,9 @@ static int refspec_nr;
>  
>  static void add_refspec(const char *ref)
>  {
> -	int nr = refspec_nr + 1;
> -	refspec = xrealloc(refspec, nr * sizeof(char *));
> -	refspec[nr-1] = ref;
> -	refspec_nr = nr;
> +	refspec_nr++;
> +	refspec = xrealloc(refspec, refspec_nr * sizeof(char *));
> +	refspec[refspec_nr-1] = ref;
>  }
>  
>  static void set_refspecs(const char **refs, int nr)
> -- 
> 1.7.2
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous: Jared HanceNext: Jared Hance
Message 5 of 6 in “Remove useless temporary integer in builtin/push.c”
  1. Remove useless temporary integer in builtin/push.cJared Hance, Jul 29, 2010
  2. Thomas RastJul 29, 2010
  3. 0/2 Clean up add_refspec in builtin/push.cJared Hance, Jul 31, 2010
  4. 1/2 Remove useless temporary integer in builtin/push.cJared Hance, Jul 31, 2010
  5. Junio C HamanoAug 2, 2010
  6. 2/2 Use ALLOC_GROW in builtin/push.cJared Hance, Jul 31, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.