ctl::string cleanup (#1215)

We now fully initialize a ctl::string’s memory, so that it is always set
to a well-defined value, thus making it always safe to memcpy out of it.
This incidentally makes our string::swap function legal, which it wasn’t
before. This also saves us a store in string::reserve.

Now that we have made both big_string and small_string POD, I believe it
is safe to elide the launder calls, and have done so, thus cleaning up a
lot of the blob-related code.

I also got rid of set_big_capacity and replaced it with a set_big_string
that leaves us in a well-defined state afterwards. This function also is
able to be somewhat simpler; rather than delicate bit-twiddling, it just
reaches straight into blob and rewrites it wholesale.

Overall, this shaves about 1–2ns off of most benchmarks, and adds 1ns to
only one of them - creating a string from a char *.
This commit is contained in:
Jōshin 2024-06-15 10:45:52 -07:00
parent d9b4f647d8
commit 8e37ee2598
No known key found for this signature in database
2 changed files with 19 additions and 28 deletions

View file

@ -91,16 +91,13 @@ string::reserve(size_t c2) noexcept
if (!isbig()) {
if (!(p2 = (char*)malloc(c2)))
__builtin_trap();
memcpy(p2, data(), size());
p2[size()] = 0;
memcpy(p2, data(), __::string_size);
} else {
if (!(p2 = (char*)realloc(big()->p, c2)))
__builtin_trap();
}
std::atomic_signal_fence(std::memory_order_seq_cst);
set_big_capacity(c2);
big()->n = n;
big()->p = p2;
set_big_string(p2, n, c2);
}
void