Quote:Original post by osmanbActually, implementations are tending away from using shared (ref counted) strings anymore. The problem is COW (copy on write). In the ever-growing world of multi-threading as multi-core CPUs become more common and with more cores etc, is that the thread synchronisation required to access these strings is slowing things down much more than simply copying the string outright in the first place does. Memory is vast and cheap too.
But that will never do pooled (shared) strings, which is a common and frequent approach to dealing with them on console. (I've worked on at least three different console codebases that basically did the same thing.) Make a new string class that stores a pointer to a shared instance of string data. When you construct one of those (eg, from a const char*), see if it's in the pool already. If so, just point at the shared copy (and increment ref-count). Otherwise, add it.
Benefits:
- Lower total memory cost. You don't pay anything extra for having N copies of any given string being referenced throughout your code.
- Constant-time comparison. Because of the semantics of the shared pool, you can just do pointer-equality to compare strings for equality. This is very, very nice.
I agree it's a great idea for a single-threaded console app, but in the PC world it is definitely fading out.