]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
process_preprocessed_file: Move gnu_getcwd() out of tight loop
authorMike Gulick <mgulick@mathworks.com>
Tue, 24 Jul 2018 19:23:03 +0000 (15:23 -0400)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 18 Aug 2018 20:31:01 +0000 (22:31 +0200)
Change process_preprocessed_file from calling getcwd() once per line
in the preprocessed source file to once at the start of the function.
The performance of getcwd() on Mac seems to be terrible compared to
Linux.  On a macOS 10.13 build machine, this change improves
process_preprocessed_file runtime on a 10MB preprocessed file from 75
seconds to .75 seconds.

src/ccache.c

index 7a094e950bfa71044c2e84421c3ba8234fbf754f..e3b8886ff03c22dd25f31e9980cd71fcac5b6fd3 100644 (file)
@@ -781,6 +781,11 @@ process_preprocessed_file(struct mdfour *hash, const char *path, bool pump)
                included_files = create_hashtable(1000, hash_from_string, strings_equal);
        }
 
+       char *cwd = NULL;
+       if (!conf->hash_dir) {
+               cwd = gnu_getcwd();
+       }
+
        // Bytes between p and q are pending to be hashed.
        char *p = data;
        char *q = data;
@@ -880,7 +885,6 @@ process_preprocessed_file(struct mdfour *hash, const char *path, bool pump)
 
                        bool should_hash_inc_path = true;
                        if (!conf->hash_dir) {
-                               char *cwd = gnu_getcwd();
                                if (str_startswith(inc_path, cwd) && str_endswith(inc_path, "//")) {
                                        // When compiling with -g or similar, GCC adds the absolute path to
                                        // CWD like this:
@@ -891,7 +895,6 @@ process_preprocessed_file(struct mdfour *hash, const char *path, bool pump)
                                        // hash it. See also how debug_prefix_map is handled.
                                        should_hash_inc_path = false;
                                }
-                               free(cwd);
                        }
                        if (should_hash_inc_path) {
                                hash_string(hash, inc_path);
@@ -928,6 +931,7 @@ process_preprocessed_file(struct mdfour *hash, const char *path, bool pump)
 
        hash_buffer(hash, p, (end - p));
        free(data);
+       free(cwd);
 
        // Explicitly check the .gch/.pch/.pth file, Clang does not include any
        // mention of it in the preprocessed output.