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

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

From
Alex Riesen <raa.lkml@gmail.com>
Date
Oct 7, 2007, 22:31 UTC
Message-ID
<20071007223140.GG2765@steel.home>
In-Reply-To
<EF81F7DD-73C7-4B6F-92D2-4A143CA05365@wincent.com>
Wincent Colaiuta, Mon, Oct 08, 2007 00:12:17 +0200:
Show 13 quoted lines
> El 7/10/2007, a las 23:54, Alex Riesen escribió:
> 
> >>... 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?

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;
}
Previous: Wincent ColaiutaNext: Miles Bader
Message 12 of 25 in “mini-refactor in rerere.c”
  1. Pierre HabouzitSep 24, 2007
  2. Johannes SchindelinSep 24, 2007
  3. Junio C HamanoSep 26, 2007
  4. Pierre HabouzitSep 26, 2007
  5. Make strbuf_cmp inline, constify its arguments and optimize it a bitAlex Riesen, Oct 7, 2007
  6. Timo HirvonenOct 7, 2007
  7. Pierre HabouzitOct 7, 2007
  8. Miles BaderOct 7, 2007
  9. David KastrupOct 7, 2007
  10. Alex RiesenOct 7, 2007
  11. Wincent ColaiutaOct 7, 2007
  12. Alex RiesenOct 7, 2007
  13. Miles BaderOct 8, 2007
  14. Pierre HabouzitOct 8, 2007
  15. Florian WeimerOct 8, 2007
  16. Alex RiesenOct 8, 2007
  17. Johannes SchindelinOct 7, 2007
  18. Timo HirvonenOct 7, 2007
  19. Johannes SchindelinOct 7, 2007
  20. Pierre HabouzitOct 7, 2007
  21. David KastrupOct 7, 2007
  22. Alex RiesenOct 7, 2007
  23. David KastrupOct 7, 2007
  24. Alex RiesenOct 7, 2007
  25. Jeff KingOct 8, 2007

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.