Skip to content

[FEATURE] replace String classes with StringUtils - #845

Merged
hroest merged 1 commit into
OpenMS:developfrom
hroest:feature/StringUtils
Jul 25, 2014
Merged

hroest merged 1 commit into
OpenMS:developfrom
hroest:feature/StringUtils

Conversation

@hroest

@hroest hroest commented May 24, 2014

Copy link
Copy Markdown
Contributor

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:

  • constructors should be replaced with a toString function
  • const functions should be replaced with an external function call, e.g. use ``StringUtils::hasPrefixinstead ofs.hasPrefix` which de-entangles the code to a certain extend
  • Chainable mutators (in OpenMS::String all those that are non-const and return String &) 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, like s.trim(), would have to be replaced with the function and the assignment, otherwise the code breaks
  • same goes for operators but to an even larger extend

the 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.0 which 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?

@timosachsenberg

Copy link
Copy Markdown
Contributor

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.

@hroest

hroest commented May 26, 2014

Copy link
Copy Markdown
Contributor Author

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

class String
{
  String& trim(); // non-const
  String& toLower(); //non-const
}

which allows us to write String("test").trim().toLower() using only one copy of the string object in memory. The other option would be to have two functions:

namspace StringUtilsMutable
{
  void trim(String& s);
  void toLower(String& s);
}
namspace StringUtilsCopy
{
  String trim(String s);
  String toLower(String s);
}

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 StringUtilsCopy is more efficient but this probably depends on the function)

// Copying allows chaining but is less efficient
String tmps = StringUtilsCopy::toLower(StringUtilsCopy::trim(StringUtils::toString("test")));
// Mutable version which is more verbose but efficient
String tmps = StringUtils::toString("test");
StringUtilsMutable::trim(tmps);
StringUtilsMutable::toLower(tmps);
//Current implementation: concise and efficient
String tmps = String("test").trim().toLower();

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?

@timosachsenberg

Copy link
Copy Markdown
Contributor

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.
E.g. wheter always returning a copy might be automatically optimized away if the compiler recognizes that the return value is just a temporary.
e.g. I would suspect that something like:
String tmps = StringUtilsCopy::toLower(StringUtilsCopy::trim(StringUtils::toString("test")));
could be inlined and optimized together.

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.

@hroest hroest added this to the Release 1.12 milestone Jun 9, 2014
@timosachsenberg

Copy link
Copy Markdown
Contributor

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:
Parameters would be const reference e.g.: String StringUtils::toUpper(const String &)

String s("hello");
s = StringUtils::toUpper(s);
Here the compiler could generate the in-place code.

String s("hello");
String a;
a = StringUtils::toUpper(s);
Here the compiler probably could not generate the in-place code.

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?

@hroest

hroest commented Jun 23, 2014

Copy link
Copy Markdown
Contributor Author

why,not try it out and look at the assembly that gets generated?

sent from my smartphone -- you may keep any typos you find.
On Jun 23, 2014 2:13 AM, "timosachsenberg" [email protected] wrote:

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:
Parameters would be const reference e.g.: String
StringUtils::toUpper(const String &)

String s("hello");
s = StringUtils::toUpper(s);
Here the compiler could generate the in-place code.

String s("hello");
String a;
a = StringUtils::toUpper(s);
Here the compiler probably could not generate the in-place code.

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?

—
Reply to this email directly or view it on GitHub
#845 (comment).

@timosachsenberg

Copy link
Copy Markdown
Contributor

here is the playground - don't want to jump into conclusions yet:
http://goo.gl/uFMBh1
I used -O3 -std=c++11 (I think move semantics might have an impact)

@hroest

hroest commented Jun 23, 2014

Copy link
Copy Markdown
Contributor Author

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.

@timosachsenberg

Copy link
Copy Markdown
Contributor

Yeah, still hard to believe. I really hoped the compiler would do more.

@aiche

aiche commented Jun 24, 2014

Copy link
Copy Markdown
Contributor

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

String s = "hello world  ";
s.trim().suffix(' ').quote().chop(2);

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.

same goes for operators but to an even larger extend

The operator+= stuff is only a shortcut for std::string::append which is actually chainable, so replacing it should be easy enough.

@timosachsenberg

Copy link
Copy Markdown
Contributor

Agreed. Judging from the generated assembly code I would vote for the in-place modification.

@hendrikweisser

Copy link
Copy Markdown
Contributor

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:

String tmps = StringUtilsCopy::toLower(StringUtilsCopy::trim(StringUtils::toString("test")));

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.

@timosachsenberg

Copy link
Copy Markdown
Contributor

Ok the discussion has a bit stalled. I think Hendrik also raised some valid points here.
Maybe we can stick with his proposal and first tackle the std::string inheritance issue as this is the point everyone agrees with. Least intrusive would be to replicate the std::string interface in the String class.
What do you think?

@hroest

hroest commented Jul 20, 2014

Copy link
Copy Markdown
Contributor Author

I also agree with Hendrik, we maybe shouldnt aim for completey purity here
but for a workable comprimise between useability and performance.

Maybe I got sidetracked here a bit too much. Basically what we would like
to have is a system which optimizes i) performance and ii) readability (not
necessarily in that order). I think currently we have a system that is good
in readability and useability and maybe (if we can show it hurts) we should
think about the cases that are hurting us in performance.

Personally I have a feeling that duplicating the std::string interface and
removing inheritance will be quite a bit of work for limited benefit. The
benefit to remove inheritance for MSExperiment was quite obvious to me
since it prohibits an alternative implementation of a spectra collection,
but for String the benefits seem less clear. Also, it is quite likely that
almost all of the interface it used somehow so we would really basically
duplicate the entire interface inside the class - which is what we also
achieve with public inheritance.

The question is whether this current pull request makes sense or not for
OpenMS? It cleans up the code a bit and puts stuff into a utility class
that does not need access to private members of String - still not a bad
thing I think.

On 12 July 2014 18:59, Timo Sachsenberg [email protected] wrote:

Ok the discussion has a bit stalled. I think Hendrik also raised some
valid points here.
Maybe we can stick with his proposal and first tackle the std::string
inheritance issue as this is the point everyone agrees with. Least
intrusive would be to replicate the std::string interface in the String
class.
What do you think?

—
Reply to this email directly or view it on GitHub
#845 (comment).

@timosachsenberg

Copy link
Copy Markdown
Contributor

+1

hroest added a commit that referenced this pull request Jul 25, 2014
[FEATURE] replace String classes with StringUtils
@hroest
hroest merged commit ce574da into OpenMS:develop Jul 25, 2014
@hroest
hroest deleted the feature/StringUtils branch July 25, 2014 09:39
@hroest

hroest commented Jul 25, 2014

Copy link
Copy Markdown
Contributor Author

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?

hroest added a commit that referenced this pull request Jul 28, 2014
[FIX] fixed unity build error introduced in #845
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants