summaryrefslogtreecommitdiff
path: root/Source/cmOutputConverter.cxx
diff options
context:
space:
mode:
authorClemens Wasser <clemens.wasser@gmail.com>2022-01-22 10:44:48 +0100
committerBrad King <brad.king@kitware.com>2022-01-25 04:27:15 -0500
commit6f835c3699f136d66f8f4baacf2b95589cf32257 (patch)
tree3af5bccbd629e76b37921e7b69789cd81f237c6d /Source/cmOutputConverter.cxx
parent850bdc4203f4599fceaed2ae9ba21c53e71fd0fb (diff)
downloadcmake-6f835c3699f136d66f8f4baacf2b95589cf32257.tar.gz
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.
Diffstat (limited to 'Source/cmOutputConverter.cxx')
-rw-r--r--Source/cmOutputConverter.cxx30
1 files changed, 26 insertions, 4 deletions
diff --git a/Source/cmOutputConverter.cxx b/Source/cmOutputConverter.cxx
index 4503038d1d..b1431709f6 100644
--- a/Source/cmOutputConverter.cxx
+++ b/Source/cmOutputConverter.cxx
@@ -8,6 +8,11 @@
#include <set>
#include <vector>
+#ifdef _WIN32
+# include <unordered_map>
+# include <utility>
+#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<std::string, std::string> 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);