Skip to content

Commit d5a2cec

Browse files
Abseil Teamcopybara-github
Abseil Team
authored andcommitted
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
1 parent ccf0c77 commit d5a2cec

File tree

7 files changed

+759
-212
lines changed

7 files changed

+759
-212
lines changed

absl/base/macros.h

-12
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,4 @@ ABSL_NAMESPACE_END
138138
#define ABSL_INTERNAL_RETHROW do {} while (false)
139139
#endif // ABSL_HAVE_EXCEPTIONS
140140

141-
// Requires the compiler to prove that the size of the given object is at least
142-
// the expected amount.
143-
#if ABSL_HAVE_ATTRIBUTE(diagnose_if) && ABSL_HAVE_BUILTIN(__builtin_object_size)
144-
#define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N) \
145-
__attribute__((diagnose_if(__builtin_object_size(Obj, 0) < N, \
146-
"object size provably too small " \
147-
"(this would corrupt memory)", \
148-
"error")))
149-
#else
150-
#define ABSL_INTERNAL_NEED_MIN_SIZE(Obj, N)
151-
#endif
152-
153141
#endif // ABSL_BASE_MACROS_H_

0 commit comments

Comments
 (0)