summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Niklas Hasse <jhasse@bixense.com>2018-11-14 17:50:40 +0100
committerGitHub <noreply@github.com>2018-11-14 17:50:40 +0100
commitf95f51bc6ee6c8a0fdf5c913b902778d5f2c5a44 (patch)
tree6112a9661eab22cee8255955290cf7f3b88e93a5
parenta63fb13322aac3ca56e008757eb0514d96549ab5 (diff)
parentc9b5eaa55231612aeff85385033a792949162228 (diff)
downloadninja-f95f51bc6ee6c8a0fdf5c913b902778d5f2c5a44.tar.gz
Merge pull request #1196 from danw/ReadFile_opt
Optimize ReadFile allocations
-rw-r--r--src/util.cc12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/util.cc b/src/util.cc
index 7bfe033..e793a92 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -346,9 +346,19 @@ int ReadFile(const string& path, string* contents, string* err) {
return -errno;
}
+ struct stat st;
+ if (fstat(fileno(f), &st) < 0) {
+ err->assign(strerror(errno));
+ fclose(f);
+ return -errno;
+ }
+
+ // +1 is for the resize in ManifestParser::Load
+ contents->reserve(st.st_size + 1);
+
char buf[64 << 10];
size_t len;
- while ((len = fread(buf, 1, sizeof(buf), f)) > 0) {
+ while (!feof(f) && (len = fread(buf, 1, sizeof(buf), f)) > 0) {
contents->append(buf, len);
}
if (ferror(f)) {