diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2017-03-22 20:59:59 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-03-22 21:00:00 +0000 |
commit | 2af784bf202085d0ed03497f596b4447f37bd25c (patch) | |
tree | dc3bd3014a39d6d87831b51d2686007b245b31e3 /base/file.cpp | |
parent | 437dc91fa39803607ddc83ce52ee40ce3208696a (diff) | |
parent | 9bb7971ae794ecfc6dcaab28d13d3ade8ab18517 (diff) |
Merge "Keep the ReadFileToString/ReadFdToString overhead down."
Diffstat (limited to 'base/file.cpp')
-rw-r--r-- | base/file.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/base/file.cpp b/base/file.cpp index 378a405bc..d4e58942c 100644 --- a/base/file.cpp +++ b/base/file.cpp @@ -49,6 +49,14 @@ using namespace android::base::utf8; 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) { |