Skip to content

Commit

Permalink
Stop calling ftell() in a loop.
Browse files Browse the repository at this point in the history
ftell() must go ask the kernel for the file offset, in case
someone knew the underlying file descriptor number and seeked it.
Thus, we can save a couple hundred thousand syscalls by just
caching the offset and maintaining it ourselves.

This cuts another ~170ms off a no-op Chromium build.
  • Loading branch information
Steinar H. Gunderson authored and sesse committed Nov 21, 2024
1 parent 32e7e23 commit cffec38
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/deps_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,13 @@ LoadStatus DepsLog::Load(const string& path, State* state, string* err) {
return LOAD_SUCCESS;
}

long offset;
long offset = ftell(f);
bool read_failed = false;
int unique_dep_record_count = 0;
int total_dep_record_count = 0;
for (;;) {
offset = ftell(f);

unsigned size;
if (fread(&size, 4, 1, f) < 1) {
if (fread(&size, sizeof(size), 1, f) < 1) {
if (!feof(f))
read_failed = true;
break;
Expand All @@ -206,6 +204,7 @@ LoadStatus DepsLog::Load(const string& path, State* state, string* err) {
read_failed = true;
break;
}
offset += size + sizeof(size);

if (is_deps) {
if ((size % 4) != 0) {
Expand Down

0 comments on commit cffec38

Please sign in to comment.