Skip to content

Commit 59334f6

Browse files
authored
[k2] add confdata request cache (#1430)
1 parent 46e1761 commit 59334f6

5 files changed

Lines changed: 62 additions & 1 deletion

File tree

runtime-light/state/instance-state.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "runtime-light/server/job-worker/job-worker-server-state.h"
2222
#include "runtime-light/server/rpc/rpc-server-state.h"
2323
#include "runtime-light/state/component-state.h"
24+
#include "runtime-light/stdlib/confdata/confdata-state.h"
2425
#include "runtime-light/stdlib/curl/curl-state.h"
2526
#include "runtime-light/stdlib/diagnostics/contextual-logger.h"
2627
#include "runtime-light/stdlib/diagnostics/error-handling-state.h"
@@ -102,6 +103,7 @@ struct InstanceState final : vk::not_copyable {
102103
JobWorkerClientInstanceState job_worker_client_instance_state;
103104
JobWorkerServerInstanceState job_worker_server_instance_state;
104105
InstanceCacheInstanceState instance_cache_instance_state;
106+
ConfdataInstanceState confdata_instance_state;
105107

106108
TimeInstanceState time_instance_state;
107109
MathInstanceState math_instance_state;

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "runtime-light/k2-platform/k2-api.h"
1919
#include "runtime-light/stdlib/component/component-api.h"
2020
#include "runtime-light/stdlib/confdata/confdata-constants.h"
21+
#include "runtime-light/stdlib/confdata/confdata-state.h"
2122
#include "runtime-light/stdlib/diagnostics/logs.h"
2223
#include "runtime-light/stdlib/fork/fork-functions.h"
2324
#include "runtime-light/streams/read-ext.h"
@@ -45,6 +46,11 @@ mixed extract_confdata_value(const tl::confdataValue& confdata_value) noexcept {
4546
} // namespace
4647

4748
kphp::coro::task<mixed> f$confdata_get_value(const string& key) noexcept {
49+
auto& confdata_key_cache{ConfdataInstanceState::get().key_cache()};
50+
if (auto it{confdata_key_cache.find(key)}; it != confdata_key_cache.end()) {
51+
co_return it->second;
52+
}
53+
4854
tl::ConfdataGet confdata_get{.key = {.value = {key.c_str(), key.size()}}};
4955
tl::storer tls{confdata_get.footprint()};
5056
confdata_get.store(tls);
@@ -67,12 +73,20 @@ kphp::coro::task<mixed> f$confdata_get_value(const string& key) noexcept {
6773
if (!maybe_confdata_value.opt_value) { // no such key
6874
co_return mixed{};
6975
}
70-
co_return extract_confdata_value(*maybe_confdata_value.opt_value); // the key exists
76+
77+
auto value{extract_confdata_value(*maybe_confdata_value.opt_value)}; // the key exists
78+
confdata_key_cache.emplace(key, value);
79+
co_return std::move(value);
7180
}
7281

7382
kphp::coro::task<array<mixed>> f$confdata_get_values_by_any_wildcard(const string& wildcard) noexcept {
7483
static constexpr size_t CONFDATA_GET_WILDCARD_STREAM_CAPACITY = 1 << 20;
7584

85+
auto& confdata_wildcard_cache{ConfdataInstanceState::get().wildcard_cache()};
86+
if (auto it{confdata_wildcard_cache.find(wildcard)}; it != confdata_wildcard_cache.end()) {
87+
co_return it->second;
88+
}
89+
7690
tl::ConfdataGetWildcard confdata_get_wildcard{.wildcard = {.value = {wildcard.c_str(), wildcard.size()}}};
7791
tl::storer tls{confdata_get_wildcard.footprint()};
7892
confdata_get_wildcard.store(tls);
@@ -98,5 +112,6 @@ kphp::coro::task<array<mixed>> f$confdata_get_values_by_any_wildcard(const strin
98112
result.set_value(string{dict_field.key.value.data(), static_cast<string::size_type>(dict_field.key.value.size())},
99113
extract_confdata_value(dict_field.value));
100114
});
115+
confdata_wildcard_cache.emplace(wildcard, result);
101116
co_return std::move(result);
102117
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Compiler for PHP (aka KPHP)
2+
// Copyright (c) 2025 LLC «V Kontakte»
3+
// Distributed under the GPL v3 License, see LICENSE.notice.txt
4+
5+
#include "runtime-light/stdlib/confdata/confdata-state.h"
6+
7+
#include "runtime-light/state/instance-state.h"
8+
9+
ConfdataInstanceState& ConfdataInstanceState::get() noexcept {
10+
return InstanceState::get().confdata_instance_state;
11+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Compiler for PHP (aka KPHP)
2+
// Copyright (c) 2025 LLC «V Kontakte»
3+
// Distributed under the GPL v3 License, see LICENSE.notice.txt
4+
5+
#pragma once
6+
7+
#include <cstddef>
8+
9+
#include "common/mixin/not_copyable.h"
10+
#include "runtime-common/core/allocator/script-allocator.h"
11+
#include "runtime-common/core/runtime-core.h"
12+
#include "runtime-common/core/std/containers.h"
13+
14+
class ConfdataInstanceState final : private vk::not_copyable {
15+
using hasher_type = decltype([](const string& s) noexcept { return static_cast<size_t>(s.hash()); });
16+
17+
kphp::stl::unordered_map<string, mixed, kphp::memory::script_allocator, hasher_type> m_key_cache;
18+
kphp::stl::unordered_map<string, array<mixed>, kphp::memory::script_allocator, hasher_type> m_wildcard_cache;
19+
20+
public:
21+
ConfdataInstanceState() noexcept = default;
22+
23+
auto& key_cache() noexcept {
24+
return m_key_cache;
25+
}
26+
27+
auto& wildcard_cache() noexcept {
28+
return m_wildcard_cache;
29+
}
30+
31+
static ConfdataInstanceState& get() noexcept;
32+
};

runtime-light/stdlib/stdlib.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ prepend(
22
RUNTIME_LIGHT_STDLIB_SRC
33
stdlib/
44
confdata/confdata-functions.cpp
5+
confdata/confdata-state.cpp
56
crypto/crypto-functions.cpp
67
curl/curl-state.cpp
78
diagnostics/backtrace.cpp

0 commit comments

Comments
 (0)