From 6f835c3699f136d66f8f4baacf2b95589cf32257 Mon Sep 17 00:00:00 2001 From: Clemens Wasser Date: Sat, 22 Jan 2022 10:44:48 +0100 Subject: cmOutputConverter: Cache Short Paths Cache the Short Paths since we only convert the same few paths anyway and calling `GetShortPathNameW` is really expensive. Also, compile the code path only on Windows hosts since it only runs when using a Windows Shell anyway. --- Source/cmOutputConverter.cxx | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/Source/cmOutputConverter.cxx b/Source/cmOutputConverter.cxx index 4503038..b143170 100644 --- a/Source/cmOutputConverter.cxx +++ b/Source/cmOutputConverter.cxx @@ -8,6 +8,11 @@ #include #include +#ifdef _WIN32 +# include +# include +#endif + #include "cmState.h" #include "cmStateDirectory.h" #include "cmStringAlgorithms.h" @@ -117,17 +122,34 @@ std::string cmOutputConverter::MaybeRelativeToCurBinDir( std::string cmOutputConverter::ConvertToOutputForExisting( const std::string& remote, OutputFormat format) const { +#ifdef _WIN32 + // Cache the Short Paths since we only convert the same few paths anyway and + // calling `GetShortPathNameW` is really expensive. + static std::unordered_map shortPathCache{}; + // If this is a windows shell, the result has a space, and the path // already exists, we can use a short-path to reference it without a // space. if (this->GetState()->UseWindowsShell() && remote.find_first_of(" #") != std::string::npos && cmSystemTools::FileExists(remote)) { - std::string tmp; - if (cmSystemTools::GetShortPath(remote, tmp)) { - return this->ConvertToOutputFormat(tmp, format); - } + + std::string shortPath = [&]() { + auto cachedShortPathIt = shortPathCache.find(remote); + + if (cachedShortPathIt != shortPathCache.end()) { + return cachedShortPathIt->second; + } + + std::string tmp{}; + cmSystemTools::GetShortPath(remote, tmp); + shortPathCache[remote] = tmp; + return tmp; + }(); + + return this->ConvertToOutputFormat(shortPath, format); } +#endif // Otherwise, perform standard conversion. return this->ConvertToOutputFormat(remote, format); -- cgit v0.12