[Mono-dev] [Patch] Small optimization to String.Replace and StringBuilder.Replace

Juraj Skripsky js at hotfeet.ch
Tue Jun 17 04:01:38 EDT 2008


Hi Andreas,

Thanks for looking at my patch.

I did a few measurements now with short strings and you're right - we're
a tiny bit (4-5%) slower with the calculated stackalloc version. The
compromise you proposed is about 2-3% faster than the const version, so
I incorporated that one.

An updated patch is attached.

- Juraj


On Mon, 2008-06-16 at 19:20 +0200, Andreas Nahr wrote:
> Did you time/measure this change?
> 
> -			const int maxValue = 200; // Allocate 800 byte
> maximum
> +			// Allocate 800 byte maximum
> +			int maxValue = Math.Min ((length + oldValue.Length -
> 1) / oldValue.Length, 200);
>  			int* dat = stackalloc int[maxValue];
>  			fixed (char* source = this, replace = newValue) {
>  				int i = 0, count = 0;
> @@ -1661,6 +1654,9 @@
>  					}
>  					i = found + oldValue.length;
>  				}
> +				if (count == 0)
> +					return this;
> +
>  				int nlen = this.length + ((newValue.length -
> oldValue.length) * count);
>  				String tmp = InternalAllocateStr (nlen);
> 
> My assumption is that for small (common) cases this may produce a noticable
> slowdown.
> I'm fine with the count == 0 shortcut, but the exact calculation will
> probably hurt performance.
> Maybe a compromise would be
> int maxValue = Math.Min (length, 200);
> 
> Happy Hacking
> Andreas
> 
> > -----Ursprüngliche Nachricht-----
> > Von: mono-devel-list-bounces at lists.ximian.com [mailto:mono-devel-list-
> > bounces at lists.ximian.com] Im Auftrag von Juraj Skripsky
> > Gesendet: Montag, 16. Juni 2008 16:38
> > An: mono-devel
> > Betreff: [Mono-dev] [Patch] Small optimization to String.Replace and
> > StringBuilder.Replace
> > 
> > Hello,
> > 
> > Attached you'll find a patch which reduces the number of allocations
> > done in String.Replace and StringBuilder.Replace.
> > 
> > All unit tests pass.
> > Please review.
> > 
> > - Juraj
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: StrReplace2.patch
Type: text/x-patch
Size: 3697 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080617/89f4d05e/attachment-0001.bin 


More information about the Mono-devel-list mailing list