Merge pull request #14716 from CookiePLMonster/startup-speed-optimizations-2

More startup speed optimizations
This commit is contained in:
Michael Steenbeek 2021-05-24 12:42:12 +02:00 committed by GitHub
commit ea47c3ffe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 13 deletions

View File

@ -24,7 +24,7 @@
- Fix: [#14682] Crash when painting Swinging Ships with invalid subtype. - Fix: [#14682] Crash when painting Swinging Ships with invalid subtype.
- Fix: [#14707] Crash when window is closed during text input. - Fix: [#14707] Crash when window is closed during text input.
- Fix: [#14710] Ride/Track Design preview does not show if it costs more money than available. - Fix: [#14710] Ride/Track Design preview does not show if it costs more money than available.
- Improved: [#14712]: Improve startup times. - Improved: [#14712, #14716]: Improve startup times.
0.3.3 (2021-03-13) 0.3.3 (2021-03-13)
------------------------------------------------------------------------ ------------------------------------------------------------------------

View File

@ -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 * For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
@ -10,6 +10,7 @@
#include "SawyerChunk.h" #include "SawyerChunk.h"
#include "../core/Memory.hpp" #include "../core/Memory.hpp"
#include "SawyerChunkReader.h"
SawyerChunk::SawyerChunk(SAWYER_ENCODING encoding, void* data, size_t length) SawyerChunk::SawyerChunk(SAWYER_ENCODING encoding, void* data, size_t length)
{ {
@ -20,5 +21,5 @@ SawyerChunk::SawyerChunk(SAWYER_ENCODING encoding, void* data, size_t length)
SawyerChunk::~SawyerChunk() SawyerChunk::~SawyerChunk()
{ {
Memory::Free(_data); SawyerChunkReader::FreeChunk(_data);
} }

View File

@ -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 * For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
@ -27,8 +27,9 @@ constexpr const char* EXCEPTION_MSG_DESTINATION_TOO_SMALL = "Chunk data larger t
constexpr const char* EXCEPTION_MSG_INVALID_CHUNK_ENCODING = "Invalid chunk encoding."; constexpr const char* EXCEPTION_MSG_INVALID_CHUNK_ENCODING = "Invalid chunk encoding.";
constexpr const char* EXCEPTION_MSG_ZERO_SIZED_CHUNK = "Encountered zero-sized chunk."; constexpr const char* EXCEPTION_MSG_ZERO_SIZED_CHUNK = "Encountered zero-sized chunk.";
SawyerChunkReader::SawyerChunkReader(OpenRCT2::IStream* stream) SawyerChunkReader::SawyerChunkReader(OpenRCT2::IStream* stream, bool persistentChunks)
: _stream(stream) : _stream(stream)
, _createsPersistentChunks(persistentChunks)
{ {
} }
@ -78,7 +79,10 @@ std::shared_ptr<SawyerChunk> SawyerChunkReader::ReadChunk()
{ {
throw SawyerChunkException(EXCEPTION_MSG_ZERO_SIZED_CHUNK); throw SawyerChunkException(EXCEPTION_MSG_ZERO_SIZED_CHUNK);
} }
buffer = static_cast<uint8_t*>(FinaliseLargeTempBuffer(buffer, uncompressedLength)); if (_createsPersistentChunks)
{
buffer = static_cast<uint8_t*>(FinaliseLargeTempBuffer(buffer, uncompressedLength));
}
return std::make_shared<SawyerChunk>( return std::make_shared<SawyerChunk>(
static_cast<SAWYER_ENCODING>(header.encoding), buffer, uncompressedLength); static_cast<SAWYER_ENCODING>(header.encoding), buffer, uncompressedLength);
} }
@ -126,7 +130,10 @@ std::shared_ptr<SawyerChunk> SawyerChunkReader::ReadChunkTrack()
{ {
throw SawyerChunkException(EXCEPTION_MSG_ZERO_SIZED_CHUNK); throw SawyerChunkException(EXCEPTION_MSG_ZERO_SIZED_CHUNK);
} }
buffer = static_cast<uint8_t*>(FinaliseLargeTempBuffer(buffer, uncompressedLength)); if (_createsPersistentChunks)
{
buffer = static_cast<uint8_t*>(FinaliseLargeTempBuffer(buffer, uncompressedLength));
}
return std::make_shared<SawyerChunk>(SAWYER_ENCODING::RLE, buffer, uncompressedLength); return std::make_shared<SawyerChunk>(SAWYER_ENCODING::RLE, buffer, uncompressedLength);
} }
catch (const std::exception&) catch (const std::exception&)
@ -158,6 +165,11 @@ void SawyerChunkReader::ReadChunk(void* dst, size_t length)
} }
} }
void SawyerChunkReader::FreeChunk(void* data)
{
FreeLargeTempBuffer(data);
}
size_t SawyerChunkReader::DecodeChunk(void* dst, size_t dstCapacity, const void* src, const sawyercoding_chunk_header& header) size_t SawyerChunkReader::DecodeChunk(void* dst, size_t dstCapacity, const void* src, const sawyercoding_chunk_header& header)
{ {
size_t resultLength; size_t resultLength;
@ -315,9 +327,7 @@ void* SawyerChunkReader::AllocateLargeTempBuffer()
void* SawyerChunkReader::FinaliseLargeTempBuffer(void* buffer, size_t len) void* SawyerChunkReader::FinaliseLargeTempBuffer(void* buffer, size_t len)
{ {
#ifdef __USE_HEAP_ALLOC__ #ifdef __USE_HEAP_ALLOC__
auto finalBuffer = std::malloc(len); auto finalBuffer = HeapReAlloc(GetProcessHeap(), 0, buffer, len);
std::memcpy(finalBuffer, buffer, len);
HeapFree(GetProcessHeap(), 0, buffer);
#else #else
auto finalBuffer = static_cast<uint8_t*>(std::realloc(buffer, len)); auto finalBuffer = static_cast<uint8_t*>(std::realloc(buffer, len));
#endif #endif

View File

@ -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 * For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
@ -36,15 +36,17 @@ namespace OpenRCT2
/** /**
* Reads sawyer encoding chunks from a data stream. This can be used to read * Reads sawyer encoding chunks from a data stream. This can be used to read
* SC6, SV6 and RCT2 objects. * SC6, SV6 and RCT2 objects. persistentChunks is a hint to the reader that the chunk will be preserved,
* and thus the chunk memory should be shrunk.
*/ */
class SawyerChunkReader final class SawyerChunkReader final
{ {
private: private:
OpenRCT2::IStream* const _stream = nullptr; OpenRCT2::IStream* const _stream = nullptr;
const bool _createsPersistentChunks = false;
public: public:
explicit SawyerChunkReader(OpenRCT2::IStream* stream); explicit SawyerChunkReader(OpenRCT2::IStream* stream, bool persistentChunks = false);
/** /**
* Skips the next chunk in the stream without decoding or reading its data * Skips the next chunk in the stream without decoding or reading its data
@ -84,6 +86,11 @@ public:
return result; return result;
} }
/**
* Frees the chunk data, to be used when destructing SawyerChunks
*/
static void FreeChunk(void* data);
private: private:
static size_t DecodeChunk(void* dst, size_t dstCapacity, const void* src, const sawyercoding_chunk_header& header); static size_t DecodeChunk(void* dst, size_t dstCapacity, const void* src, const sawyercoding_chunk_header& header);
static size_t DecodeChunkRLERepeat(void* dst, size_t dstCapacity, const void* src, size_t srcLength); static size_t DecodeChunkRLERepeat(void* dst, size_t dstCapacity, const void* src, size_t srcLength);