From 95388cfbb1f1f06cb9eee719e8633544b8e8d509 Mon Sep 17 00:00:00 2001 From: Silent Date: Fri, 21 May 2021 16:55:07 +0200 Subject: [PATCH] Add File::GetSize to get object size in a simpler way Drastically reduces startup speed because of the optimization applied to ObjectAsset::GetSize - now the file size is obtained without reading the entire file into memory. --- distribution/changelog.txt | 1 + src/openrct2/core/File.cpp | 7 ++++++- src/openrct2/core/File.h | 3 ++- src/openrct2/object/Object.cpp | 13 +++---------- src/openrct2/object/Object.h | 4 ++-- src/openrct2/platform/Platform.Posix.cpp | 15 ++++++++++++++- src/openrct2/platform/Platform.Win32.cpp | 17 ++++++++++++++++- src/openrct2/platform/Platform2.h | 3 ++- 8 files changed, 46 insertions(+), 17 deletions(-) diff --git a/distribution/changelog.txt b/distribution/changelog.txt index f0c2a3da24..eda7cd7591 100644 --- a/distribution/changelog.txt +++ b/distribution/changelog.txt @@ -20,6 +20,7 @@ - Fix: [#14587] Confusing message when joining server with mismatched network version. - Fix: [#14604] American-style Steam Trains are not imported correctly from RCT1 saves. - Fix: [#14638] The “About OpenRCT2” window cannot be themed. +- Improved: [#14712]: Improve startup times. 0.3.3 (2021-03-13) ------------------------------------------------------------------------ diff --git a/src/openrct2/core/File.cpp b/src/openrct2/core/File.cpp index df722e1196..99bc40b93d 100644 --- a/src/openrct2/core/File.cpp +++ b/src/openrct2/core/File.cpp @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -123,6 +123,11 @@ namespace File { return Platform::GetLastModified(path); } + + uint64_t GetSize(std::string_view path) + { + return Platform::GetFileSize(path); + } } // namespace File bool writeentirefile(const utf8* path, const void* buffer, size_t length) diff --git a/src/openrct2/core/File.h b/src/openrct2/core/File.h index 9dc216042e..cdd6179fa7 100644 --- a/src/openrct2/core/File.h +++ b/src/openrct2/core/File.h @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -26,4 +26,5 @@ namespace File std::vector ReadAllLines(std::string_view path); void WriteAllBytes(const std::string& path, const void* buffer, size_t length); uint64_t GetLastModified(const std::string& path); + uint64_t GetSize(std::string_view path); } // namespace File diff --git a/src/openrct2/object/Object.cpp b/src/openrct2/object/Object.cpp index 0d221293e5..6de569acdf 100644 --- a/src/openrct2/object/Object.cpp +++ b/src/openrct2/object/Object.cpp @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -252,18 +252,11 @@ bool ObjectAsset::IsAvailable() const } } -size_t ObjectAsset::GetSize() const +uint64_t ObjectAsset::GetSize() const { if (_zipPath.empty()) { - try - { - return File::ReadAllBytes(_path).size(); - } - catch (...) - { - return 0; - } + return File::GetSize(_path); } else { diff --git a/src/openrct2/object/Object.h b/src/openrct2/object/Object.h index 47e9ee87d5..7e3a496e86 100644 --- a/src/openrct2/object/Object.h +++ b/src/openrct2/object/Object.h @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -223,7 +223,7 @@ public: } bool IsAvailable() const; - size_t GetSize() const; + uint64_t GetSize() const; std::unique_ptr GetStream() const; }; diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index 8889749720..dc1184564d 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -177,6 +177,19 @@ namespace Platform return lastModified; } + uint64_t GetFileSize(std::string_view path) + { + uint64_t size = 0; + struct stat statInfo + { + }; + if (stat(std::string(path).c_str(), &statInfo) == 0) + { + size = statInfo.st_size; + } + return size; + } + bool ShouldIgnoreCase() { return false; diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index d7d22fbbea..4a7f203e96 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -554,6 +554,21 @@ namespace Platform return lastModified; } + uint64_t GetFileSize(std::string_view path) + { + uint64_t size = 0; + auto pathW = String::ToWideChar(path); + WIN32_FILE_ATTRIBUTE_DATA attributes; + if (GetFileAttributesExW(pathW.c_str(), GetFileExInfoStandard, &attributes) != FALSE) + { + ULARGE_INTEGER fileSize; + fileSize.LowPart = attributes.nFileSizeLow; + fileSize.HighPart = attributes.nFileSizeHigh; + size = fileSize.QuadPart; + } + return size; + } + bool ShouldIgnoreCase() { return true; diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index 6189f21b96..98d886d282 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -39,6 +39,7 @@ namespace Platform bool IsPathSeparator(char c); utf8* GetAbsolutePath(utf8* buffer, size_t bufferSize, const utf8* relativePath); uint64_t GetLastModified(const std::string& path); + uint64_t GetFileSize(std::string_view path); std::string ResolveCasing(const std::string& path, bool fileExists); rct2_time GetTimeLocal(); rct2_date GetDateLocal();