Skip to contentSkip to conversation

Remove useless temporary integer in builtin/push.c

PATCH In review Jared Hance 3 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Remove useless temporary integer in builtin/push.c

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.

---
 builtin/push.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
builtin/push.c +3 -4
index f4358b9..79d8192 100644
@@ -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)
Signature · 1 line
1.7.2
#1 lore raw
Quoted 5 lines · Jared Hance

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.

[...]

builtin/push.c static int refspec_nr; · line 30

Quoted 7 lines
builtin/push.c +3 -4
-	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...

Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch
#2 lore raw

cover Clean up add_refspec in builtin/push.c

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(-)
Signature · 1 line
1.7.2
#3 lore raw

PATCH 1/2 Remove useless temporary integer in builtin/push.c

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.

---
 builtin/push.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
builtin/push.c +3 -4
index f4358b9..79d8192 100644
@@ -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)
Signature · 1 line
1.7.2
#4 lore raw
Quoted 3 lines · Jared Hance

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.

Quoted 30 lines
---
 builtin/push.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
builtin/push.c +3 -4
index f4358b9..79d8192 100644
@@ -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)
Signature · 6 lines
1.7.2

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#5 lore raw

PATCH 2/2 Use ALLOC_GROW in builtin/push.c

In reply to Jared Hance

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

---
 builtin/push.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
builtin/push.c +2 -1
index 79d8192..0da0ec8 100644
@@ -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;
 }
 
Signature · 1 line
1.7.2