]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Avoid duplicate stat calls (#550)
authorErik Johansson <erik@ejohansson.se>
Sun, 1 Mar 2020 20:17:49 +0000 (21:17 +0100)
committerGitHub <noreply@github.com>
Sun, 1 Mar 2020 20:17:49 +0000 (21:17 +0100)
hash_source_code_string will, in read_file, stat the file to get the file
size. But when called from verify_result, the file size is already known so by
using this size the number of stat calls in a normal run can be cut in half.

src/hashutil.cpp
src/hashutil.hpp
src/manifest.cpp

index f8ac485350b06a4686ca7c2808fcc46d4a3cf0f3..81f545c6ee9425779498d7a8f6f477cfa64f8a7f 100644 (file)
@@ -264,7 +264,10 @@ hash_source_code_string(const Config& config,
 // Hash a file ignoring comments. Returns a bitmask of HASH_SOURCE_CODE_*
 // results.
 int
-hash_source_code_file(const Config& config, struct hash* hash, const char* path)
+hash_source_code_file(const Config& config,
+                      struct hash* hash,
+                      const char* path,
+                      size_t size_hint)
 {
   if (is_precompiled_header(path)) {
     if (hash_file(hash, path)) {
@@ -275,7 +278,7 @@ hash_source_code_file(const Config& config, struct hash* hash, const char* path)
   } else {
     char* data;
     size_t size;
-    if (!read_file(path, 0, &data, &size)) {
+    if (!read_file(path, size_hint, &data, &size)) {
       return HASH_SOURCE_CODE_ERROR;
     }
     int result = hash_source_code_string(config, hash, data, size, path);
index a6299186b8d3b46a98443f319879459123346dd0..0f3cfc74712f632e0da1f773840b5fc2d0670e2f 100644 (file)
@@ -43,7 +43,8 @@ int hash_source_code_string(const Config& config,
                             const char* path);
 int hash_source_code_file(const Config& config,
                           struct hash* hash,
-                          const char* path);
+                          const char* path,
+                          size_t size_hint = 0);
 bool hash_command_output(struct hash* hash,
                          const char* command,
                          const char* compiler);
index 961c62681eac23f1b6f55a37281f9729216cd772..67410ffe9e7b1c845aa4d39e0efd290c176ecf5c 100644 (file)
@@ -447,7 +447,7 @@ verify_result(const Context& ctx,
     auto hashed_files_iter = hashed_files.find(path);
     if (hashed_files_iter == hashed_files.end()) {
       struct hash* hash = hash_init();
-      int ret = hash_source_code_file(ctx.config, hash, path.c_str());
+      int ret = hash_source_code_file(ctx.config, hash, path.c_str(), fs.size);
       if (ret & HASH_SOURCE_CODE_ERROR) {
         cc_log("Failed hashing %s", path.c_str());
         hash_free(hash);