6 ms·
The problem with std::string is that it's named wrong. It should be called std::string_buffer, because that is what it is. Its performance characteristics are c
by humanrebar 12y ago
The problem with std::string is that it's named wrong. It should be called std::string_buffer, because that is what it is. Its performance characteristics are closer to a std::vector than a std::array (now available since C++11).
Many projects cannot copy around std::vector<char> in good conscience. They really want a copy-on-write string, an immutable string, a rope, a reference-counted string, or an always-in-place string. Or some combination of the above depending on the circumstance.
The problem is that std::string is not a good type to use as a parameter for various reasons. In addition to its aggressive allocation behavior, it's also fairly inflexible. What are the alternatives?
1. boost::string_ref - available now, so use it
2. std::string_view - available starting in C++14 and works roughly like boost::string_ref
3. pass around pairs of iterators instead of single objects
3) is actually the most flexible, though it requires different kinds of overhead. The most obvious way would be to template all your string-accepting functions on two parameters: the type of your begin iterator and the type of your end iterator. But the benefit is that you can pass around any of the above to your heart's content, plus more, like elements in tries.
std::string still has an important place, but it should generally be used as a private member variable, not as something you require in your interface. Pretty much the same thing goes for char* unless you are implementing a C ABI (plus a size, please). Even then, you can immediately convert to/from a boost::string_ref and still have yourself a self-contained reference to a bounded character sequence.
- rwmj 12y agoOr proper garbage collection so you don't need to worry about who owns the string and you don't have the big overhead of ref-counting. Why on earth are they using C++ for a web browser anyway? That's about the worst possible choice of programming language for that problem domain.
- IvyMike 12y agoSo which web browsers are not written in C++?
- rwmj 12y agoAlmost all of Windows (including apps that predominantly do string and tree manipulation) is written in C++. Almost all of Linux userspace is written in C. These are still terrible, insecure, fragile languages which cause frustration and loss to programmers and users every day.
- panzi 12y ago> Almost all of Linux userspace is written in C. Depends on what you count. Qt can be seen as (part of the) Linux userspace. KDE applications are usually written in C++ (partially in QML+JavaScript), Gtk applications are written in C or Vala (a few in C#). If you don't count DE frameworks basically the only thing left would be libc, which of course is C (but available in some form under every noteworthy OS).
- ibebrett 12y agowhat experience do you have with writing c++? what is the last large project you completed with it? i'm not a huge fan of c++, but it has been the foundation of an extremely large number of wildly successful and pervasive projects.
- rwmj 12y agoI have in the past written many large C++ projects. I now try to avoid it as much as possible, but the last time I programmed in C++ was just two weeks ago[1], and it reminded me how bad it was -- terrible error messages, and abysmal performance of the final program. At some point I will have to replace it because the program doesn't meet our performance requirements, and no one can tell me why[2]. (Edit: Apparently [2] has been answered now) [1] http://git.annexia.org/?p=virt-bmap.git;a=tree http://git.annexia.org/?p=virt-bmap.git;a=tree [2] https://stackoverflow.com/questions/27152834/how-to-improve-performance-of-boost-interval-map-lookups https://stackoverflow.com/questions/27152834/how-to-improve-...
- IvyMike 12y agoIn #2, I'm curious why you didn't like sehe's answer?
- titanomachy 12y agoAll 5 major web browsers are written in C++. EDIT: Chrome, Firefox, Safari, IE, Opera
- hutattedonmyarm 12y agoSure that Safari is C++ and not Objective-C?
- mahouse 12y agoWell WebKit is C++, but the UI is probably Obj-C.
- kibwen 12y agoBecause they forked it from WebKit which is derived from KHTML, which has a history that stretches back to 1998. Better to ask which languages would have been a better fit for a web browser in the context of the late nineties that have had the staying power of C++ (I'm guessing you'll come up with a rather short list). If you want to see an attempt to write a new browser engine in a language that is memory-safe by default, I suggest you check out Mozilla's Servo engine: https://github.com/servo/servo https://github.com/servo/servo
- titanomachy 12y agoThis is awesome! Tons of respect for Mozilla for supporting this kind of research.
- panzi 12y agoSharing mutable buffers? Nope, you would still need to switch to immutable strings or copy the buffers.
- ibebrett 12y agohow do you "share" objects that multiple pieces of code wan't to own. you need to copy if you are passing around a mutable string.
- Scramblejams 12y agoBy the way, reference counting may not have to be as bad as it is. Looks like there's a lot of opportunity for improvement. See here: http://users.cecs.anu.edu.au/~steveb/downloads/pdf/rc-ismm-2012.pdf http://users.cecs.anu.edu.au/~steveb/downloads/pdf/rc-ismm-2...
- ninkendo 12y ago> The problem is that std::string is not a good type to use as a parameter for various reasons. In addition to its aggressive allocation behavior, it's also fairly inflexible. What are the alternatives? I think a more specific critique is that std::string is not a good type to use as a copy parameter (ie. a non-reference, non-rvalue parameter.) It's perfectly acceptable in scenarios where you're passing it as a const reference (ie. not transferring ownership) or passing it as an rvalue where it can be moved in-place. Your critique (and suggestion to use string_ref) seems to be oriented more towards use cases where a mutated copy of a string (be it a substring or some other transformation) needs to be passed around... I don't think it's fair to say "it's not good as a parameter."
- humanrebar 12y agoNo, it's bigger than just copy parameters. std::string, even as a const reference, only works well in these cases: 1. your users already have a std::string 2. you want to support string literals 3. you'll be converting to string in the implementation anyway What if I have a vector<const char>? What if I have a type called SanitizedString or OracleString? What about character arrays retrieved from C ABI calls? And even if 3. is true, you are encoding implementation details in your interface. If you need to optimize your type later to use a trie, you'll have to do extra allocations and copies after all. My point is that interfaces should require what they need: a character sequence if they need a character sequence and a character buffer if they need a character buffer. Hence my comment about std::string being more appropriately named std::string_buffer. Even string_ref isn't perfect because it assumes your sequence is in contiguous memory. I'm hoping developments in the C++ language and standard library comes up with a good answer to that problem, perhaps one that leverages the concepts proposals. But even a concepts-based solution will have drawbacks, such as requiring even more code to go in your header file.
- detrino 12y agostd::string_view didn't make it into C++14.
- humanrebar 12y agoAh, I guess I'm behind in the news then. I thought it was in an experimental header. I guess I hope it's in C++17 then, or else we may need to root for a good ranges proposal that covers the same ground.