From b30f16d7c9a832ed09b5cd03d73f343f933ca872 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Wed, 28 Mar 2018 19:21:50 +0500 Subject: Minor Section optimizations --- src/Section.cpp | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) (limited to 'src/Section.cpp') diff --git a/src/Section.cpp b/src/Section.cpp index 1f60471..c8c67dc 100644 --- a/src/Section.cpp +++ b/src/Section.cpp @@ -13,17 +13,12 @@ void Section::CalculateHash() const { size_t offset = 0; std::vector rawData; - rawData.resize(block.size() * sizeof(long long) + light.size() + sky.size()); + rawData.resize(block.size() * sizeof(long long) + 4096); - std::memcpy(rawData.data() + offset, block.data(), block.size() * sizeof(BlockId)); - offset += block.size() * sizeof(BlockId); - - std::memcpy(rawData.data() + offset, light.data(), light.size() * sizeof(unsigned char)); - offset += light.size() * sizeof(unsigned char); - - if (!sky.empty()) - std::memcpy(rawData.data() + offset, sky.data(), sky.size() * sizeof(unsigned char)); - + std::memcpy(rawData.data(), light, 2048); + std::memcpy(rawData.data() + 2048, sky, 2048); + std::memcpy(rawData.data() + 4096, block.data(), block.size() * sizeof(long long)); + for (auto& it : overrideList) { rawData.push_back(*reinterpret_cast (&it.second) & 0xF); rawData.push_back(*reinterpret_cast (&it.second) >> 0xF); @@ -36,7 +31,7 @@ void Section::CalculateHash() const { hash = std::hash{}(str); } -Section::Section(Vector pos, unsigned char bitsPerBlock, std::vector palette, std::vector blockData, std::vector lightData, std::vector skyData) { +Section::Section(Vector pos, unsigned char bitsPerBlock, std::vector palette, std::vector blockData, const std::vector &lightData, const std::vector &skyData) { if (bitsPerBlock < 4) bitsPerBlock = 4; if (bitsPerBlock > 8) @@ -46,8 +41,8 @@ Section::Section(Vector pos, unsigned char bitsPerBlock, std::vectorworldPosition = pos; this->block = std::move(blockData); this->palette = std::move(palette); - this->light = std::move(lightData); - this->sky = std::move(skyData); + std::copy(lightData.begin(), lightData.end(), light); + std::copy(skyData.begin(), skyData.end(), sky); hash = -1; } @@ -99,9 +94,6 @@ BlockId Section::GetBlockId(Vector pos) const { unsigned char Section::GetBlockLight(Vector pos) const { - if (light.empty()) - return 0; - int blockNumber = pos.y * 256 + pos.z * 16 + pos.x; unsigned char lightValue = this->light[blockNumber / 2]; return (blockNumber % 2 == 0) ? (lightValue & 0xF) : (lightValue >> 4); @@ -109,10 +101,7 @@ unsigned char Section::GetBlockLight(Vector pos) const unsigned char Section::GetBlockSkyLight(Vector pos) const { - if (sky.empty()) - return 0; - - int blockNumber = pos.y * 256 + pos.z * 16 + pos.x; + int blockNumber = pos.y * 256 + pos.z * 16 + pos.x; unsigned char skyValue = this->sky[blockNumber / 2]; return (blockNumber % 2 == 0) ? (skyValue & 0xF) : (skyValue >> 4); } -- cgit v1.2.3