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

Re: [PATCH] Microoptimize strbuf_cmp

From
Alex Riesen <raa.lkml@gmail.com>
Date
Mar 19, 2009, 22:27 UTC
Message-ID
<20090319222732.GB8433@blimp.localdomain>
In-Reply-To
<7vvdq56ukb.fsf@gitster.siamese.dyndns.org>

It can be less object code and may be even faster, even if at the moment there is no callers to take an advantage of that. This implementation can be trivially made inlinable later.

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
Junio C Hamano, Thu, Mar 19, 2009 23:01:40 +0100:
Show 12 quoted lines
> Alex Riesen <raa.lkml@gmail.com> writes:
> 
> > Make it inline and cleanup a bit. It is definitely less code
> > including object code, but it is not always measurably faster
> > (but mostly is).
> 
> The only in-tree user seems to be rerere, so inlining for that single
> caller will reduce the object side, but I am not sure if this is a good
> change in the longer term if we want to encourage the use of strbuf
> library.
> 
> The rewrite of the logic does seem worth doing, though.

But then it is only a half of the micro-optimization. In this case, the cost of call to the function's code is comparable with the change of the code.

Anyway, FWIW.
 strbuf.c |   13 +++++--------
 1 files changed, 5 insertions(+), 8 deletions(-)
diff --git a/strbuf.c b/strbuf.c
index 6ed0684..bfbd816 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -139,14 +139,11 @@ void strbuf_list_free(struct strbuf **sbs)
 
 int strbuf_cmp(const struct strbuf *a, const 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 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;
 }
 
 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
-- 
1.6.2.1.237.g7206c6
Previous: Junio C Hamano
Message 3 of 3 in “Microoptimize strbuf_cmp”
  1. Microoptimize strbuf_cmpAlex Riesen, Mar 19, 2009
  2. Junio C HamanoMar 19, 2009
  3. Alex RiesenMar 19, 2009

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.