All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Microoptimize strbuf_cmp
@ 2009-03-19 21:09 Alex Riesen
  2009-03-19 22:01 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Riesen @ 2009-03-19 21:09 UTC (permalink / raw)
  To: git; +Cc: Pierre Habouzit, Junio C Hamano

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

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---

I have this patch for a long time in my tree and vaguely remember
sending it once.

 strbuf.c |   12 ------------
 strbuf.h |    9 ++++++++-
 2 files changed, 8 insertions(+), 13 deletions(-)

diff --git a/strbuf.c b/strbuf.c
index 6ed0684..e1f4db7 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -137,18 +137,6 @@ void strbuf_list_free(struct strbuf **sbs)
 	free(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;
-	}
-}
-
 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
 				   const void *data, size_t dlen)
 {
diff --git a/strbuf.h b/strbuf.h
index 89bd36e..df95960 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -80,11 +80,18 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
 extern void strbuf_trim(struct strbuf *);
 extern void strbuf_rtrim(struct strbuf *);
 extern void strbuf_ltrim(struct strbuf *);
-extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
 extern void strbuf_tolower(struct strbuf *);
 
 extern struct strbuf **strbuf_split(const struct strbuf *, int delim);
 extern void strbuf_list_free(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) {
-- 
1.6.2.1.250.ga1458

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

* Re: [PATCH] Microoptimize strbuf_cmp
  2009-03-19 21:09 [PATCH] Microoptimize strbuf_cmp Alex Riesen
@ 2009-03-19 22:01 ` Junio C Hamano
  2009-03-19 22:27   ` Alex Riesen
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2009-03-19 22:01 UTC (permalink / raw)
  To: Alex Riesen; +Cc: git, Pierre Habouzit

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.

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

* Re: [PATCH] Microoptimize strbuf_cmp
  2009-03-19 22:01 ` Junio C Hamano
@ 2009-03-19 22:27   ` Alex Riesen
  0 siblings, 0 replies; 3+ messages in thread
From: Alex Riesen @ 2009-03-19 22:27 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Pierre Habouzit

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:
> 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

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

end of thread, other threads:[~2009-03-19 22:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-19 21:09 [PATCH] Microoptimize strbuf_cmp Alex Riesen
2009-03-19 22:01 ` Junio C Hamano
2009-03-19 22:27   ` Alex Riesen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.