Skip to contentSkip to conversation

mini-refactor in rerere.c

PATCH In review Pierre Habouzit 10 participants 25 messages Jun 15, 2016 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

Here is a smallish series in builtin-rerere.c. Nothing very exciting
though.

#3 lore raw

Re: [PATCH 2/2] Make builtin-rerere use of strbuf nicer and more efficient.

In reply to a message not in the archive

Hi,

both patches appear to be obviously correct to me. (However, I was lazy enough not to compile and test, but I'd not expect any breakage there, given your previous patch serieses. ;-)

Ciao,
Dscho

#5 lore raw

Re: [PATCH 2/2] Make builtin-rerere use of strbuf nicer and more efficient.

Quoted 1 line · Junio C Hamano

Signoffs?

This is obviously a lapse on my end. You can add:

To any patch I send to this list.

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#6 lore raw

PATCH 1/1 Make strbuf_cmp inline, constify its arguments and optimize it a bit

In reply to a message not in the archive

It is definitely less code (also object code). It is not always measurably faster (but mostly is).

---
 strbuf.c |   12 ------------
 strbuf.h |    9 ++++++++-
 2 files changed, 8 insertions(+), 13 deletions(-)
strbuf.c +0 -12
index f4201e1..215837b 100644
@@ -58,18 +58,6 @@ void strbuf_rtrim(struct strbuf *sb)
 	sb->buf[sb->len] = '\0';
 }
 
-int strbuf_cmp(struct strbuf *a, struct strbuf *b)
-{
-	int cmp;
-	if (a->len < b->len) {
-		cmp = memcmp(a->buf, b->buf, a->len);
-		return cmp ? cmp : -1;
-	} else {
-		cmp = memcmp(a->buf, b->buf, b->len);
-		return cmp ? cmp : a->len != b->len;
-	}
-}
-
 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
 				   const void *data, size_t dlen)
 {
strbuf.h +8 -1
index 9b9e861..3116387 100644
@@ -78,7 +78,14 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
 
 /*----- content related -----*/
 extern void strbuf_rtrim(struct strbuf *);
-extern int strbuf_cmp(struct strbuf *, struct strbuf *);
+static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
+{
+	int len = a->len < b->len ? a->len: b->len;
+	int cmp = memcmp(a->buf, b->buf, len);
+	if (cmp)
+		return cmp;
+	return a->len < b->len ? -1: a->len != b->len;
+}
 
 /*----- add data in your buffer -----*/
 static inline void strbuf_addch(struct strbuf *sb, int c) {
Signature · 1 line
1.5.3.4.223.g78587
#7 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

strbuf.h static inline void strbuf_setlen(struct strbuf *sb, size_t len) { · line 88

Quoted 8 lines · Alex Riesen
strbuf.h +8 -0
+static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
+{
+	int len = a->len < b->len ? a->len: b->len;
+	int cmp = memcmp(a->buf, b->buf, len);
+	if (cmp)
+		return cmp;
+	return a->len < b->len ? -1: a->len != b->len;
+}

strbuf->buf is always non-NULL and NUL-terminated so you could just do

static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)

{
	int len = a->len < b->len ? a->len : b->len;
	return memcmp(a->buf, b->buf, len + 1);
}
#8 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

Quoted 13 lines · Timo Hirvonen

strbuf.h static inline void strbuf_setlen(struct strbuf *sb, size_t len) { · line 88

Quoted 8 lines · Alex Riesen
strbuf.h +8 -0
+static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
+{
+	int len = a->len < b->len ? a->len: b->len;
+	int cmp = memcmp(a->buf, b->buf, len);
+	if (cmp)
+		return cmp;
+	return a->len < b->len ? -1: a->len != b->len;
+}

strbuf->buf is always non-NULL and NUL-terminated so you could just do

Quoted 5 lines

static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)

{
	int len = a->len < b->len ? a->len : b->len;
	return memcmp(a->buf, b->buf, len + 1);
}

doesn't work, because a buffer can have (in some very specific cases)
an embeded NUL.

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#9 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

Quoted 10 lines · Pierre Habouzit
Quoted 7 lines

strbuf->buf is always non-NULL and NUL-terminated so you could just do

static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)

{
	int len = a->len < b->len ? a->len : b->len;
	return memcmp(a->buf, b->buf, len + 1);
}

doesn't work, because a buffer can have (in some very specific cases)
an embeded NUL.

Couldn't you then just do:

   int len = a->len < b->len ? a->len : b->len;
   int cmp = memcmp(a->buf, b->buf, len);
   if (cmp == 0)
      cmp = b->len - a->len;
   return cmp;

[In the case where one string is a prefix of the other, then the longer one is "greater".]

?

-Miles

Signature · 2 lines
"Suppose He doesn't give a shit?  Suppose there is a God but He
just doesn't give a shit?"  [George Carlin]
#10 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

Quoted 20 lines · Miles Bader
Quoted 10 lines · Pierre Habouzit
Quoted 7 lines

strbuf->buf is always non-NULL and NUL-terminated so you could just do

static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)

