Small string optimisation proof-of-concept
github.com
github.com
https://github.com/facebook/folly/blob/master/folly/FBString...
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
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)