Skip to content
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

do not initialize StringLibContext's static buffers #1229

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions runtime-common/stdlib/string/string-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ inline constexpr std::string_view PERCENT_ = "%";

}; // namespace string_context_impl_

class StringLibContext final : vk::not_copyable {
struct StringLibContext final : private vk::not_copyable {
static constexpr int32_t MASK_BUFFER_LENGTH = 256;

public:
static constexpr int32_t STATIC_BUFFER_LENGTH = 1U << 23U;

std::array<char, STATIC_BUFFER_LENGTH + 1> static_buf{};
std::array<char, MASK_BUFFER_LENGTH> mask_buffer{};

int64_t str_replace_count_dummy{};
double default_similar_text_percent_stub{};

// Do not initialize these arrays. Initializing it would zero out the memory,
// which significantly impacts K2's performance due to the large size of the buffer.
// The buffer is intended to be used as raw storage, and its contents will be
// explicitly managed elsewhere in the code.
std::array<char, STATIC_BUFFER_LENGTH + 1> static_buf;
std::array<char, MASK_BUFFER_LENGTH> mask_buf;

StringLibContext() noexcept = default;

static StringLibContext &get() noexcept;
};

Expand Down
3 changes: 1 addition & 2 deletions runtime-common/stdlib/string/string-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "runtime-common/stdlib/string/string-functions.h"

#include <cctype>
#include <clocale>
#include <cstdint>
#include <sys/types.h>

Expand All @@ -16,7 +15,7 @@
#include "runtime-common/stdlib/string/string-context.h"

const char *get_mask(const string &what) noexcept {
auto &mask{StringLibContext::get().mask_buffer};
auto &mask{StringLibContext::get().mask_buf};
std::memset(mask.data(), 0, mask.size());

int len = what.size();
Expand Down
4 changes: 2 additions & 2 deletions runtime-light/state/instance-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include <utility>

#include "common/mixin/not_copyable.h"
#include "runtime-common/core/allocator/runtime-allocator.h"
#include "runtime-common/core/runtime-core.h"
#include "runtime-common/core/std/containers.h"
#include "runtime-light/allocator/allocator.h"
#include "runtime-light/core/globals/php-script-globals.h"
#include "runtime-light/coroutine/task.h"
#include "runtime-light/k2-platform/k2-api.h"
Expand Down Expand Up @@ -120,7 +120,7 @@ struct InstanceState final : vk::not_copyable {
RegexInstanceState regex_instance_state;
CurlInstanceState curl_instance_state{};
CryptoInstanceState crypto_instance_state{};
StringInstanceState string_instance_state{};
StringInstanceState string_instance_state;
SystemInstanceState system_instance_state{};
FileSystemInstanceState file_system_instance_state{};

Expand Down
Loading