Skip to content

Commit ef38504

Browse files
committed
[k2] remove wildcard prefix from confdata keys
1 parent c58896c commit ef38504

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

runtime-light/stdlib/confdata/confdata-functions.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <algorithm>
88
#include <cstddef>
99
#include <span>
10+
#include <string_view>
1011
#include <utility>
1112

1213
#include "runtime-common/core/allocator/script-allocator.h"
@@ -75,19 +76,21 @@ kphp::coro::task<mixed> f$confdata_get_value(string key) noexcept {
7576
}
7677

7778
auto value{extract_confdata_value(*maybe_confdata_value.opt_value)}; // the key exists
78-
confdata_key_cache.emplace(key, value);
79+
confdata_key_cache.emplace(std::move(key), value);
7980
co_return std::move(value);
8081
}
8182

8283
kphp::coro::task<array<mixed>> f$confdata_get_values_by_any_wildcard(string wildcard) noexcept {
83-
static constexpr size_t CONFDATA_GET_WILDCARD_STREAM_CAPACITY = 1 << 20;
84+
static constexpr size_t CONFDATA_GET_WILDCARD_INIT_BUFFER_CAPACITY = 1 << 20;
8485

8586
auto& confdata_wildcard_cache{ConfdataInstanceState::get().wildcard_cache()};
8687
if (auto it{confdata_wildcard_cache.find(wildcard)}; it != confdata_wildcard_cache.end()) {
8788
co_return it->second;
8889
}
8990

90-
tl::ConfdataGetWildcard confdata_get_wildcard{.wildcard = {.value = {wildcard.c_str(), wildcard.size()}}};
91+
const std::string_view wildcard_view{wildcard.c_str(), wildcard.size()};
92+
93+
const tl::ConfdataGetWildcard confdata_get_wildcard{.wildcard = {.value = wildcard_view}};
9194
tl::storer tls{confdata_get_wildcard.footprint()};
9295
confdata_get_wildcard.store(tls);
9396

@@ -98,7 +101,7 @@ kphp::coro::task<array<mixed>> f$confdata_get_values_by_any_wildcard(string wild
98101

99102
auto stream{*std::move(expected_stream)};
100103
kphp::stl::vector<std::byte, kphp::memory::script_allocator> response{};
101-
response.reserve(CONFDATA_GET_WILDCARD_STREAM_CAPACITY);
104+
response.reserve(CONFDATA_GET_WILDCARD_INIT_BUFFER_CAPACITY);
102105
if (!co_await kphp::forks::id_managed(kphp::component::query(stream, tls.view(), kphp::component::read_ext::append(response)))) [[unlikely]] {
103106
co_return array<mixed>{};
104107
}
@@ -108,10 +111,18 @@ kphp::coro::task<array<mixed>> f$confdata_get_values_by_any_wildcard(string wild
108111
kphp::log::assertion(dict_confdata_value.fetch(tlf));
109112

110113
array<mixed> result{array_size{static_cast<int64_t>(dict_confdata_value.size()), false}};
111-
std::for_each(dict_confdata_value.begin(), dict_confdata_value.end(), [&result](const auto& dict_field) noexcept {
112-
result.set_value(string{dict_field.key.value.data(), static_cast<string::size_type>(dict_field.key.value.size())},
113-
extract_confdata_value(dict_field.value));
114+
std::ranges::for_each(dict_confdata_value, [&result, wildcard_size = wildcard_view.size()](const auto& dict_field) noexcept {
115+
kphp::log::assertion(dict_field.key.value.size() >= wildcard_size);
116+
117+
mixed confdata_value{extract_confdata_value(dict_field.value)};
118+
const std::string_view key_without_wildcard_prefix{dict_field.key.value.substr(wildcard_size)};
119+
if (key_without_wildcard_prefix.empty()) [[unlikely]] {
120+
result.emplace_back(std::move(confdata_value));
121+
} else {
122+
result.set_value(string{key_without_wildcard_prefix.data(), static_cast<string::size_type>(key_without_wildcard_prefix.size())},
123+
std::move(confdata_value));
124+
}
114125
});
115-
confdata_wildcard_cache.emplace(wildcard, result);
126+
confdata_wildcard_cache.emplace(std::move(wildcard), result);
116127
co_return std::move(result);
117128
}

0 commit comments

Comments
 (0)