-
Notifications
You must be signed in to change notification settings - Fork 79
feat: add compression for PCH/PCM #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
664b7ec
Feat: add default LZ4 compression for PCH/PCM
AXiX-official 1fc7ad3
style: format code with clang-format
AXiX-official 3ff754f
fix: correct parameter type in compressPreCompiledFile()
AXiX-official f2a169c
build: add missing Support/Logger.h include to Compilation.cpp
AXiX-official File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #pragma once | ||
|
|
||
| #include "llvm/ADT/StringRef.h" | ||
| #include "llvm/Support/raw_ostream.h" | ||
| #include "clang/Frontend/CompilerInstance.h" | ||
| #include <memory> | ||
|
|
||
| namespace llvm { | ||
| class MemoryBuffer; | ||
| } | ||
|
|
||
| namespace clice { | ||
|
|
||
| /** | ||
| * @brief Compresses a pre-compiled file (PCH/PCM) using LZ4 and removes the original file. | ||
| * | ||
| * The compressed file has a custom format: an 8-byte header for the | ||
| * original size (uint64_t), followed by the LZ4 compressed data. | ||
| * | ||
| * @param path The path to the pre-compiled file to compress. | ||
| */ | ||
| void compressPreCompiledFile(std::string path); | ||
| /** | ||
| * @brief Compresses a file using LZ4 and saves it to an output path. | ||
| * | ||
| * The compressed file has a custom format: an 8-byte header for the | ||
| * original size (uint64_t), followed by the LZ4 compressed data. | ||
| * | ||
| * @param inputPath The path to the file to compress. | ||
| * @param outputPath The path where the compressed file will be saved. | ||
| * @return true on success, false on failure (e.g., input file not found). | ||
| */ | ||
| bool compressToFile(std::string inputPath, std::string outputPath); | ||
| /** | ||
| * @brief Decompresses a file compressed with `compressToFile`. | ||
| * | ||
| * The function reads the custom format: an 8-byte header for the | ||
| * original size (uint64_t), followed by the LZ4 compressed data. | ||
| * | ||
| * @param path The path to the compressed file. | ||
| * @return A unique pointer to a MemoryBuffer containing the decompressed data, | ||
| * or nullptr on failure (e.g., file not found or decompression error). | ||
| */ | ||
| std::unique_ptr<llvm::MemoryBuffer> decompressFile(std::string path); | ||
|
|
||
| } // namespace clice |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #include "Support/Compression.h" | ||
| #include "llvm/Support/MemoryBuffer.h" | ||
| #include "lz4.h" | ||
| #include <string> | ||
| #include "Support/FileSystem.h" | ||
| #include "Support/Logger.h" | ||
|
|
||
| namespace clice { | ||
|
|
||
| void compressPreCompiledFile(std::string path){ | ||
|
AXiX-official marked this conversation as resolved.
Outdated
|
||
| if (!fs::exists(path)) { | ||
| log::warn("PreCompiledFile does not exist: {}", path); | ||
| } else if (!compressToFile(path, path + ".lz4")) { | ||
| log::fatal("Fail to compress PreCompiledFile: {}", path); | ||
| } else { | ||
| if (auto ec = fs::remove(path)) { | ||
| log::fatal("Fail to remove original PreCompiledFile: {}. Reason: {}", path, ec.message()); | ||
| } | ||
| } | ||
|
AXiX-official marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| bool compressToFile(std::string inputPath, std::string outputPath) { | ||
| auto content = fs::read(inputPath); | ||
| if (!content) { | ||
| return false; | ||
| } | ||
| uint64_t originalSize = content->size(); | ||
| int maxCompressedSize = LZ4_compressBound(originalSize); | ||
| std::vector<char> compressed(maxCompressedSize + sizeof(uint64_t)); | ||
|
|
||
| // Our custom format: Prepend the 8-byte original size as a header | ||
| // before the compressed data. | ||
| memcpy(compressed.data(), &originalSize, sizeof(uint64_t)); | ||
|
|
||
| int compressedDataSize = LZ4_compress_default(content->data(), compressed.data() + sizeof(uint64_t), originalSize, maxCompressedSize); | ||
| if (compressedDataSize <= 0) { | ||
| return false; | ||
| } | ||
|
|
||
| size_t totalSize = compressedDataSize + sizeof(uint64_t); | ||
|
|
||
| llvm::StringRef dataToWrite(compressed.data(), totalSize); | ||
| auto result = fs::write(outputPath, dataToWrite); | ||
| return result.has_value(); | ||
| } | ||
|
|
||
| std::unique_ptr<llvm::MemoryBuffer> decompressFile(std::string path) { | ||
| auto content = fs::read(path); | ||
| if (!content) { | ||
| log::fatal("Fail to read compressed file: {}", path); | ||
| return nullptr; | ||
| } | ||
|
AXiX-official marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (content->size() < sizeof(uint64_t)) { | ||
| log::fatal("Fail to read compressed file: {}", path); | ||
| return nullptr; // Not enough data for size header | ||
| } | ||
|
AXiX-official marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Our custom format: Prepend the 8-byte original size as a header | ||
| // before the compressed data. | ||
| uint64_t originalSize; | ||
| memcpy(&originalSize, content->data(), sizeof(uint64_t)); | ||
|
|
||
| const char* compressedData = content->data() + sizeof(uint64_t); | ||
| size_t compressedDataSize = content->size() - sizeof(uint64_t); | ||
|
|
||
| auto buffer = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(originalSize); | ||
| if (!buffer) { | ||
| log::fatal("Fail to allocate memory buffer for decompression: {}", path); | ||
| return nullptr; | ||
| } | ||
|
AXiX-official marked this conversation as resolved.
Outdated
|
||
|
|
||
| int decompressedSize = LZ4_decompress_safe(compressedData, buffer->getBufferStart(), compressedDataSize, originalSize); | ||
|
|
||
| if (decompressedSize < 0) { // LZ4 returns negative on error | ||
| log::fatal("Fail to decompress file: {}", path); | ||
| return nullptr; | ||
| } | ||
|
AXiX-official marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (static_cast<uint64_t>(decompressedSize) != originalSize) { | ||
| // This case should ideally not happen if the stored size is correct | ||
| return nullptr; | ||
| } | ||
|
|
||
| return buffer; | ||
| } | ||
|
|
||
| } // namespace clice | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.