On Sat, Sep 29, 2018 at 11:50 PM Walter Bright via Digitalmars-d
<digitalmars-d@puremagic.com> wrote:
>
> On 9/29/2018 9:34 PM, Manu wrote:
> > GNU's std::string implementation stores an interior pointer! >_<
> >
> > No other implementation does this. It's a really bad implementation
> > actually, quite inefficient. It could make better use of its space for
> > small-strings if it wasn't wasting 8-bytes for an interior pointer to
> > a small string buffer...
>
> Could you post a synopsis of the layout of std::string?

The code's all in the PR if you wanna dig into it.

The synopsis is:
struct string
{
  char* ptr;
  size_t len;
  union
  {
    char[16] localBuffer;
    size_type allocatedCapacity;
  }

  bool isAllocated() { return ptr != &localBuffer[0]; }
  bool capacity() { return isAllocated() ? allocatedCapacity :
localBuffer.length; }

  this(DefaultCtor) { ptr = &localBuffer[0]; }  // <- and here it is.
interior pointer that breaks move semantics
}

Other implementations make much better use of that built-in space by
not wasting 8 bytes on an interior pointer for small-strings.

Reply via email to