Re: [PATCH] speed: reuse char instead of recreation in loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 25 May 2009, Thomas Spura wrote:

> Am Mon, 25 May 2009 22:16:02 +0200 schrieb Björn Steinbrink:
> 
> > On 2009.05.25 19:44:10 +0000, Thomas Spura wrote:
> >> Move a char and a char * outside of a for loop for speed improvements
> >> 
> >> Signed-off-by: Thomas Spura <tomspur@xxxxxxxxxxxxxxxxx> ---
> >> Comments?
> >> 
> >>  transport.c |    7 +++----
> >>  1 files changed, 3 insertions(+), 4 deletions(-)
> >> 
> >> diff --git a/transport.c b/transport.c index 17891d5..e350937 100644
> >> --- a/transport.c
> >> +++ b/transport.c
> >> @@ -263,11 +263,10 @@ static int write_refs_to_temp_dir(struct strbuf
> >> *temp_dir,
> >>  		int refspec_nr, const char **refspec)
> >>  {
> >>  	int i;
> >> +	unsigned char sha1[20];
> >> +	char *ref;
> >>  
> >>  	for (i = 0; i < refspec_nr; i++) {
> >> -		unsigned char sha1[20];
> >> -		char *ref;
> >> -
> > 
> > I doubt that this makes any difference at all.
> 
> With ints, the loop costs about 40% of speed. Without recreation, it 
> should be always faster.

Actually, having the variables go out of scope should be at least as fast. 
The compiler doesn't actually do anything to make the old variable
inaccessible and get a new variable; with the variable uninitialized, it's 
legitimate for the compiler to simply reuse the same storage for all 
iterations. Futhermore, with the variables declared inside the loop, the 
compiler is allowed to make optimizations that would fail to preserve 
those variables between iterations. There are probably no such 
optimizations in this code for it to make, but, in general, letting 
variables in loops go out of scope (in C) only improves optimization 
possibilities.

	-Daniel
*This .sig left intentionally blank*

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]