Remember that std::string can be on the stack or a member of a larger structure.
These comparisons seem to all be against versions of std::string that keep size/capacity inside the std::string, not ones (like the default GCC one) that keep those members adjacent to the character data itself
Well that's a whole other topic: does your string library support copy on write or not? If you have mostly short strings and SSO you are better off without copy on write.
My string class has copy on write, including splicing- so it uses the most space.. I'm sure it's not really worth it (It also supports string semi-predicates: you can safely return NULL as a string and test for it- I sure wish they had this in the standard library).
struct Stringraw
{
int ref; // Reference count
int len; // String length
// String data starts here
};
class String
{
Stringraw *raw; // Pointer to possibly shared malloc block holding string
char *s; // Start of string in raw
int len; // Length of string
. . .
// Sub-string
inline String substr(int start,int size) const
{
String x;
x.s=s+start;
x.len=size;
x.raw=raw;
++raw->ref;
return x;
}
I think refcounted strings are falling out of favor. In a multithreaded environment you need to use atomic ops to maintain the reference count which gets expensive. (folly's fbstring class does use them for large strings though)
Does there exist a similar optimization for arbitrary size integers? Like using the pointer that is supposed to point to something like an array of ints, instead store the int itself (with a bit of bit twiddling to make room for a flag that says that "this is an integer; don't use me as a pointer").
https://github.com/facebook/folly/blob/master/folly/FBString...