-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try a String.Replace with 1-length oldValue and multi-length newValue #44061
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Do you have data on how common this pattern is? Eg by using grep.app with some regex, possibly. |
IMHO, |
Even if this API will be not approved, internally use this path to handle the case which we receive string with length of 1 is seems to be good. |
Thanks for the issue. #44088 optimizes the path where oldValue is a single character. There's not much overhead in extracting the single char from the string, so the primary benefit of the additional proposed overload would I think be just if the oldValue was dynamically computed and required a string allocation that could otherwise be avoided, but from looking at existing usage, that's not common. |
Hi @stephentoub Awesome you optimizes single char old value that soon, thanks for optimizing work :) Actually what I would like to do is to find some idea during my daily .net development and try to verify benefit of code change for idea in my local. If I can convince myself, I really want to contribute my change to dotnet repo based on your guys comments. So the question is, if we want to contribute code, should we raise issue before PR, or raise PR directly and wait for comment? What is the recommended way to have opportunity to contribute code? Can you guys give related comments just in current issue so that issuer can adjust solution? please give actual guidance, thanks again! |
PRs are welcome (though of course aren't guaranteed to be merged) without an issue if the changes being proposed are relatively small and well-justified, e.g. you're able to improve the performance of some existing API and speak to both the improvements and possible regressions with numbers. Given #44061 (comment), I'm going to close this issue now. If you still see an impactful opportunity here and can highlight places where a new API would make a meaningful difference, feel free to re-open. Thanks. |
Background and Motivation
When use string.Replace() to replace 1-length 'oldValue' by >1 length string newValue (length is known before compiling), sometimes I thought there was a method overload as: string.Replace(char, string). Like existing method string.split() which has one overload with string-type separator but also has char-type separator overload. The benefit of trying to use char-type argument overload as possible is well-known and described by lots of blogs (char is a value type without overhead of ref type and most time it is in stack and size is also smaller than word size so that argument assignment can have better performance, compared to string)
Proposed API
With this idea, I tried to understand source code of string.Split() and created this new overload by refering to implementation of string.Replace(string, string). The main and natural difference is can remove multi-char handling logic for 'oldValue' argument.
After that, I read some wikis of dotnet/runtime and consult some contributors so that I have my first-time journey to build my new corelib locally and update dotnet/performance to run benchmark on mew string.Replace(char, string). Benchmark result compared with original overload passing 1-length oldValue string:
So just want to ask, is it worth to introduce this new Replace() overload to corelib so that can have better performance in case of 1-length 'oldValue' known during compile time (If yes, can I try to contribute into remote repo). Please give comment, thanks!
The text was updated successfully, but these errors were encountered: