Keep the ReadFileToString/ReadFdToString overhead down.
Bug: https://code.google.com/p/android/issues/detail?id=258500
Bug: http://b/36046324
Test: ran tests
Change-Id: I40e76a6dd164ea9a5e8e18159f543e1bb221dcba
diff --git a/base/file.cpp b/base/file.cpp
index 378a405..d4e5894 100644
--- a/base/file.cpp
+++ b/base/file.cpp
@@ -49,6 +49,14 @@
bool ReadFdToString(int fd, std::string* content) {
content->clear();
+ // Although original we had small files in mind, this code gets used for
+ // very large files too, where the std::string growth heuristics might not
+ // be suitable. https://code.google.com/p/android/issues/detail?id=258500.
+ struct stat sb;
+ if (fstat(fd, &sb) != -1 && sb.st_size > 0) {
+ content->reserve(sb.st_size);
+ }
+
char buf[BUFSIZ];
ssize_t n;
while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) {