]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Avoid unnecessary stat calls in Util::make_relative_path
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 6 Oct 2020 12:47:57 +0000 (14:47 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 6 Oct 2020 12:47:57 +0000 (14:47 +0200)
src/Util.cpp

index 0a579952ff8bcb9c2d82e5a0ac48e6269fded2d3..54519f7cdbb6ce4e037bac83f6a2bd661c5092a6 100644 (file)
@@ -868,11 +868,14 @@ make_relative_path(const Context& ctx, string_view path)
   std::string normalized_path = Util::normalize_absolute_path(path_str);
   std::vector<std::string> relpath_candidates = {
     Util::get_relative_path(ctx.actual_cwd, normalized_path),
-    Util::get_relative_path(ctx.apparent_cwd, normalized_path),
   };
-  // Move best (= shortest) match first:
-  if (relpath_candidates[0].length() > relpath_candidates[1].length()) {
-    std::swap(relpath_candidates[0], relpath_candidates[1]);
+  if (ctx.apparent_cwd != ctx.actual_cwd) {
+    relpath_candidates.emplace_back(
+      Util::get_relative_path(ctx.apparent_cwd, normalized_path));
+    // Move best (= shortest) match first:
+    if (relpath_candidates[0].length() > relpath_candidates[1].length()) {
+      std::swap(relpath_candidates[0], relpath_candidates[1]);
+    }
   }
 
   for (const auto& relpath : relpath_candidates) {