Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ctl::string cleanup #1215

Merged
merged 2 commits into from
Jun 15, 2024
Merged

ctl::string cleanup #1215

merged 2 commits into from
Jun 15, 2024

Conversation

mrdomino
Copy link
Collaborator

@mrdomino mrdomino commented Jun 15, 2024

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 *.

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 *.
@mrdomino
Copy link
Collaborator Author

mrdomino commented Jun 15, 2024

master:

   10.1243 ns 10000000x { ctl::string s; s.append("hello "); s.append("world"); }
   3.79312 ns 1000000x { ctl::string s; for (int i = 0; i < 8; ++i) { s.append('a'); } }
   4.20162 ns 1000000x { ctl::string s; for (int i = 0; i < 16; ++i) { s.append('a'); } }
   4.03643 ns 1000000x { ctl::string s; for (int i = 0; i < 23; ++i) { s.append('a'); } }
   4.89956 ns 1000000x { ctl::string s; for (int i = 0; i < 32; ++i) { s.append('a'); } }
     7.245 ns 1000000x { ctl::string s("hello world"); }
     6.634 ns 1000000x { ctl::string s2(s); }
     8.184 ns 1000000x { ctl::string s("hello world"); ctl::string s2(std::move(s)); }
    13.006 ns 1000000x { ctl::string s("hello world"); ctl::string s2(s); }
     6.717 ns 1000000x { ctl::string s(23, 'a'); }
    16.947 ns 1000000x { ctl::string s(24, 'a'); }
     1.312 ns 1000000x { ctl::string_view s2(s); }

sso-cleanup:

    8.2435 ns 10000000x { ctl::string s; s.append("hello "); s.append("world"); }
   3.67887 ns 1000000x { ctl::string s; for (int i = 0; i < 8; ++i) { s.append('a'); } }
   3.72369 ns 1000000x { ctl::string s; for (int i = 0; i < 16; ++i) { s.append('a'); } }
   3.79352 ns 1000000x { ctl::string s; for (int i = 0; i < 23; ++i) { s.append('a'); } }
   3.66553 ns 1000000x { ctl::string s; for (int i = 0; i < 32; ++i) { s.append('a'); } }
     8.242 ns 1000000x { ctl::string s("hello world"); }
     5.534 ns 1000000x { ctl::string s2(s); }
     7.173 ns 1000000x { ctl::string s("hello world"); ctl::string s2(std::move(s)); }
    10.605 ns 1000000x { ctl::string s("hello world"); ctl::string s2(s); }
     5.856 ns 1000000x { ctl::string s(23, 'a'); }
    14.457 ns 1000000x { ctl::string s(24, 'a'); }
     1.327 ns 1000000x { ctl::string_view s2(s); }

@mrdomino mrdomino requested a review from jart June 15, 2024 17:31
ctl/string.cc Outdated Show resolved Hide resolved
@mrdomino mrdomino merged commit 330037d into jart:master Jun 15, 2024
6 checks passed
@mrdomino mrdomino deleted the sso-cleanup branch June 15, 2024 17:45
mrdomino added a commit that referenced this pull request Jun 15, 2024
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 *.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants