# [PATCH] Remove useless temporary integer in builtin/push.c

6 messages from 2010-07-29 to 2010-08-02. Participants: Jared Hance, Thomas Rast, Junio C Hamano.
Thread: https://gitlist.dev/t/24574

## Jared Hance, 2010-07-29 15:59

Subject: [PATCH] Remove useless temporary integer in builtin/push.c
Message-ID: <70ee84752cb7db08c65c608a12ed321dd2c26830.1280419073.git.jaredhance@gmail.com>
URL: https://gitlist.dev/e/70ee84752cb7db08c65c608a12ed321dd2c26830.1280419073.git.jaredhance%40gmail.com

```
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.

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

```

## Thomas Rast, 2010-07-29 22:21

Subject: Re: [PATCH] Remove useless temporary integer in builtin/push.c
Message-ID: <201007300021.34061.trast@student.ethz.ch>
URL: https://gitlist.dev/e/201007300021.34061.trast%40student.ethz.ch
In-Reply-To: <70ee84752cb7db08c65c608a12ed321dd2c26830.1280419073.git.jaredhance@gmail.com>

```
Jared Hance wrote:
> 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.
> 
> Signed-off-by: Jared Hance <jaredhance@gmail.com>
[...]
> -	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;

While you're already here, you could switch to ALLOC_GROW instead to
avoid the n**2 behaviour of xrealloc...

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

```

## Jared Hance, 2010-07-31 12:54

Subject: [PATCH 0/2] Clean up add_refspec in builtin/push.c
Message-ID: <cover.1280580026.git.jaredhance@gmail.com>
URL: https://gitlist.dev/e/cover.1280580026.git.jaredhance%40gmail.com
In-Reply-To: <201007300021.34061.trast@student.ethz.ch>

```
This patch series removes code duplication and other poor coding
practices from builtin/push.c.

Jared Hance (2):
  Remove useless temporary integer in builtin/push.c
  Use ALLOC_GROW in builtin/push.c

 builtin/push.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

-- 
1.7.2

```

## Jared Hance, 2010-07-31 12:54

Subject: [PATCH 1/2] Remove useless temporary integer in builtin/push.c
Message-ID: <70ee84752cb7db08c65c608a12ed321dd2c26830.1280580026.git.jaredhance@gmail.com>
URL: https://gitlist.dev/e/70ee84752cb7db08c65c608a12ed321dd2c26830.1280580026.git.jaredhance%40gmail.com
In-Reply-To: <cover.1280580026.git.jaredhance@gmail.com>

```
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.

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

```

## Jared Hance, 2010-07-31 12:57

Subject: [PATCH 2/2] Use ALLOC_GROW in builtin/push.c
Message-ID: <fbf6f49fa7c1d6b265d6cf6cfa772ec133550389.1280580026.git.jaredhance@gmail.com>
URL: https://gitlist.dev/e/fbf6f49fa7c1d6b265d6cf6cfa772ec133550389.1280580026.git.jaredhance%40gmail.com
In-Reply-To: <cover.1280580026.git.jaredhance@gmail.com>

```
The current implementation of add_refspec(const char *ref) duplicates
functionality found in the xalloc api. Use ALLOC_GROW instead to prevent
code duplication.

Signed-off-by: Jared Hance <jaredhance@gmail.com>
---
 builtin/push.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/builtin/push.c b/builtin/push.c
index 79d8192..0da0ec8 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -22,11 +22,12 @@ static int progress;
 
 static const char **refspec;
 static int refspec_nr;
+static size_t refspec_alloc;
 
 static void add_refspec(const char *ref)
 {
 	refspec_nr++;
-	refspec = xrealloc(refspec, refspec_nr * sizeof(char *));
+	ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
 	refspec[refspec_nr-1] = ref;
 }
 
-- 
1.7.2

```

## Junio C Hamano, 2010-08-02 18:51

Subject: Re: [PATCH 1/2] Remove useless temporary integer in builtin/push.c
Message-ID: <7vocdklueo.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vocdklueo.fsf%40alter.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.

> 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

```
