[IronPython] Performance str.replace in Silverlight

Dino Viehland dinov at microsoft.com
Tue Jun 1 22:11:35 CEST 2010


Michael wrote:
> I just tracked down a really nasty performance bug in our Silverlight
> application. It turned out that doing a simple string replace in a 400
> character string was taking 700ms. As we actually do two replaces and
> the text is usually substantially longer this was a real problem.
> 
> I fixed the problem by switching to explicitly calling the .NET
> String.Replace instead of str.replace, so it looks like an IronPython
> issue. It doesn't happen with *all* text, but it looks like non-ascii
> exacerbates the problem.
> 
> The particular text that triggered it was:
> 
> Die Neuanlage einer Welle muss auch zu Einträgen in der Tabelle
> dbo_tv_wellenwebsitesfirmenverbinder führen. Dabei werden die
> Zuordnungen aus der Vorwelle bei der Neuanlage einer neuen Welle
> einmalig übernommen. Jede Zeile der Vorwelle wird also kopiert und die
> Kopie erhält die Welle_uniqueID der neuen Welle. Die Verbindung zwischen
> Website_uniqueID <-> Firmen_uniqueID <-> FirmenAbrechnung_uniqueID
> bleibt somit erhalten.
> 
> The replace code was:
> 
> text = text.replace('\r\n', '\n').replace('\r', '\n')
> 
> The fix was:
> 
> text = String.Replace(text, '\r\n', '\n')
> text = String.Replace(text, '\r', '\n')

Couple of follow up questions (a simple repro isn't working for me):
	Is this on your Mac?
	What locale is your machine set to?  I'm guessing it's something like
en-UK (or whatever the Mac equivalent is if it's different) or do you have it
set to a German locale?

If you're willing to try an experiment to re-build IronPython you could try 
changing the "v.IndexOf(oldString, start)" call in StringOps.cs to be:

v.IndexOf(oldString, start, StringComparison.Ordinal)

I think that's the only culture sensitive code path in our replace 
Implementation and so my guess is that's the problem.  If you can't rebuild
and try it out I can probably track down a Mac to try the repro on.




More information about the Ironpython-users mailing list