-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize String.Replace(string, string) (#44088)
We can significantly improve its throughput for a few scenarios: - If both oldValue and newValue are single characters in the form of strings, we can just delegate to the Replace(char, char) overload, which is much faster. - If oldValue is a single character but newValue isn't, we can use IndexOf to find it, rather than an open-coded loop, making search times much faster for reasonably sized strings. - If oldValue is multiple characters and the string being searched for isn't super frequent (e.g. doesn't repeat every few characters), we can significantly speed up throughput by using IndexOf to search for the whole string. For example, replacing "\r\n" with "\n" in the contents of a typical file. This does come at a measurable cost when the oldValue is really common and tightly packed, e.g. searching for "aa" in "aaaaaaaaaaaaaaaaaa", so we can decide whether the tradeoff is the right one.
- Loading branch information
1 parent
3cc9f2c
commit f29a272
Showing
1 changed file
with
48 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters