Repository navigation
[FEATURE] replace String classes with StringUtils - #845
Conversation
|
Chaining leads to very nice syntax but also comes with performance problems (I had to replace some instances of it because it had quite a big impact.). I could live with e.g. StringUtils::trim(s) and therefore automatically discourage sequential string modifications a bit. But as you pointed out this would involve quite a lot of work. Chaining also seems closely related to immutable classes as one finds in e.g. Java - but we use it the other way around by returning String& instead of a new String. Mixing mutability and chaining sometimes is also not very intuitive in our case as other functions e.g. return a copy to a new String. |
|
Hi timo, what were the performance problems associated with chaining? I assumed that the implementation would be rather efficient since there would be no need for a copy and all manipulation could be in place (in theory - I think some functions create a temporary object, operate on it and then assign it back to *this , but this seems to be an implementation problem not a design problem, correct?). I agree that the current solution is somewhat confusing sometimes but to me it also seems that it avoids copying the string in some situations. Currently we have which allows us to write where only the second one would allow chaining of operators while the first one would be more efficient but more verbose (maybe not in all cases, there might be cases where So it seems like we are giving up quite a bit of readability and coniceness and maybe even efficiency in either case compared to the current implementation - or am I missing something here? |
|
I mainly replaced occurances of following form (you get the idea): s.substitute("a","").substitute("b","").substitute("c","") Concerning efficiency I'm not sure how well this type of code can be optimized in practice. I'm not really oposed to chaining the way we do it now - I just don't see a huge advantage and there are the tradeoffs we have to life with if we keep. |
|
Stephan and me had recently a quick discussion. He proposed to make StringUtils function work on the passed string (in-place). While think this is probably the best option we wondered if there are other options. Example: String s("hello"); String s("hello"); The idea is, that if temporaries are recognized by the compiler it might be fully optimized away resulting in the same code as in the in-place version. On the other hand the same code would work if a no temporary is used. Does anyone have some information/idea how well this (returning a String and passing a const String&) works on modern compilers? |
|
why,not try it out and look at the assembly that gets generated? sent from my smartphone -- you may keep any typos you find.
|
|
here is the playground - don't want to jump into conclusions yet: |
|
Definitely not the same thing:
it looks like there is much more code generated for the const string version and also the code seems to allocate the extra temporary string explicitly. |
|
Yeah, still hard to believe. I really hoped the compiler would do more. |
|
Let me add my 2cts: I think we should remove the chainable mutators since the current implementation is somehow inconsistent since there are two types of functions returning strings, some return mutated copies, some references to the in-place mutated string. Just as a little example, find the point where the string gets copied without looking into api documentation Since we decided to remove all the classes inheriting from std containers I would say we move towards a clean utility class which either performs all the operations in-place or returns mutated copies. This should cover nearly all cases where we really need the utilities.
The |
|
Agreed. Judging from the generated assembly code I would vote for the in-place modification. |
|
I strongly disagree with Stephan about removing chainable mutators. These exist (in standard C++, Qt, Python...) for a reason - they are very useful and allow idiomatic code. This is just horrible:
I also believe we don't have to be consistent to the point of hurting usability: If some of the mutators return copies and some work in place, so what? The same is true in Qt. The original ticket #448 was primarily about inheriting from "std::string". So "String" should use composition instead and yes, the API and the implementation could probably use a clean-up. But let's not throw the baby out with the bathwater and design an ugly API or get rid of useful features, just for the sake of total consistency and questionable performance considerations. I actually don't think we should get rid of the "String" class - there's too much useful functionality in it. While some of it can be moved to a utility class to make things cleaner, I doubt that everything can (without making things much uglier) and frankly I don't see the big benefit anyway. |
|
Ok the discussion has a bit stalled. I think Hendrik also raised some valid points here. |
|
I also agree with Hendrik, we maybe shouldnt aim for completey purity here Maybe I got sidetracked here a bit too much. Basically what we would like Personally I have a feeling that duplicating the std::string interface and The question is whether this current pull request makes sense or not for On 12 July 2014 18:59, Timo Sachsenberg [email protected] wrote:
|
|
+1 |
[FEATURE] replace String classes with StringUtils
|
ok, I close this pull request at this timepoint @timosachsenberg can you identify a few places where you think current string utils are not performant and then we can think about ways to make it more performant for the special case? |
[FIX] fixed unity build error introduced in #845
This is a first attempt to tackle issue #448 by putting most OpenMS::String functions into a utility header file called StringUtils. Basically the idea is to have simple, independent functions to perform operations on strings and would allow us to replace OpenMS::String (at least partially) with time. Currently one way to do this would be:
toStringfunctioninstead ofs.hasPrefix` which de-entangles the code to a certain extendString &) will be a real pain because there is no way to simply replace with them with an external function since they will not operate on temporary String objects. This would make chaining of of mutators impossible, e.g.String("aha ").trim().toLower()would not really work with external utility functions (unless we copy the object but then all function calls which expect the underlying object to be modified, likes.trim(), would have to be replaced with the function and the assignment, otherwise the code breaksthe main question is whether we would like to move on this and if so, what the way forward should be. One big advantage of the current OpenMS::String class are its utility classes that directly operate on the object and allows us to chain operators in a very easy and intuitive matter, basically writing things like
String("aha ").trim().toLower() + 2.0which would be much harder without such a class.Another question is whether it would be beneficial to at least have parts of the class outsourced to StringUtils, e.g. const functions only. Then at least it would be clearer what is part of the actual String class and which parts are simply utility functions. Any thoughts on this?