Skip to content

Commit

Permalink
Micro-optimize file::read (sorbet#1625)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkDimius authored Aug 18, 2019
1 parent b3a86a8 commit 357c33d
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions common/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ bool sorbet::FileOps::exists(string_view filename) {
string sorbet::FileOps::read(string_view filename) {
FILE *fp = std::fopen((string(filename)).c_str(), "rb");
if (fp) {
string contents;
fseek(fp, 0, SEEK_END);
contents.resize(ftell(fp));
auto sz = ftell(fp);
string contents(sz, '\0');
rewind(fp);
auto readBytes = fread(&contents[0], 1, contents.size(), fp);
auto readBytes = fread(&contents[0], 1, sz, fp);
fclose(fp);
if (readBytes != contents.size()) {
// Error reading file?
Expand Down

0 comments on commit 357c33d

Please sign in to comment.