diff options
author | Jan Niklas Hasse <jhasse@bixense.com> | 2018-11-14 16:50:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-14 16:50:40 (GMT) |
commit | f95f51bc6ee6c8a0fdf5c913b902778d5f2c5a44 (patch) | |
tree | 6112a9661eab22cee8255955290cf7f3b88e93a5 | |
parent | a63fb13322aac3ca56e008757eb0514d96549ab5 (diff) | |
parent | c9b5eaa55231612aeff85385033a792949162228 (diff) | |
download | Ninja-f95f51bc6ee6c8a0fdf5c913b902778d5f2c5a44.zip Ninja-f95f51bc6ee6c8a0fdf5c913b902778d5f2c5a44.tar.gz Ninja-f95f51bc6ee6c8a0fdf5c913b902778d5f2c5a44.tar.bz2 |
Merge pull request #1196 from danw/ReadFile_opt
Optimize ReadFile allocations
-rw-r--r-- | src/util.cc | 12 |
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)) { |