-
Notifications
You must be signed in to change notification settings - Fork 112
[k2] zstd builtins #1453
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
Merged
Merged
[k2] zstd builtins #1453
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9ad9ba1
almost implemented zstd
57f333d
removed k2-skip from zstd phpt tests
91ab99f
runtime-light.cmake: added ZSTD::pic::zstd to runtime-light-pic linking
869a6d6
fixed some issues
8d1fa69
fixed other issues
8ed9314
fixed other issues
cd65d7e
noexcept lambdas and std::addressof
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
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,139 @@ | ||
| // Compiler for PHP (aka KPHP) | ||
| // Copyright (c) 2025 LLC «V Kontakte» | ||
| // Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
|
||
| #include "runtime-light/stdlib/zstd/zstd-functions.h" | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <optional> | ||
| #include <span> | ||
|
|
||
| #define ZSTD_STATIC_LINKING_ONLY | ||
| #include "zstd/zstd.h" | ||
|
|
||
| #include "common/containers/final_action.h" | ||
| #include "runtime-common/core/allocator/script-malloc-interface.h" | ||
| #include "runtime-common/core/runtime-core.h" | ||
| #include "runtime-common/stdlib/string/string-context.h" | ||
| #include "runtime-light/stdlib/diagnostics/logs.h" | ||
|
|
||
| namespace { | ||
|
|
||
| static_assert(2 * ZSTD_BLOCKSIZE_MAX < StringLibContext::STATIC_BUFFER_LENGTH, "double block size is expected to be less then buffer size"); | ||
|
|
||
| constexpr ZSTD_customMem zstd_allocator{[](void*, size_t size) noexcept { return kphp::memory::script::alloc(size); }, | ||
| [](void*, void* ptr) noexcept { return kphp::memory::script::free(ptr); }}; | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace kphp::zstd { | ||
|
|
||
| std::optional<string> compress(std::span<const std::byte> data, int64_t level, std::span<const std::byte> dict) noexcept { | ||
| const int32_t min_level{ZSTD_minCLevel()}; | ||
| const int32_t max_level{ZSTD_maxCLevel()}; | ||
| if (level < min_level || max_level < level) { | ||
| kphp::log::warning("zstd_compress: compression level ({}) must be within [{}..{}]", level, min_level, max_level); | ||
| return {}; | ||
| } | ||
|
|
||
| ZSTD_CCtx* ctx{ZSTD_createCCtx_advanced(zstd_allocator)}; | ||
| if (!ctx) { | ||
| kphp::log::warning("zstd_compress: can not create context"); | ||
| return {}; | ||
| } | ||
| const auto finalizer{vk::finally([&ctx]() noexcept { ZSTD_freeCCtx(ctx); })}; | ||
|
|
||
| size_t result{ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, static_cast<int>(level))}; | ||
| if (ZSTD_isError(result)) { | ||
| kphp::log::warning("zstd_compress: can not init context: {}", ZSTD_getErrorName(result)); | ||
| return {}; | ||
| } | ||
|
|
||
| result = ZSTD_CCtx_loadDictionary_byReference(ctx, dict.data(), dict.size()); | ||
| if (ZSTD_isError(result)) { | ||
| kphp::log::warning("zstd_compress: can not load dict: {}", ZSTD_getErrorName(result)); | ||
| return {}; | ||
| } | ||
|
|
||
| kphp::log::assertion(ZSTD_CStreamOutSize() <= StringLibContext::STATIC_BUFFER_LENGTH); | ||
| ZSTD_outBuffer out{StringLibContext::get().static_buf.get(), StringLibContext::STATIC_BUFFER_LENGTH, 0}; | ||
| ZSTD_inBuffer in{data.data(), data.size(), 0}; | ||
|
|
||
| string encoded_string{}; | ||
| do { | ||
| result = ZSTD_compressStream2(ctx, std::addressof(out), std::addressof(in), ZSTD_e_end); | ||
| if (ZSTD_isError(result)) { | ||
| kphp::log::warning("zstd_compress: got zstd stream compression error: {}", ZSTD_getErrorName(result)); | ||
| return {}; | ||
| } | ||
| encoded_string.append(static_cast<char*>(out.dst), out.pos); | ||
| out.pos = 0; | ||
| } while (result); | ||
| return encoded_string; | ||
| } | ||
|
|
||
| std::optional<string> uncompress(std::span<const std::byte> data, std::span<const std::byte> dict) noexcept { | ||
| auto size{ZSTD_getFrameContentSize(data.data(), data.size())}; | ||
| if (size == ZSTD_CONTENTSIZE_ERROR) { | ||
| kphp::log::warning("zstd_uncompress: it was not compressed by zstd"); | ||
| return {}; | ||
| } | ||
|
|
||
| ZSTD_DCtx* ctx{ZSTD_createDCtx_advanced(zstd_allocator)}; | ||
| if (!ctx) { | ||
| kphp::log::warning("zstd_uncompress: can not create context"); | ||
| return {}; | ||
| } | ||
| const auto finalizer{vk::finally([&ctx]() noexcept { ZSTD_freeDCtx(ctx); })}; | ||
|
|
||
| size_t result{ZSTD_DCtx_loadDictionary_byReference(ctx, dict.data(), dict.size())}; | ||
| if (ZSTD_isError(result)) { | ||
| kphp::log::warning("zstd_uncompress: can not load dict: {}", ZSTD_getErrorName(result)); | ||
| return {}; | ||
| } | ||
|
|
||
| if (size != ZSTD_CONTENTSIZE_UNKNOWN) { | ||
| if (size > string::max_size()) { | ||
| kphp::log::warning("zstd_uncompress: trying to uncompress too large data"); | ||
| return {}; | ||
| } | ||
| string decompressed{static_cast<string::size_type>(size), false}; | ||
| result = ZSTD_decompressDCtx(ctx, decompressed.buffer(), size, data.data(), data.size()); | ||
| if (ZSTD_isError(result)) { | ||
| kphp::log::warning("zstd_uncompress: got zstd error: {}", ZSTD_getErrorName(result)); | ||
| return {}; | ||
| } | ||
| return decompressed; | ||
| } | ||
|
|
||
| if (ZSTD_isError(result)) { | ||
| kphp::log::warning("zstd_uncompress: can not init stream: {}", ZSTD_getErrorName(result)); | ||
| return {}; | ||
| } | ||
|
|
||
| kphp::log::assertion(ZSTD_DStreamOutSize() <= StringLibContext::STATIC_BUFFER_LENGTH); | ||
| ZSTD_inBuffer in{data.data(), data.size(), 0}; | ||
| ZSTD_outBuffer out{StringLibContext::get().static_buf.get(), StringLibContext::STATIC_BUFFER_LENGTH, 0}; | ||
|
|
||
| string decoded_string{}; | ||
| while (in.pos < in.size) { | ||
| if (out.pos == out.size) { | ||
| decoded_string.append(static_cast<char*>(out.dst), static_cast<string::size_type>(out.pos)); | ||
| out.pos = 0; | ||
| } | ||
|
|
||
| result = ZSTD_decompressStream(ctx, std::addressof(out), std::addressof(in)); | ||
| if (ZSTD_isError(result)) { | ||
| kphp::log::warning("zstd_uncompress: can not decompress stream: {}", ZSTD_getErrorName(result)); | ||
| return {}; | ||
| } | ||
| if (result == 0) { | ||
| break; | ||
| } | ||
| } | ||
| decoded_string.append(static_cast<char*>(out.dst), static_cast<string::size_type>(out.pos)); | ||
| return decoded_string; | ||
| } | ||
|
|
||
| } // namespace kphp::zstd | ||
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,57 @@ | ||
| // Compiler for PHP (aka KPHP) | ||
| // Copyright (c) 2025 LLC «V Kontakte» | ||
| // Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <optional> | ||
| #include <span> | ||
|
|
||
| #include "runtime-common/core/runtime-core.h" | ||
|
|
||
| namespace kphp::zstd { | ||
|
|
||
| inline constexpr int64_t DEFAULT_COMPRESS_LEVEL = 3; | ||
|
|
||
| std::optional<string> compress(std::span<const std::byte> data, int64_t level = DEFAULT_COMPRESS_LEVEL, | ||
| std::span<const std::byte> dict = std::span<const std::byte>{}) noexcept; | ||
|
|
||
| std::optional<string> uncompress(std::span<const std::byte> data, std::span<const std::byte> dict = std::span<const std::byte>{}) noexcept; | ||
|
|
||
| } // namespace kphp::zstd | ||
|
|
||
| inline Optional<string> f$zstd_compress(const string& data, int64_t level = kphp::zstd::DEFAULT_COMPRESS_LEVEL) noexcept { | ||
| auto res{kphp::zstd::compress({reinterpret_cast<const std::byte*>(data.c_str()), static_cast<size_t>(data.size())}, level)}; | ||
| if (!res) [[unlikely]] { | ||
| return false; | ||
| } | ||
| return res.value(); | ||
| } | ||
|
|
||
| inline Optional<string> f$zstd_uncompress(const string& data) noexcept { | ||
| auto res{kphp::zstd::uncompress({reinterpret_cast<const std::byte*>(data.c_str()), static_cast<size_t>(data.size())})}; | ||
| if (!res) [[unlikely]] { | ||
| return false; | ||
| } | ||
| return res.value(); | ||
| } | ||
|
|
||
| inline Optional<string> f$zstd_compress_dict(const string& data, const string& dict) noexcept { | ||
| auto res{kphp::zstd::compress({reinterpret_cast<const std::byte*>(data.c_str()), static_cast<size_t>(data.size())}, kphp::zstd::DEFAULT_COMPRESS_LEVEL, | ||
| {reinterpret_cast<const std::byte*>(dict.c_str()), static_cast<size_t>(dict.size())})}; | ||
| if (!res) [[unlikely]] { | ||
| return false; | ||
| } | ||
| return res.value(); | ||
| } | ||
|
|
||
| inline Optional<string> f$zstd_uncompress_dict(const string& data, const string& dict) noexcept { | ||
| auto res{kphp::zstd::uncompress({reinterpret_cast<const std::byte*>(data.c_str()), static_cast<size_t>(data.size())}, | ||
| {reinterpret_cast<const std::byte*>(dict.c_str()), static_cast<size_t>(dict.size())})}; | ||
| if (!res) [[unlikely]] { | ||
| return false; | ||
| } | ||
| return res.value(); | ||
| } |
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| @ok k2_skip | ||
| @ok | ||
| <?php | ||
|
|
||
| function test_compress_levels() { | ||
|
|
||
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| @ok k2_skip | ||
| @ok | ||
| <?php | ||
|
|
||
| function test_uncompress() { | ||
|
|
||
16 changes: 16 additions & 0 deletions
16
tests/phpt/zstd/4_compress_uncompress_without_large_strings.php
T-y-c-o-o-n marked this conversation as resolved.
Show resolved
Hide resolved
|
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,16 @@ | ||
| @ok | ||
| <?php | ||
|
|
||
| require_once 'kphp_tester_include.php'; | ||
|
|
||
| function test_compress_uncompress() { | ||
| var_dump(zstd_uncompress((string)zstd_compress("foo bar baz"))); | ||
|
|
||
| var_dump(zstd_uncompress((string)zstd_compress(str_repeat("foo bar baz", 10000)))); | ||
|
|
||
| $random_data = (string)openssl_random_pseudo_bytes(1024*1024*15); | ||
| assert_true(zstd_uncompress((string)zstd_compress($random_data)) === $random_data); | ||
| } | ||
|
|
||
|
|
||
| test_compress_uncompress(); |
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.