{
	int len = a->len < b->len ? a->len : b->len;
	return memcmp(a->buf, b->buf, len + 1);
}

doesn't work, because a buffer can have (in some very specific cases)
an embeded NUL.

Couldn't you then just do:

   int len = a->len < b->len ? a->len : b->len;
   int cmp = memcmp(a->buf, b->buf, len);
   if (cmp == 0)
      cmp = b->len - a->len;
   return cmp;
Quoted 4 lines

[In the case where one string is a prefix of the other, then the longer one is "greater".]

?

I fail to see where this variant is simpler than what we started the journey of simplification from.

The only change I consider worth checking from the whole series in this thread is making the function inline. All the rest pretty much was worse than what we started from in that it needed to reevaluate more conditions and turned out more complicated and obfuscate even to the human reader.

Signature · 1 line
David Kastrup, Kriemhildstr. 15, 44793 Bochum
#11 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

David Kastrup, Sun, Oct 07, 2007 18:07:17 +0200:

Quoted 25 lines
Quoted 23 lines · Miles Bader
Quoted 10 lines · Pierre Habouzit
Quoted 7 lines

strbuf->buf is always non-NULL and NUL-terminated so you could just do

static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)

{
	int len = a->len < b->len ? a->len : b->len;
	return memcmp(a->buf, b->buf, len + 1);
}

doesn't work, because a buffer can have (in some very specific cases)
an embeded NUL.

Couldn't you then just do:

   int len = a->len < b->len ? a->len : b->len;
   int cmp = memcmp(a->buf, b->buf, len);
   if (cmp == 0)
      cmp = b->len - a->len;
   return cmp;

[In the case where one string is a prefix of the other, then the longer one is "greater".]

Quoted 7 lines
Quoted 1 line

?

I fail to see where this variant is simpler than what we started the journey of simplification from.

The only change I consider worth checking from the whole series in this thread is making the function inline.

It also makes arguments const (which admittedly wont make it faster).

Quoted 4 lines

... All the rest pretty much
was worse than what we started from in that it needed to reevaluate more conditions and turned out more complicated and obfuscate even to the human reader.

it _is_ smaller. And it is _measurably_ faster on that thing I have at home (and old p4).

#12 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

Quoted 7 lines · Alex Riesen
Quoted 4 lines

... All the rest pretty much
was worse than what we started from in that it needed to reevaluate more conditions and turned out more complicated and obfuscate even to the human reader.

it _is_ smaller. And it is _measurably_ faster on that thing I have at home (and old p4).

Can we see the numbers and the steps used to obtain them? I'm also a little bit confused about how an inlined function can lead to a smaller executable... or did you just mean lines-of-code?

Cheers,
Wincent

#13 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

Wincent Colaiuta, Mon, Oct 08, 2007 00:12:17 +0200:

