From 9d06d10a92ae0a244e209562478ea709efd0e4a5 Mon Sep 17 00:00:00 2001 From: yggdrasil75 Date: Mon, 17 Nov 2025 05:27:36 -0500 Subject: [PATCH] think I fixed the reported memory usage bug --- tests/g2chromatic2.cpp | 35 ++++++++---------- util/output/frame.hpp | 84 ++++++++++++++++++++++++++++++------------ 2 files changed, 75 insertions(+), 44 deletions(-) diff --git a/tests/g2chromatic2.cpp b/tests/g2chromatic2.cpp index 27191b0..7d46ca9 100644 --- a/tests/g2chromatic2.cpp +++ b/tests/g2chromatic2.cpp @@ -125,29 +125,24 @@ bool exportavi(std::vector frames, AnimationConfig config) { std::cout << "\n=== Frame Compression Statistics ===" << std::endl; size_t totalOriginalSize = 0; size_t totalCompressedSize = 0; - int compressedFrameCount = 0; for (int i = 0; i < frames.size(); ++i) { totalOriginalSize += frames[i].getSourceSize(); - totalCompressedSize += frames[i].getCompressedSize(); - compressedFrameCount++; + totalCompressedSize += frames[i].getTotalCompressedSize(); } - // Print summary - //if (compressedFrameCount > 0) { - double overallRatio = static_cast(totalOriginalSize) / totalCompressedSize; - double overallSavings = (1.0 - 1.0/overallRatio) * 100.0; - - std::cout << "\n=== Overall Compression Summary ===" << std::endl; - std::cout << "Total frames: " << frames.size() << std::endl; - std::cout << "Compressed frames: " << compressedFrameCount << std::endl; - std::cout << "Total original size: " << totalOriginalSize << " bytes (" - << std::fixed << std::setprecision(2) << (totalOriginalSize / (1024.0 * 1024.0)) << " MB)" << std::endl; - std::cout << "Total compressed size: " << totalCompressedSize << " bytes (" - << std::fixed << std::setprecision(2) << (totalCompressedSize / (1024.0 * 1024.0)) << " MB)" << std::endl; - std::cout << "Overall compression ratio: " << std::fixed << std::setprecision(2) << overallRatio << ":1" << std::endl; - std::cout << "Overall space savings: " << std::fixed << std::setprecision(1) << overallSavings << "%" << std::endl; - //} + double overallRatio = static_cast(totalOriginalSize) / totalCompressedSize; + double overallSavings = (1.0 - 1.0/overallRatio) * 100.0; + + std::cout << "\n=== Overall Compression Summary ===" << std::endl; + std::cout << "Total frames: " << frames.size() << std::endl; + std::cout << "Compressed frames: " << frames.size() << std::endl; + std::cout << "Total original size: " << totalOriginalSize << " bytes (" + << std::fixed << std::setprecision(2) << (totalOriginalSize / (1024.0 * 1024.0)) << " MB)" << std::endl; + std::cout << "Total compressed size: " << totalCompressedSize << " bytes (" + << std::fixed << std::setprecision(2) << (totalCompressedSize / (1024.0 * 1024.0)) << " MB)" << std::endl; + std::cout << "Overall compression ratio: " << std::fixed << std::setprecision(2) << overallRatio << ":1" << std::endl; + std::cout << "Overall space savings: " << std::fixed << std::setprecision(1) << overallSavings << "%" << std::endl; std::filesystem::path dir = "output"; if (!std::filesystem::exists(dir)) { @@ -183,12 +178,12 @@ int main() { std::vector frames; for (int i = 0; i < config.totalFrames; ++i){ - std::cout << "Processing frame " << i + 1 << "/" << config.totalFrames << std::endl; expandPixel(grid,config,seeds); - frame bgrframe; // Print compression info for this frame if (i % 10 == 0 ) { + frame bgrframe; + std::cout << "Processing frame " << i + 1 << "/" << config.totalFrames << std::endl; bgrframe = grid.getGridAsFrame(frame::colormap::BGR); bgrframe.printCompressionStats(); //(bgrframe, i + 1); diff --git a/util/output/frame.hpp b/util/output/frame.hpp index 7a9a536..e7f19cc 100644 --- a/util/output/frame.hpp +++ b/util/output/frame.hpp @@ -72,6 +72,7 @@ public: cformat = compresstype::RAW; _compressedData.clear(); _compressedData.shrink_to_fit(); + overheadmap.clear(); } const std::vector& getData() const { @@ -151,7 +152,7 @@ public: std::mutex mutex; std::vector> matches128plus; std::vector> matches64plus; - std::vector> matches32plus; + //std::vector> matches32plus; std::vector> matchesAll; void addMatch(std::vector&& match, size_t length) { @@ -160,9 +161,11 @@ public: if (matches128plus.size() < 65534) matches128plus.push_back(std::move(match)); } else if (length >= 64) { if (matches64plus.size() < 65534) matches64plus.push_back(std::move(match)); - } else if (length >= 32) { - if (matches32plus.size() < 65534) matches32plus.push_back(std::move(match)); - } else { + } + // else if (length >= 32) { + // if (matches32plus.size() < 65534) matches32plus.push_back(std::move(match)); + // } + else { if (matchesAll.size() < 65534) matchesAll.push_back(std::move(match)); } } @@ -254,15 +257,15 @@ public: else break; } - for (const auto& match : threadMatches.matches32plus) { - if (result.size() < 65534) result.push_back(match); - else break; - } + // for (const auto& match : threadMatches.matches32plus) { + // if (result.size() < 65534) result.push_back(match); + // else break; + // } - for (const auto& match : threadMatches.matchesAll) { - if (result.size() < 65534) result.push_back(match); - else break; - } + // for (const auto& match : threadMatches.matchesAll) { + // if (result.size() < 65534) result.push_back(match); + // else break; + // } return result; } @@ -389,6 +392,7 @@ public: _data = std::move(decompressedData); _compressedData.clear(); _compressedData.shrink_to_fit(); + overheadmap.clear(); cformat = compresstype::RAW; return *this; @@ -449,9 +453,25 @@ public: } } + // Calculate the size of the dictionary in bytes + size_t getDictionarySize() const { + size_t dictSize = 0; + dictSize = sizeof(overheadmap); + return dictSize; + } + + // Get compressed size including dictionary overhead + size_t getTotalCompressedSize() const { + size_t baseSize = getCompressedSize() * 2; // Convert 16-bit words to bytes + if (cformat == compresstype::LZ78) { + baseSize += getDictionarySize(); + } + return baseSize; + } + double getCompressionRatio() const { if (_compressedData.empty() || sourceSize == 0) return 0.0; - return static_cast(sourceSize) / _compressedData.size(); + return static_cast(sourceSize) / getTotalCompressedSize(); } // Get source size (uncompressed size) @@ -459,8 +479,13 @@ public: return sourceSize; } - // Get compressed size + // Get compressed size (just the compressed data in bytes, excluding dictionary) size_t getCompressedSize() const { + return _compressedData.size() * 2; // Convert 16-bit words to bytes + } + + // Get just the compressed data size in 16-bit words + size_t getCompressedDataSize() const { return _compressedData.size(); } @@ -479,26 +504,37 @@ public: std::cout << std::endl; std::cout << "Source Size: " << getSourceSize() << " bytes" << std::endl; - std::cout << "Compressed Size: " << getCompressedSize() << " 16-bit words" << std::endl; - std::cout << "Compressed Size: " << getCompressedSize() * 2 << " bytes" << std::endl; + std::cout << "Compressed data Size: " << getCompressedDataSize() << " 16-bit words" << std::endl; + std::cout << "Compressed Size: " << getCompressedSize() << " bytes" << std::endl; + + if (cformat == compresstype::LZ78) { + std::cout << "Dictionary Size: " << getDictionarySize() << " bytes" << std::endl; + std::cout << "Dictionary Entries: " << overheadmap.size() << std::endl; + std::cout << "Total Compressed Size: " << getTotalCompressedSize() << " bytes" << std::endl; + } else { + std::cout << "Total Compressed Size: " << getTotalCompressedSize() << " bytes" << std::endl; + } + std::cout << "Compression Ratio: " << getCompressionRatio() << ":1" << std::endl; if (getCompressionRatio() > 1.0) { double savings = (1.0 - (1.0 / getCompressionRatio())) * 100.0; std::cout << "Space Savings: " << savings << "%" << std::endl; } - - // Show dictionary size for LZ78 - if (cformat == compresstype::LZ78) { - std::cout << "Dictionary Size: " << overheadmap.size() << " entries" << std::endl; - } } // Print compression information in a compact format void printCompressionStats() const { - std::cout << "[" << getCompressionTypeString() << "] " - << getSourceSize() << "B -> " << getCompressedSize() * 2 << "B " - << "(ratio: " << getCompressionRatio() << ":1)" << std::endl; + if (cformat == compresstype::LZ78) { + std::cout << "[" << getCompressionTypeString() << "] " + << getSourceSize() << "B -> " << getCompressedSize() << "B + " + << getDictionarySize() << "B dict = " << getTotalCompressedSize() << "B " + << "(ratio: " << getCompressionRatio() << ":1)" << std::endl; + } else { + std::cout << "[" << getCompressionTypeString() << "] " + << getSourceSize() << "B -> " << getTotalCompressedSize() << "B " + << "(ratio: " << getCompressionRatio() << ":1)" << std::endl; + } } // Get compression type as string