git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Remove useless temporary integer in builtin/push.c
@ 2010-07-29 15:59 Jared Hance
  2010-07-29 22:21 ` Thomas Rast
  0 siblings, 1 reply; 6+ messages in thread
From: Jared Hance @ 2010-07-29 15:59 UTC (permalink / raw)
  To: git; +Cc: gitster

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

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

* Re: [PATCH] Remove useless temporary integer in builtin/push.c
  2010-07-29 15:59 [PATCH] Remove useless temporary integer in builtin/push.c Jared Hance
@ 2010-07-29 22:21 ` Thomas Rast
  2010-07-31 12:54   ` [PATCH 0/2] Clean up add_refspec " Jared Hance
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas Rast @ 2010-07-29 22:21 UTC (permalink / raw)
  To: Jared Hance; +Cc: git, gitster

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

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

* [PATCH 0/2] Clean up add_refspec in builtin/push.c
  2010-07-29 22:21 ` Thomas Rast
@ 2010-07-31 12:54   ` Jared Hance
  2010-07-31 12:54     ` [PATCH 1/2] Remove useless temporary integer " Jared Hance
  2010-07-31 12:57     ` [PATCH 2/2] Use ALLOC_GROW " Jared Hance
  0 siblings, 2 replies; 6+ messages in thread
From: Jared Hance @ 2010-07-31 12:54 UTC (permalink / raw)
  To: git; +Cc: gitster

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

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

* [PATCH 1/2] Remove useless temporary integer in builtin/push.c
  2010-07-31 12:54   ` [PATCH 0/2] Clean up add_refspec " Jared Hance
@ 2010-07-31 12:54     ` Jared Hance
  2010-08-02 18:51       ` Junio C Hamano
  2010-07-31 12:57     ` [PATCH 2/2] Use ALLOC_GROW " Jared Hance
  1 sibling, 1 reply; 6+ messages in thread
From: Jared Hance @ 2010-07-31 12:54 UTC (permalink / raw)
  To: git; +Cc: gitster

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

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

* [PATCH 2/2] Use ALLOC_GROW in builtin/push.c
  2010-07-31 12:54   ` [PATCH 0/2] Clean up add_refspec " Jared Hance
  2010-07-31 12:54     ` [PATCH 1/2] Remove useless temporary integer " Jared Hance
@ 2010-07-31 12:57     ` Jared Hance
  1 sibling, 0 replies; 6+ messages in thread
From: Jared Hance @ 2010-07-31 12:57 UTC (permalink / raw)
  To: git; +Cc: gitster

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

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

* Re: [PATCH 1/2] Remove useless temporary integer in builtin/push.c
  2010-07-31 12:54     ` [PATCH 1/2] Remove useless temporary integer " Jared Hance
@ 2010-08-02 18:51       ` Junio C Hamano
  0 siblings, 0 replies; 6+ messages in thread
From: Junio C Hamano @ 2010-08-02 18:51 UTC (permalink / raw)
  To: Jared Hance; +Cc: git

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

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

end of thread, other threads:[~2010-08-02 18:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-07-29 15:59 [PATCH] Remove useless temporary integer in builtin/push.c Jared Hance
2010-07-29 22:21 ` Thomas Rast
2010-07-31 12:54   ` [PATCH 0/2] Clean up add_refspec " Jared Hance
2010-07-31 12:54     ` [PATCH 1/2] Remove useless temporary integer " Jared Hance
2010-08-02 18:51       ` Junio C Hamano
2010-07-31 12:57     ` [PATCH 2/2] Use ALLOC_GROW " Jared Hance

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).