SGPath: optimise exists() on Windows

For a very common existence only check, use a dedicated win32 API
function, to save some time.
This commit is contained in:
Automatic Release Builder
2020-10-29 14:06:26 +00:00
committed by James Turner
parent 439fa4c4a1
commit dd6dfafabf
2 changed files with 21 additions and 3 deletions
+20 -3
View File
@@ -44,6 +44,7 @@
#if defined(SG_WINDOWS) #if defined(SG_WINDOWS)
# include <direct.h> # include <direct.h>
# include <sys/utime.h> # include <sys/utime.h>
# include <Shlwapi.h>
#endif #endif
#include "sg_path.hxx" #include "sg_path.hxx"
@@ -194,7 +195,8 @@ SGPath::SGPath(PermissionChecker validator)
_permission_checker(validator), _permission_checker(validator),
_cached(false), _cached(false),
_rwCached(false), _rwCached(false),
_cacheEnabled(true) _cacheEnabled(true),
_existsCached(false)
{ {
} }
@@ -205,7 +207,8 @@ SGPath::SGPath( const std::string& p, PermissionChecker validator )
_permission_checker(validator), _permission_checker(validator),
_cached(false), _cached(false),
_rwCached(false), _rwCached(false),
_cacheEnabled(true) _cacheEnabled(true),
_existsCached(false)
{ {
fix(); fix();
} }
@@ -230,7 +233,8 @@ SGPath::SGPath( const SGPath& p,
_permission_checker(validator), _permission_checker(validator),
_cached(false), _cached(false),
_rwCached(false), _rwCached(false),
_cacheEnabled(p._cacheEnabled) _cacheEnabled(p._cacheEnabled),
_existsCached(false)
{ {
append(r); append(r);
fix(); fix();
@@ -510,6 +514,19 @@ void SGPath::checkAccess() const
bool SGPath::exists() const bool SGPath::exists() const
{ {
#if defined(SG_WINDOWS)
// optimisation: _wstat is slow, eg for TerraSync
if (!_cached && !_existsCached) {
std::wstring w(wstr());
if ((path.length() > 1) && (path.back() == '/')) {
w.pop_back();
}
_existsCached = true;
_exists = PathFileExistsW(w.c_str());
return _exists;
}
#endif
validate(); validate();
return _exists; return _exists;
} }
+1
View File
@@ -356,6 +356,7 @@ private:
mutable bool _exists : 1; mutable bool _exists : 1;
mutable bool _isDir : 1; mutable bool _isDir : 1;
mutable bool _isFile : 1; mutable bool _isFile : 1;
mutable bool _existsCached : 1; ///< only used on Windows
mutable time_t _modTime; mutable time_t _modTime;
mutable size_t _size; mutable size_t _size;
}; };