git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Simplify crud() in ident.c
@ 2007-12-03 19:11 Alex Riesen
  2007-12-03 20:19 ` Jakub Narebski
  2007-12-03 20:47 ` Johannes Schindelin
  0 siblings, 2 replies; 8+ messages in thread
From: Alex Riesen @ 2007-12-03 19:11 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano

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

Noticed it accidentally.

 ident.c |   28 +++++++++-------------------
 1 files changed, 9 insertions(+), 19 deletions(-)

diff --git a/ident.c b/ident.c
index 9b2a852..dbd0f52 100644
--- a/ident.c
+++ b/ident.c
@@ -113,25 +113,15 @@ static int add_raw(char *buf, size_t size, int offset, const char *str)
 
 static int crud(unsigned char c)
 {
-	static char crud_array[256];
-	static int crud_array_initialized = 0;
-
-	if (!crud_array_initialized) {
-		int k;
-
-		for (k = 0; k <= 31; ++k) crud_array[k] = 1;
-		crud_array[' '] = 1;
-		crud_array['.'] = 1;
-		crud_array[','] = 1;
-		crud_array[':'] = 1;
-		crud_array[';'] = 1;
-		crud_array['<'] = 1;
-		crud_array['>'] = 1;
-		crud_array['"'] = 1;
-		crud_array['\''] = 1;
-		crud_array_initialized = 1;
-	}
-	return crud_array[c];
+	return  c <= 32  ||
+		c == '.' ||
+		c == ',' ||
+		c == ':' ||
+		c == ';' ||
+		c == '<' ||
+		c == '>' ||
+		c == '"' ||
+		c == '\'';
 }
 
 /*
-- 
1.5.3.6.1022.g35305

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

* Re: [PATCH] Simplify crud() in ident.c
  2007-12-03 19:11 [PATCH] Simplify crud() in ident.c Alex Riesen
@ 2007-12-03 20:19 ` Jakub Narebski
  2007-12-03 21:32   ` Alex Riesen
  2007-12-03 22:55   ` Luke Lu
  2007-12-03 20:47 ` Johannes Schindelin
  1 sibling, 2 replies; 8+ messages in thread
From: Jakub Narebski @ 2007-12-03 20:19 UTC (permalink / raw)
  To: Alex Riesen; +Cc: git, Junio C Hamano

Perhaps simplier, but isn't it slower?

-- 
Jakub Narebski

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

* Re: [PATCH] Simplify crud() in ident.c
  2007-12-03 19:11 [PATCH] Simplify crud() in ident.c Alex Riesen
  2007-12-03 20:19 ` Jakub Narebski
@ 2007-12-03 20:47 ` Johannes Schindelin
  2007-12-03 21:37   ` Alex Riesen
  1 sibling, 1 reply; 8+ messages in thread
From: Johannes Schindelin @ 2007-12-03 20:47 UTC (permalink / raw)
  To: Alex Riesen; +Cc: git, Junio C Hamano

Hi,

On Mon, 3 Dec 2007, Alex Riesen wrote:

> diff --git a/ident.c b/ident.c
> index 9b2a852..dbd0f52 100644
> --- a/ident.c
> +++ b/ident.c
> @@ -113,25 +113,15 @@ static int add_raw(char *buf, size_t size, int offset, const char *str)
>  
>  static int crud(unsigned char c)
>  {
> -	static char crud_array[256];
> -	static int crud_array_initialized = 0;
> -
> -	if (!crud_array_initialized) {
> -		int k;
> -
> -		for (k = 0; k <= 31; ++k) crud_array[k] = 1;
> -		crud_array[' '] = 1;
> -		crud_array['.'] = 1;
> -		crud_array[','] = 1;
> -		crud_array[':'] = 1;
> -		crud_array[';'] = 1;
> -		crud_array['<'] = 1;
> -		crud_array['>'] = 1;
> -		crud_array['"'] = 1;
> -		crud_array['\''] = 1;
> -		crud_array_initialized = 1;
> -	}
> -	return crud_array[c];
> +	return  c <= 32  ||
> +		c == '.' ||
> +		c == ',' ||
> +		c == ':' ||
> +		c == ';' ||
> +		c == '<' ||
> +		c == '>' ||
> +		c == '"' ||
> +		c == '\'';

Or enhance ctype.c.

Ciao,
Dscho

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

* Re: [PATCH] Simplify crud() in ident.c
  2007-12-03 20:19 ` Jakub Narebski
@ 2007-12-03 21:32   ` Alex Riesen
  2007-12-03 22:55   ` Luke Lu
  1 sibling, 0 replies; 8+ messages in thread
From: Alex Riesen @ 2007-12-03 21:32 UTC (permalink / raw)
  To: Jakub Narebski; +Cc: git, Junio C Hamano

Jakub Narebski, Mon, Dec 03, 2007 21:19:29 +0100:
> Perhaps simplier, but isn't it slower?

doubt it

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

* Re: [PATCH] Simplify crud() in ident.c
  2007-12-03 20:47 ` Johannes Schindelin
@ 2007-12-03 21:37   ` Alex Riesen
  2007-12-03 22:48     ` Johannes Schindelin
  0 siblings, 1 reply; 8+ messages in thread
From: Alex Riesen @ 2007-12-03 21:37 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git, Junio C Hamano

Johannes Schindelin, Mon, Dec 03, 2007 21:47:09 +0100:
> On Mon, 3 Dec 2007, Alex Riesen wrote:
> > +	return  c <= 32  ||
> > +		c == '.' ||
> > +		c == ',' ||
> > +		c == ':' ||
> > +		c == ';' ||
> > +		c == '<' ||
> > +		c == '>' ||
> > +		c == '"' ||
> > +		c == '\'';
> 
> Or enhance ctype.c.
> 

That's be nice, but the "crud" conflicts with existing classification,
so I'd have to change the is*-macros as well. Don't feel like it.

I believe the code is never in hotpath anyway so the shorter the
better.

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

* Re: [PATCH] Simplify crud() in ident.c
  2007-12-03 21:37   ` Alex Riesen
@ 2007-12-03 22:48     ` Johannes Schindelin
  2007-12-03 22:52       ` David Kastrup
  0 siblings, 1 reply; 8+ messages in thread
From: Johannes Schindelin @ 2007-12-03 22:48 UTC (permalink / raw)
  To: Alex Riesen; +Cc: git, Junio C Hamano

Hi,

On Mon, 3 Dec 2007, Alex Riesen wrote:

> Johannes Schindelin, Mon, Dec 03, 2007 21:47:09 +0100:
> > On Mon, 3 Dec 2007, Alex Riesen wrote:
> > > +	return  c <= 32  ||
> > > +		c == '.' ||
> > > +		c == ',' ||
> > > +		c == ':' ||
> > > +		c == ';' ||
> > > +		c == '<' ||
> > > +		c == '>' ||
> > > +		c == '"' ||
> > > +		c == '\'';
> > 
> > Or enhance ctype.c.
> > 
> 
> That's be nice, but the "crud" conflicts with existing classification,
> so I'd have to change the is*-macros as well. Don't feel like it.
> 
> I believe the code is never in hotpath anyway so the shorter the
> better.

Really?

	return !!strchr(".,:;<>\"\\", c);

Ciao,
Dscho

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

* Re: [PATCH] Simplify crud() in ident.c
  2007-12-03 22:48     ` Johannes Schindelin
@ 2007-12-03 22:52       ` David Kastrup
  0 siblings, 0 replies; 8+ messages in thread
From: David Kastrup @ 2007-12-03 22:52 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Alex Riesen, git, Junio C Hamano

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> On Mon, 3 Dec 2007, Alex Riesen wrote:
>
>> Johannes Schindelin, Mon, Dec 03, 2007 21:47:09 +0100:
>> > On Mon, 3 Dec 2007, Alex Riesen wrote:
>> > > +	return  c <= 32  ||
>> > > +		c == '.' ||
>> > > +		c == ',' ||
>> > > +		c == ':' ||
>> > > +		c == ';' ||
>> > > +		c == '<' ||
>> > > +		c == '>' ||
>> > > +		c == '"' ||
>> > > +		c == '\'';
>> > 
>> > Or enhance ctype.c.
>> > 
>> 
>> That's be nice, but the "crud" conflicts with existing classification,
>> so I'd have to change the is*-macros as well. Don't feel like it.
>> 
>> I believe the code is never in hotpath anyway so the shorter the
>> better.
>
> Really?
>
> 	return !!strchr(".,:;<>\"\\", c);

What happened to c <= 32?

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

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

* Re: [PATCH] Simplify crud() in ident.c
  2007-12-03 20:19 ` Jakub Narebski
  2007-12-03 21:32   ` Alex Riesen
@ 2007-12-03 22:55   ` Luke Lu
  1 sibling, 0 replies; 8+ messages in thread
From: Luke Lu @ 2007-12-03 22:55 UTC (permalink / raw)
  To: Jakub Narebski; +Cc: Alex Riesen, git, Junio C Hamano


On Dec 3, 2007, at 12:19 PM, Jakub Narebski wrote:

> Perhaps simplier, but isn't it slower?

Actually it's faster on modern cpu with deep pipelines. The following  
is simple test on my macbookpro (repeated 3 times and picked lowest  
one):

$ time ./crudtest 1000000000
old crud...
real    0m0.856s
user    0m0.839s
sys     0m0.011s
$ time ./crudtest 1000000000 simple
new crud...
real    0m0.431s
user    0m0.421s
sys     0m0.007s

Note: it's compiled with gcc -O2. -O3 gives the same timing; -O:  
simple crud has the same timing while the old crud is 10x slower; -O0  
(off): the simple code is 50% slower than old crud (note: 10x less  
iterations):

$ time ./crudtest0 100000000
old crud...
real    0m0.659s
user    0m0.638s
sys     0m0.008s
$ time ./crudtest0 100000000 simple
new crud...
real    0m1.175s
user    0m1.149s
sys     0m0.014s

Since the default CFLAGS in git Makefile has -O2, the simple/new code  
is faster by default.

__Luke

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

end of thread, other threads:[~2007-12-03 22:56 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-12-03 19:11 [PATCH] Simplify crud() in ident.c Alex Riesen
2007-12-03 20:19 ` Jakub Narebski
2007-12-03 21:32   ` Alex Riesen
2007-12-03 22:55   ` Luke Lu
2007-12-03 20:47 ` Johannes Schindelin
2007-12-03 21:37   ` Alex Riesen
2007-12-03 22:48     ` Johannes Schindelin
2007-12-03 22:52       ` David Kastrup

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).