From 8c253d4d42018d77b78de8f8c70c6204f5aa6d0e Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 29 Apr 2020 14:10:12 -0700 Subject: [PATCH] Avoid zero-initializing our most-used buffers. The StringPrintf one is heavily used and brings the overhead versus fmtlib down to 1.5x rather than 2x. I don't have a convenient benchmark for the other two. Test: libbase tests & benchmarks Bug: http://b/155324241 Change-Id: I9e704a360846d5520c53f668e7c315b0c0ea55f8 --- base/file.cpp | 2 +- base/logging.cpp | 2 +- base/stringprintf.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/base/file.cpp b/base/file.cpp index 6321fc624..97cc2b27f 100644 --- a/base/file.cpp +++ b/base/file.cpp @@ -225,7 +225,7 @@ bool ReadFdToString(borrowed_fd fd, std::string* content) { content->reserve(sb.st_size); } - char buf[BUFSIZ]; + char buf[BUFSIZ] __attribute__((__uninitialized__)); ssize_t n; while ((n = TEMP_FAILURE_RETRY(read(fd.get(), &buf[0], sizeof(buf)))) > 0) { content->append(buf, n); diff --git a/base/logging.cpp b/base/logging.cpp index 6e9c67fc4..5bd21da66 100644 --- a/base/logging.cpp +++ b/base/logging.cpp @@ -260,7 +260,7 @@ static void KernelLogLine(const char* msg, int length, android::base::LogSeverit // The kernel's printk buffer is only 1024 bytes. // TODO: should we automatically break up long lines into multiple lines? // Or we could log but with something like "..." at the end? - char buf[1024]; + char buf[1024] __attribute__((__uninitialized__)); size_t size = snprintf(buf, sizeof(buf), "<%d>%s: %.*s\n", level, tag, length, msg); if (size > sizeof(buf)) { size = snprintf(buf, sizeof(buf), "<%d>%s: %zu-byte message too long for printk\n", diff --git a/base/stringprintf.cpp b/base/stringprintf.cpp index 78e1e8d14..e83ab1316 100644 --- a/base/stringprintf.cpp +++ b/base/stringprintf.cpp @@ -25,7 +25,7 @@ namespace base { void StringAppendV(std::string* dst, const char* format, va_list ap) { // First try with a small fixed size buffer - char space[1024]; + char space[1024] __attribute__((__uninitialized__)); // It's possible for methods that use a va_list to invalidate // the data in it upon use. The fix is to make a copy