diff options
author | Abseil Team <absl-team@google.com> | 2024-01-04 13:14:50 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-01-04 13:15:44 -0800 |
commit | d5a2cec006d14c6801ddeb768bf2574a1cf4fa7f (patch) | |
tree | 3dc1ffb00d164ddda4c22b8abfc42594db1fa2da /absl/strings/numbers_test.cc | |
parent | ccf0c7730db976d2b7814e74f6f01d044ae6f853 (diff) | |
download | abseil-d5a2cec006d14c6801ddeb768bf2574a1cf4fa7f.tar.gz abseil-d5a2cec006d14c6801ddeb768bf2574a1cf4fa7f.tar.bz2 abseil-d5a2cec006d14c6801ddeb768bf2574a1cf4fa7f.zip |
Optimize integer-to-string conversions
The updated code is designed to:
- Be branch-predictor-friendly
- Be cache-friendly
- Minimize the lengths of critical paths
- Minimize slow operations (particularly multiplications)
- Minimize binary/codegen bloat
The most notable performance trick here is perhaps the precomputation & caching of the number of digits, so that we can reuse/exploit it when writing the output.
This precomputation of the exact length enables 2 further performance benefits:
- It makes `StrCat` and `StrAppend` zero-copy when only integers are passed, by avoiding intermediate `AlphaNum` entirely in those cases. If needed in the future, we can probably also make many other mixtures of non-integer types zero-copy as well.
- It avoids over-reservation of the string buffer, allowing for more strings to fit inside SSO, which will likely have further performance benefits.
There is also a side benefit of preventing `FastIntToBuffer` from writing beyond the end of the buffer, which has caused buffer overflows in the past.
The new code continues to use & extend some of the existing core tricks (such as the division-by-100 trick), as those are already efficient.
PiperOrigin-RevId: 595785531
Change-Id: Id6920e7e038fec10b2c45f213de75dc7e2cbddd1
Diffstat (limited to 'absl/strings/numbers_test.cc')
-rw-r--r-- | absl/strings/numbers_test.cc | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/absl/strings/numbers_test.cc b/absl/strings/numbers_test.cc index 75c2dcf2..1ceff70f 100644 --- a/absl/strings/numbers_test.cc +++ b/absl/strings/numbers_test.cc @@ -231,10 +231,15 @@ TEST(Numbers, TestFastPrints) { CheckInt32(INT_MIN); CheckInt32(INT_MAX); CheckInt64(LONG_MIN); + CheckInt64(uint64_t{10000000}); + CheckInt64(uint64_t{100000000}); CheckInt64(uint64_t{1000000000}); CheckInt64(uint64_t{9999999999}); CheckInt64(uint64_t{100000000000000}); CheckInt64(uint64_t{999999999999999}); + CheckInt64(uint64_t{1000000000000000}); + CheckInt64(uint64_t{10000000000000000}); + CheckInt64(uint64_t{100000000000000000}); CheckInt64(uint64_t{1000000000000000000}); CheckInt64(uint64_t{1199999999999999999}); CheckInt64(int64_t{-700000000000000000}); @@ -246,6 +251,8 @@ TEST(Numbers, TestFastPrints) { CheckUInt64(uint64_t{999999999999999}); CheckUInt64(uint64_t{1000000000000000000}); CheckUInt64(uint64_t{1199999999999999999}); + CheckUInt64(uint64_t{10000000000000000000u}); + CheckUInt64(uint64_t{10200300040000500006u}); CheckUInt64(std::numeric_limits<uint64_t>::max()); for (int i = 0; i < 10000; i++) { |