Quoted 7 lines · Alex Riesen
Quoted 6 lines
Quoted 5 lines

... All the rest pretty much
was worse than what we started from in that it needed to reevaluate more conditions and turned out more complicated and obfuscate even to the human reader.

Quoted 6 lines
Quoted 2 lines

it _is_ smaller. And it is _measurably_ faster on that thing I have at home (and old p4).

Can we see the numbers and the steps used to obtain them? I'm also a little bit confused about how an inlined function can lead to a smaller executable... or did you just mean lines-of-code?

I did mean the bytes of object code. I never said it produces a smaller executable.

I compiled with gcc -O2 and -O4, gcc 4.1.2 (Ubuntu 4.1.2-0ubuntu4). Cut the functions out into their own files and compile them to get the object code. Compile with -S (assembly) to examine the generated code. Compare.

#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>

struct strbuf {
	size_t alloc;
	size_t len;
	char *buf;
};

int strbuf_cmp2(struct strbuf *a, struct strbuf *b)

{
	int len = a->len < b->len ? a->len: b->len;
	int cmp = memcmp(a->buf, b->buf, len);
	if (cmp)
		return cmp;
	return a->len < b->len ? -1: a->len != b->len;
}

int strbuf_cmp1(struct strbuf *a, struct strbuf *b)

{
	int cmp;
	if (a->len < b->len) {
		cmp = memcmp(a->buf, b->buf, a->len);
		return cmp ? cmp : -1;
	} else {
		cmp = memcmp(a->buf, b->buf, b->len);
		return cmp ? cmp : a->len != b->len;
	}
}

int main(int argc, char *argv[], char *envp[])

{
	struct strbuf s1 = {
		.alloc = 0,
		.len = 50,
		.buf = "01234567890123456789012345678901234567890123456789",
	};
	struct strbuf s2 = {
		.alloc = 0,
		.len = 50,
		.buf = "0123456789012345678901234567890123456789",
	};
	struct strbuf s3 = {
		.alloc = 0,
		.len = 50,
		.buf = "0123456789012345678901234567890123456789012345678x",
	};
	struct timeval tv1, tv2, diff;
	unsigned n;
	int result;

#define CYCLES 0xffffffffu

	strbuf_cmp1(&s1, &s2);
	strbuf_cmp1(&s2, &s3);
	result = 0;
	gettimeofday(&tv1, NULL);
	for (n = CYCLES; n--; ) {
		result += strbuf_cmp1(&s1, &s2);
		result += strbuf_cmp1(&s2, &s3);
		result += strbuf_cmp1(&s1, &s3);
		result += strbuf_cmp1(&s1, &s1);
		result += n;
	}
	gettimeofday(&tv2, NULL);
	timersub(&tv2, &tv1, &diff);
	printf("ph=%ld.%ld (%d)\n", diff.tv_sec, diff.tv_usec, result);

	strbuf_cmp2(&s1, &s2);
	strbuf_cmp2(&s2, &s3);
	result = 0;
	gettimeofday(&tv1, NULL);
	for (n = CYCLES; n--; ) {
		result += strbuf_cmp2(&s1, &s2);
		result += strbuf_cmp2(&s2, &s3);
		result += strbuf_cmp2(&s1, &s3);
		result += strbuf_cmp2(&s1, &s1);
		result += n;
	}
	gettimeofday(&tv2, NULL);
	timersub(&tv2, &tv1, &diff);
	printf("ar=%ld.%ld (%d)\n", diff.tv_sec, diff.tv_usec, result);
	return 0;
}
#14 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

Quoted 8 lines · Alex Riesen

int strbuf_cmp2(struct strbuf *a, struct strbuf *b)

{
	int len = a->len < b->len ? a->len: b->len;
	int cmp = memcmp(a->buf, b->buf, len);
	if (cmp)
		return cmp;
	return a->len < b->len ? -1: a->len != b->len;
}

