13

My application is having memory problems, including copying lots of strings about, using the same strings as keys in lots of hashtables, etc. I'm looking for a base class for my strings that makes this very efficient.

I'm hoping for:

  • String interning (multiple strings of the same value use the same memory),
  • copy-on-write (I think this comes for free in nearly all std::string implementations),
  • something with ropes would be a bonus (for O(1)-ish concatenation).

My platform is g++ on Linux (but that is unlikely to matter).

Do you know of such a library?

Paul Biggar
  • 27,579
  • 21
  • 99
  • 152

4 Answers4

9

copy-on-write (I think this comes for free in nearly all std::string implementations)

I don't believe this is the case any longer. Copy-on-write causes problems when you modify the strings through iterators: in particular, this either causes unwanted results (i.e. no copy, and both strings get modified) or an unnecessary overhead (since the iterators cannot be implemented purely in terms of pointers: they need to perform additional checks when being dereferenced).

Additionally, all modern C++ compilers perform NRVO and eliminate the need for copying return value strings in most cases. Since this has been one of the most common cases for copy-on-write semantics, it has been removed due to the aforementioned downsides.

Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
7

If most of your strings are immutable, the Boost Flyweight library might suit your needs.

It will do the string interning, but I don't believe it does copy-on-write.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
5

Andrei Alexandrescu's 'Policy Based basic_string implementation' may help.

graham.reeds
  • 16,230
  • 17
  • 74
  • 137
2

Take a look at The Better String Library from legendary Paul Hsieh

Indy9000
  • 8,651
  • 2
  • 32
  • 37
  • That looks quite nice but it sorely lacks iterators. – Konrad Rudolph Jul 13 '09 at 09:24
  • 2
    I wouldn't trust any library write who states that "Bstrlib is, by design, impervious to memory size overflow attacks. The reason is it is resiliant to length overflows is that bstring lengths are bounded above by INT_MAX, instead of ~(size_t)0.". Yet another amateur who thinks he's found a silver bullet, that's how it appears. – MSalters Jul 13 '09 at 10:51
  • It doesn't seem to offer anything I'm looking for... – Paul Biggar Jul 13 '09 at 12:01
  • 2
    @MSalters I think you are confusing buffer overflows with memory size overflows here. Still, there are silver bullets in the buffer overflow department and they are called runtime checking of array bounds. – jpc Apr 30 '11 at 01:34