New, faster StringReplace
Original Reporter info from Mantis: jarto
-
Reporter name: Jarto Tarpio
Original Reporter info from Mantis: jarto
- Reporter name: Jarto Tarpio
Description:
RTL's StringReplace is extremely slow. It does a lot of string copy and concatenation - even with cases where the search and replace patterns are as long. I noticed this with Indy10, which uses StringReplace to replace '+' with ' ' while parsing http POST data.
Steps to reproduce:
Try StringReplace(StringOfChar('a',200000),'a','b',[rfReplaceAll]);
Additional information:
Attached is a new, faster version, which I'd like to contribute to FPC's RTL. With a string of just 50K, it's up to 150 times faster than FPC's current implementation. When the string is 200K, it's up to 1000 times faster.
All string concatenations are eliminated. The general idea is to calculate the length of the result string, set the length once and only manipulate the contents of it.
The new implementation uses PosEx so StrUtils is needed. I wonder if it's a problem?
Mantis conversion info:
- Mantis ID: 26864
- Version: 2.6.4
- Fixed in version: 3.1.1
- Fixed in revision: 33055 (#cf530f11)
- Monitored by: » luizamerico (Luiz Americo), » and (Andrew G. Khodotov), » faber (Tomasz Wieckowski), » Vincent (Vincent Snijders)
- Target version: 3.0.2