BTW, why are you making such effort to return only -1, 0, or 1 in the last line? memcmp/strcmp make no such guarantee; e.g. glibc says:

The strcmp' function compares the string S1 against S2, returning a value that has the same sign as the difference between the first differing pair of characters (interpreted as unsigned char' objects, then promoted to `int').

If the two strings are equal, strcmp' returns 0'.

A consequence of the ordering used by `strcmp' is that if S1 is an initial substring of S2, then S1 is considered to be "less than" S2.

So I think the last line can just be:

return a->len - b->len;

-miles

Signature · 1 line
Suburbia: where they tear out the trees and then name streets after them.
#18 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

In reply to Pierre Habouzit

Hi,

Quoted 4 lines · Pierre Habouzit
Quoted 2 lines · Timo Hirvonen

strbuf->buf is always non-NULL and NUL-terminated so you could just do

Quoted 8 lines
Quoted 5 lines

static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)

{
	int len = a->len < b->len ? a->len : b->len;
	return memcmp(a->buf, b->buf, len + 1);
}

doesn't work, because a buffer can have (in some very specific cases)
an embeded NUL.

But it should work. The function memcmp() could not care less if there is a NUL or not, it just compares until it finds a difference.

Ciao,
Dscho

#19 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

Quoted 14 lines · Johannes Schindelin

Hi,

Quoted 10 lines · Pierre Habouzit
Quoted 7 lines · Timo Hirvonen

strbuf->buf is always non-NULL and NUL-terminated so you could just do

static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)

{
	int len = a->len < b->len ? a->len : b->len;
	return memcmp(a->buf, b->buf, len + 1);
}
Quoted 5 lines
Quoted 2 lines

doesn't work, because a buffer can have (in some very specific cases)
an embeded NUL.

But it should work. The function memcmp() could not care less if there is a NUL or not, it just compares until it finds a difference.

not if your one of your strbuf has as prefix, the other followed by
'\0', then anything else (including nothing ;p).

Your test would yield equality.

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#20 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

In reply to Johannes Schindelin
Quoted 14 lines · Johannes Schindelin

Hi,

Quoted 10 lines · Pierre Habouzit
Quoted 7 lines · Timo Hirvonen

strbuf->buf is always non-NULL and NUL-terminated so you could just do

static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)

{
	int len = a->len < b->len ? a->len : b->len;
	return memcmp(a->buf, b->buf, len + 1);
}
Quoted 5 lines
Quoted 2 lines

doesn't work, because a buffer can have (in some very specific cases)
an embeded NUL.

But it should work. The function memcmp() could not care less if there is a NUL or not, it just compares until it finds a difference.

Almost. If a is "hello\0world" and b is "hello" then it would compare 6 characters from both and think the strings are equal.

#21 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

Hi,

Quoted 14 lines · Timo Hirvonen
Quoted 12 lines · Johannes Schindelin
Quoted 10 lines · Pierre Habouzit
Quoted 7 lines · Timo Hirvonen

strbuf->buf is always non-NULL and NUL-terminated so you could just do

static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)

{
	int len = a->len < b->len ? a->len : b->len;
	return memcmp(a->buf, b->buf, len + 1);
}
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

doesn't work, because a buffer can have (in some very specific cases)
an embeded NUL.

But it should work. The function memcmp() could not care less if there is a NUL or not, it just compares until it finds a difference.

Almost. If a is "hello\0world" and b is "hello" then it would compare 6 characters from both and think the strings are equal.

Good point.

Ciao,
Dscho

#22 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

In reply to Alex Riesen
Quoted 14 lines · Alex Riesen

It is definitely less code (also object code). It is not always measurably faster (but mostly is).

strbuf.c +0 -11
-int strbuf_cmp(struct strbuf *a, struct strbuf *b)
-{
-	int cmp;
-	if (a->len < b->len) {
-		cmp = memcmp(a->buf, b->buf, a->len);
-		return cmp ? cmp : -1;
-	} else {
-		cmp = memcmp(a->buf, b->buf, b->len);
-		return cmp ? cmp : a->len != b->len;
-	}
-}

