From dd6dfafabf6aab88d4a839b5a2877a48c09f3667 Mon Sep 17 00:00:00 2001 From: Automatic Release Builder Date: Wed, 7 Oct 2020 12:21:49 +0100 Subject: [PATCH] SGPath: optimise exists() on Windows For a very common existence only check, use a dedicated win32 API function, to save some time. --- simgear/misc/sg_path.cxx | 23 ++++++++++++++++++++--- simgear/misc/sg_path.hxx | 1 + 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/simgear/misc/sg_path.cxx b/simgear/misc/sg_path.cxx index 48d6b638..9e74bafc 100644 --- a/simgear/misc/sg_path.cxx +++ b/simgear/misc/sg_path.cxx @@ -44,6 +44,7 @@ #if defined(SG_WINDOWS) # include # include +# include #endif #include "sg_path.hxx" @@ -194,7 +195,8 @@ SGPath::SGPath(PermissionChecker validator) _permission_checker(validator), _cached(false), _rwCached(false), - _cacheEnabled(true) + _cacheEnabled(true), + _existsCached(false) { } @@ -205,7 +207,8 @@ SGPath::SGPath( const std::string& p, PermissionChecker validator ) _permission_checker(validator), _cached(false), _rwCached(false), - _cacheEnabled(true) + _cacheEnabled(true), + _existsCached(false) { fix(); } @@ -230,7 +233,8 @@ SGPath::SGPath( const SGPath& p, _permission_checker(validator), _cached(false), _rwCached(false), - _cacheEnabled(p._cacheEnabled) + _cacheEnabled(p._cacheEnabled), + _existsCached(false) { append(r); fix(); @@ -510,6 +514,19 @@ void SGPath::checkAccess() 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(); return _exists; } diff --git a/simgear/misc/sg_path.hxx b/simgear/misc/sg_path.hxx index af37d6ad..205759a1 100644 --- a/simgear/misc/sg_path.hxx +++ b/simgear/misc/sg_path.hxx @@ -356,6 +356,7 @@ private: mutable bool _exists : 1; mutable bool _isDir : 1; mutable bool _isFile : 1; + mutable bool _existsCached : 1; ///< only used on Windows mutable time_t _modTime; mutable size_t _size; };