ctl::string small-string optimization (#1199)

A small-string optimization is a way of reusing inline storage space for
sufficiently small strings, rather than allocating them on the heap. The
current approach takes after an old Facebook string class: it reuses the
highest-order byte for flags and small-string size, in such a way that a
maximally-sized small string will have its last byte zeroed, making it a
null terminator for the C string.

The only flag we have is in the highest-order bit, that says whether the
string is big (set) or small (cleared.) Most of the logic switches based
on the value of this bit; e.g. data() returns big()->p if it's set, else
small()->buf if it's cleared. For a small string, the capacity is always
fixed at sizeof(string) - 1 bytes; we store the length in the last byte,
but we store it as the number of remaining bytes of capacity, so that at
max size, the last byte will read zero and serve as our null terminator.

Morally speaking, our class's storage is a union over two POD C structs.
For now I gravitated towards a slightly more obtuse approach: the string
class itself contains a blob of the right size, and we alias that blob's
pointer for the two structs, taking some care not to run afoul of object
lifetime rules in C++. If anyone wants to improve on this, contributions
are welcome.

This commit also introduces the `ctl::__` namespace. It can't be legally
spelled by library users, and serves as our version of boost's "detail".

We introduced a string::swap function, and we now use that in operator=.
operator= now takes its argument by value, so we never need to check for
the case where the pointers are equal and can just swap the entire store
of the argument with our own, leaving the C++ destructor to free our old
storage afterwards.

There are probably still a few places where our capacity is slightly off
and we grow too fast, although there don't appear to be any where we are
too slow. I will leave these to be fixed in future changes.
This commit is contained in:
Jōshin 2024-06-06 17:50:51 -07:00 committed by GitHub
parent df6b384e31
commit 8b3e368e9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 333 additions and 139 deletions

View file

@ -23,7 +23,9 @@
namespace ctl {
string::~string() noexcept
namespace __ {
big_string::~big_string() /* noexcept */
{
if (n) {
if (n >= c)
@ -36,27 +38,36 @@ string::~string() noexcept
free(p);
}
string::string(const char* s) noexcept
} // namespace __
string::~string() /* noexcept */
{
if (isbig()) {
big()->~big_string();
}
}
string::string(const char* s) noexcept : string()
{
append(s, strlen(s));
}
string::string(const string& s) noexcept
string::string(const string& s) noexcept : string()
{
append(s.data(), s.size());
}
string::string(const string_view s) noexcept : string()
{
append(s.p, s.n);
}
string::string(const string_view s) noexcept
{
append(s.p, s.n);
}
string::string(size_t size, char ch) noexcept
string::string(size_t size, char ch) noexcept : string()
{
resize(size, ch);
}
string::string(const char* s, size_t size) noexcept
string::string(const char* s, size_t size) noexcept : string()
{
append(s, size);
}
@ -64,29 +75,39 @@ string::string(const char* s, size_t size) noexcept
const char*
string::c_str() const noexcept
{
if (!n)
if (!size())
return "";
if (n >= c)
if (size() >= capacity())
__builtin_trap();
if (p[n])
if (data()[size()])
__builtin_trap();
return p;
return data();
}
void
string::reserve(size_t c2) noexcept
{
char* p2;
if (c2 < n)
c2 = n;
size_t n = size();
if (c2 < n + 1)
c2 = n + 1;
if (c2 <= __::string_size)
return;
if (ckd_add(&c2, c2, 15))
__builtin_trap();
c2 &= -16;
if (!(p2 = (char*)realloc(p, c2)))
__builtin_trap();
if (!isbig()) {
if (!(p2 = (char*)malloc(c2)))
__builtin_trap();
__builtin_memcpy(p2, data(), size() + 1);
} else {
if (!(p2 = (char*)realloc(big()->p, c2)))
__builtin_trap();
}
std::atomic_signal_fence(std::memory_order_seq_cst);
c = c2;
p = p2;
set_big_capacity(c2);
big()->n = n;
big()->p = p2;
}
void
@ -96,41 +117,54 @@ string::resize(size_t n2, char ch) noexcept
if (ckd_add(&c2, n2, 1))
__builtin_trap();
reserve(c2);
if (n2 > n)
memset(p + n, ch, n2 - n);
p[n = n2] = 0;
if (n2 > size())
memset(data() + size(), ch, n2 - size());
if (isbig()) {
big()->p[big()->n = n2] = 0;
} else {
set_small_size(n2);
data()[size()] = 0;
}
}
void
string::append(char ch) noexcept
{
if (n + 2 > c) {
size_t c2 = c + 2;
c2 += c2 >> 1;
size_t n2;
if (ckd_add(&n2, size(), 2))
__builtin_trap();
if (n2 > capacity()) {
size_t c2 = capacity() + 2;
if (ckd_add(&c2, c2, c2 >> 1))
__builtin_trap();
reserve(c2);
}
p[n++] = ch;
p[n] = 0;
// XXX do we care to fence this?
data()[size()] = ch;
data()[size() + 1] = 0;
if (isbig()) {
++big()->n;
} else {
--small()->rem;
}
}
void
string::grow(size_t size) noexcept
{
size_t need;
if (ckd_add(&need, n, size))
if (ckd_add(&need, this->size(), size))
__builtin_trap();
if (ckd_add(&need, need, 1))
__builtin_trap();
if (need <= c)
if (need <= capacity())
return;
size_t c2 = c;
if (!c2) {
c2 = need;
} else {
while (c2 < need)
if (ckd_add(&c2, c2, c2 >> 1))
__builtin_trap();
}
size_t c2 = capacity();
if (!c2)
__builtin_trap();
while (c2 < need)
if (ckd_add(&c2, c2, c2 >> 1))
__builtin_trap();
reserve(c2);
}
@ -139,8 +173,14 @@ string::append(char ch, size_t size) noexcept
{
grow(size);
if (size)
memset(p + n, ch, size);
p[n += size] = 0;
memset(data() + this->size(), ch, size);
// XXX fence?
if (isbig()) {
big()->n += size;
} else {
small()->rem -= size;
}
data()[this->size()] = 0;
}
void
@ -148,55 +188,53 @@ string::append(const void* data, size_t size) noexcept
{
grow(size);
if (size)
memcpy(p + n, data, size);
p[n += size] = 0;
memcpy(this->data() + this->size(), data, size);
if (isbig()) {
big()->n += size;
} else {
small()->rem -= size;
}
this->data()[this->size()] = 0;
}
void
string::pop_back() noexcept
{
if (!n)
if (!size())
__builtin_trap();
p[--n] = 0;
if (isbig()) {
--big()->n;
} else {
++small()->rem;
}
data()[size()] = 0;
}
string&
string::operator=(string&& s) noexcept
string::operator=(string s) noexcept
{
if (p != s.p) {
if (p) {
clear();
append(s.p, s.n);
} else {
p = s.p;
n = s.n;
c = s.c;
s.p = nullptr;
s.n = 0;
s.c = 0;
}
}
swap(s);
return *this;
}
bool
string::operator==(const string_view s) const noexcept
{
if (n != s.n)
if (size() != s.n)
return false;
if (!n)
if (!s.n)
return true;
return !memcmp(p, s.p, n);
return !memcmp(data(), s.p, s.n);
}
bool
string::operator!=(const string_view s) const noexcept
{
if (n != s.n)
if (size() != s.n)
return true;
if (!n)
if (!s.n)
return false;
return !!memcmp(p, s.p, n);
return !!memcmp(data(), s.p, s.n);
}
bool
@ -204,35 +242,35 @@ string::contains(const string_view s) const noexcept
{
if (!s.n)
return true;
return !!memmem(p, n, s.p, s.n);
return !!memmem(data(), size(), s.p, s.n);
}
bool
string::ends_with(const string_view s) const noexcept
{
if (n < s.n)
if (size() < s.n)
return false;
if (!s.n)
return true;
return !memcmp(p + n - s.n, s.p, s.n);
return !memcmp(data() + size() - s.n, s.p, s.n);
}
bool
string::starts_with(const string_view s) const noexcept
{
if (n < s.n)
if (size() < s.n)
return false;
if (!s.n)
return true;
return !memcmp(p, s.p, s.n);
return !memcmp(data(), s.p, s.n);
}
size_t
string::find(char ch, size_t pos) const noexcept
{
char* q;
if ((q = (char*)memchr(p, ch, n)))
return q - p;
if ((q = (char*)memchr(data(), ch, size())))
return q - data();
return npos;
}
@ -240,10 +278,10 @@ size_t
string::find(const string_view s, size_t pos) const noexcept
{
char* q;
if (pos > n)
if (pos > size())
__builtin_trap();
if ((q = (char*)memmem(p + pos, n - pos, s.p, s.n)))
return q - p;
if ((q = (char*)memmem(data() + pos, size() - pos, s.p, s.n)))
return q - data();
return npos;
}
@ -251,15 +289,15 @@ string
string::substr(size_t pos, size_t count) const noexcept
{
size_t last;
if (pos > n)
if (pos > size())
__builtin_trap();
if (count > n - pos)
count = n - pos;
if (count > size() - pos)
count = size() - pos;
if (ckd_add(&last, pos, count))
last = n;
if (last > n)
last = size();
if (last > size())
__builtin_trap();
return string(p + pos, count);
return string(data() + pos, count);
}
string&
@ -268,12 +306,12 @@ string::replace(size_t pos, size_t count, const string_view& s) noexcept
size_t last;
if (ckd_add(&last, pos, count))
__builtin_trap();
if (last > n)
if (last > size())
__builtin_trap();
size_t need;
if (ckd_add(&need, pos, s.n))
__builtin_trap();
size_t extra = n - last;
size_t extra = size() - last;
if (ckd_add(&need, need, extra))
__builtin_trap();
size_t c2;
@ -281,42 +319,57 @@ string::replace(size_t pos, size_t count, const string_view& s) noexcept
__builtin_trap();
reserve(c2);
if (extra)
memmove(p + pos + s.n, p + last, extra);
memcpy(p + pos, s.p, s.n);
p[n = need] = 0;
memmove(data() + pos + s.n, data() + last, extra);
memcpy(data() + pos, s.p, s.n);
if (isbig()) {
big()->p[big()->n = need] = 0;
} else {
set_small_size(need);
data()[size()] = 0;
}
return *this;
}
string&
string::insert(size_t i, const string_view s) noexcept
{
if (i > n)
if (i > size())
__builtin_trap();
size_t extra = n - i;
size_t extra = size() - i;
size_t need;
if (ckd_add(&need, n, s.n))
if (ckd_add(&need, size(), s.n))
__builtin_trap();
if (ckd_add(&need, need, 1))
__builtin_trap();
reserve(need);
if (extra)
memmove(p + i + s.n, p + i, extra);
memcpy(p + i, s.p, s.n);
p[n += s.n] = 0;
memmove(data() + i + s.n, data() + i, extra);
memcpy(data() + i, s.p, s.n);
if (isbig()) {
big()->n += s.n;
} else {
small()->rem -= s.n;
}
data()[size()] = 0;
return *this;
}
string&
string::erase(size_t pos, size_t count) noexcept
{
if (pos > n)
if (pos > size())
__builtin_trap();
if (count > n - pos)
count = n - pos;
size_t extra = n - (pos + count);
if (count > size() - pos)
count = size() - pos;
size_t extra = size() - (pos + count);
if (extra)
memmove(p + pos, p + pos + count, extra);
p[n = pos + extra] = 0;
memmove(data() + pos, data() + pos + count, extra);
if (isbig()) {
big()->n = pos + extra;
} else {
set_small_size(pos + extra);
}
data()[size()] = 0;
return *this;
}