strbuf.c void strbuf_rtrim(struct strbuf *sb)

Quoted 10 lines
strbuf.c +8 -1
-
 
+static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
+{
+	int len = a->len < b->len ? a->len: b->len;
+	int cmp = memcmp(a->buf, b->buf, len);
+	if (cmp)
+		return cmp;
+	return a->len < b->len ? -1: a->len != b->len;
+}

My guess is that you are conflating two issues about speed here: the inlining will like speed the stuff up. But having to evaluate the (a->len < b->len) comparison twice will likely slow it down.

So if you do any profiling, you should do it on both separate angles of this patch.

Signature · 1 line
David Kastrup, Kriemhildstr. 15, 44793 Bochum
#23 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

David Kastrup, Sun, Oct 07, 2007 16:24:57 +0200:

Quoted 18 lines
Quoted 15 lines · Alex Riesen

It is definitely less code (also object code). It is not always measurably faster (but mostly is).

strbuf.c +0 -12
-int strbuf_cmp(struct strbuf *a, struct strbuf *b)
-{
-	int cmp;
-	if (a->len < b->len) {
-		cmp = memcmp(a->buf, b->buf, a->len);
-		return cmp ? cmp : -1;
-	} else {
-		cmp = memcmp(a->buf, b->buf, b->len);
-		return cmp ? cmp : a->len != b->len;
-	}
-}
-
Quoted 12 lines

