From: Joel Rosdahl Date: Tue, 6 Oct 2020 12:47:57 +0000 (+0200) Subject: Avoid unnecessary stat calls in Util::make_relative_path X-Git-Tag: v4.0~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2bdf962b1e6151efa24cfa6945de34a613e9be8e;p=thirdparty%2Fccache.git Avoid unnecessary stat calls in Util::make_relative_path --- diff --git a/src/Util.cpp b/src/Util.cpp index 0a579952f..54519f7cd 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -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 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) {