strbuf.h static inline void strbuf_setlen(struct strbuf *sb, size_t len) { · line 88

Quoted 8 lines
strbuf.h +8 -0
+static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
+{
+	int len = a->len < b->len ? a->len: b->len;
+	int cmp = memcmp(a->buf, b->buf, len);
+	if (cmp)
+		return cmp;
+	return a->len < b->len ? -1: a->len != b->len;
+}

My guess is that you are conflating two issues about speed here: the inlining will like speed the stuff up. But having to evaluate the (a->len < b->len) comparison twice will likely slow it down.

Can't the result of the expression be reused in compiled? Isn't it a common expression?

Quoted 3 lines

So if you do any profiling, you should do it on both separate angles of this patch.

I compared the inlined versions of both.

#24 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

Quoted 32 lines · Alex Riesen

David Kastrup, Sun, Oct 07, 2007 16:24:57 +0200:

Quoted 18 lines
Quoted 15 lines · Alex Riesen

It is definitely less code (also object code). It is not always measurably faster (but mostly is).

strbuf.c +0 -12
-int strbuf_cmp(struct strbuf *a, struct strbuf *b)
-{
-	int cmp;
-	if (a->len < b->len) {
-		cmp = memcmp(a->buf, b->buf, a->len);
-		return cmp ? cmp : -1;
-	} else {
-		cmp = memcmp(a->buf, b->buf, b->len);
-		return cmp ? cmp : a->len != b->len;
-	}
-}
-
Quoted 12 lines

strbuf.h static inline void strbuf_setlen(struct strbuf *sb, size_t len) { · line 88

Quoted 8 lines
strbuf.h +8 -0
+static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
+{
+	int len = a->len < b->len ? a->len: b->len;
+	int cmp = memcmp(a->buf, b->buf, len);
+	if (cmp)
+		return cmp;
+	return a->len < b->len ? -1: a->len != b->len;
+}

My guess is that you are conflating two issues about speed here: the inlining will like speed the stuff up. But having to evaluate the (a->len < b->len) comparison twice will likely slow it down.

Quoted 2 lines

Can't the result of the expression be reused in compiled? Isn't it a common expression?

No, since the call to memcmp might change a->len or b->len. A standard-compliant C compiler can't make assumptions about what memcmp might or might not touch unless both a and b can be shown to refer to variables with an address never passed out of the scope of the compilation unit.

Quoted 4 lines
Quoted 2 lines

So if you do any profiling, you should do it on both separate angles of this patch.

I compared the inlined versions of both.

Interesting.

Signature · 1 line
David Kastrup, Kriemhildstr. 15, 44793 Bochum
#25 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

David Kastrup, Sun, Oct 07, 2007 18:27:15 +0200:

Quoted 14 lines
Quoted 13 lines · Alex Riesen
Quoted 12 lines

strbuf.h static inline void strbuf_setlen(struct strbuf *sb, size_t len) { · line 88

Quoted 8 lines
strbuf.h +8 -0
+static inline int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
+{
+	int len = a->len < b->len ? a->len: b->len;
+	int cmp = memcmp(a->buf, b->buf, len);
+	if (cmp)
+		return cmp;
+	return a->len < b->len ? -1: a->len != b->len;
+}

My guess is that you are conflating two issues about speed here: the inlining will like speed the stuff up. But having to evaluate the (a->len < b->len) comparison twice will likely slow it down.

Quoted 4 lines
Quoted 2 lines

Can't the result of the expression be reused in compiled? Isn't it a common expression?

No, since the call to memcmp might change a->len or b->len. A

Huh?! How's that? It is not even given them!

#26 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

Quoted 6 lines · Alex Riesen
Quoted 4 lines
Quoted 2 lines

Can't the result of the expression be reused in compiled? Isn't it a common expression?

No, since the call to memcmp might change a->len or b->len. A

Huh?! How's that? It is not even given them!

But they are non-local variables (they are part of structs passed in as pointers), so that translation unit has no idea how they are allocated. They could be globals that memcmp mucks with as a side effect.

That being said, standards-conforming compilers _can_ realize that memcmp is a special, standards-defined function with no side effects and act accordingly. gcc provides the 'pure' function attribute for this purpose, which is used by glibc.

-Peff

#15 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

In reply to Miles Bader
Quoted 20 lines · Miles Bader
Quoted 8 lines · Alex Riesen

int strbuf_cmp2(struct strbuf *a, struct strbuf *b)

{
	int len = a->len < b->len ? a->len: b->len;
	int cmp = memcmp(a->buf, b->buf, len);
	if (cmp)
		return cmp;
	return a->len < b->len ? -1: a->len != b->len;
}

BTW, why are you making such effort to return only -1, 0, or 1 in the last line? memcmp/strcmp make no such guarantee; e.g. glibc says:

The strcmp' function compares the string S1 against S2, returning a value that has the same sign as the difference between the first differing pair of characters (interpreted as unsigned char' objects, then promoted to `int').

If the two strings are equal, strcmp' returns 0'.

Quoted 7 lines

A consequence of the ordering used by `strcmp' is that if S1 is an initial substring of S2, then S1 is considered to be "less than" S2.

So I think the last line can just be:

   return a->len - b->len;

Won't work because ->len are size_t and return value is int, so on 64
bits platform, this has chances to overflow.

FWIW I believe we are doing micro-benchs in a function that is used in
2 places in git right now.

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#16 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

* Pierre Habouzit:

Quoted 6 lines
Quoted 3 lines

So I think the last line can just be:

   return a->len - b->len;

Won't work because ->len are size_t and return value is int, so on 64
bits platform, this has chances to overflow.

Nit: It can overflow on 32-bit, too.

And "int len" in the first line of the function body should be "size_t len".

Moving that to a compare_int/compare_size_t function should help; AFAIK there's no short idiom which does the job.

Signature · 4 lines
Florian Weimer                <[email protected]>
BFK edv-consulting GmbH       http://www.bfk.de/
Kriegsstraße 100              tel: +49-721-96201-1
D-76133 Karlsruhe             fax: +49-721-96201-99
#17 lore raw

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

Florian Weimer, Mon, Oct 08, 2007 10:54:32 +0200:

Quoted 2 lines

And "int len" in the first line of the function body should be "size_t len".

right. Missed that.