Skip to content

Commit 7892ef6

Browse files
committed
add a little bit of style
1 parent 83f1223 commit 7892ef6

File tree

6 files changed

+80
-87
lines changed

6 files changed

+80
-87
lines changed

runtime-light/state/instance-state.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ struct InstanceState final : vk::not_copyable {
122122
HttpServerInstanceState http_server_instance_state;
123123
JobWorkerClientInstanceState job_worker_client_instance_state;
124124
JobWorkerServerInstanceState job_worker_server_instance_state;
125-
InstanceCacheInstanceState instance_cache_state;
125+
InstanceCacheInstanceState instance_cache_instance_state;
126126

127127
MathInstanceState math_instance_state;
128128
RandomInstanceState random_instance_state;

runtime-light/stdlib/instance-cache/instance-cache-functions.h

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <cstdint>
88
#include <limits>
9+
#include <string_view>
10+
#include <utility>
911

1012
#include "runtime-common/core/runtime-core.h"
1113
#include "runtime-common/stdlib/serialization/msgpack-functions.h"
@@ -19,135 +21,135 @@
1921
#include "runtime-light/utils/logs.h"
2022

2123
namespace kphp::instance_cache::details {
24+
2225
inline constexpr std::string_view COMPONENT_NAME{"instance_cache"};
26+
2327
} // namespace kphp::instance_cache::details
2428

25-
template<typename ClassInstanceType>
26-
kphp::coro::task<bool> f$instance_cache_store(string key, ClassInstanceType instance, int64_t ttl = 0) noexcept {
29+
template<typename InstanceType>
30+
kphp::coro::task<bool> f$instance_cache_store(string key, InstanceType instance, int64_t ttl = 0) noexcept {
2731
if (ttl < 0) [[unlikely]] {
28-
kphp::log::warning("ttl value '{}' < 0 is noop for key '{}'", ttl, key.c_str());
32+
kphp::log::warning("ttl can't be negative: ttl -> {}, key -> {}", ttl, key.c_str());
2933
co_return false;
3034
}
31-
3235
if (ttl > std::numeric_limits<uint32_t>::max()) [[unlikely]] {
33-
kphp::log::warning("ttl value '{}' exceeds maximum allowed value '{}', it will be stored forever for key: {}", ttl, std::numeric_limits<uint32_t>::max(),
34-
key.c_str());
36+
kphp::log::warning("ttl exceeds maximum allowed value, key will be stored forever: ttl -> {}, max -> {}, key -> {}", ttl,
37+
std::numeric_limits<uint32_t>::max(), key.c_str());
3538
ttl = 0;
3639
}
3740

3841
auto serialized_instance{f$instance_serialize(instance)};
3942
if (!serialized_instance.has_value()) [[unlikely]] {
40-
kphp::log::warning("cannot serialize instance for key: {}", key.c_str());
43+
kphp::log::warning("can't serialize instance: key -> {}", key.c_str());
4144
co_return false;
4245
}
4346

4447
tl::TLBuffer tlb;
45-
46-
tl::CacheStore{.key{tl::string{std::string_view{key.c_str(), key.size()}}},
47-
.value{tl::string{std::string_view{serialized_instance.val().c_str(), serialized_instance.val().size()}}},
48-
.ttl{tl::u32{static_cast<uint32_t>(ttl)}}}
48+
tl::CacheStore{.key = tl::string{.value = {key.c_str(), key.size()}},
49+
.value = tl::string{.value = {serialized_instance.val().c_str(), serialized_instance.val().size()}},
50+
.ttl = tl::u32{.value = static_cast<uint32_t>(ttl)}}
4951
.store(tlb);
5052

5153
auto query{co_await f$component_client_send_request(
5254
string{kphp::instance_cache::details::COMPONENT_NAME.data(), static_cast<string::size_type>(kphp::instance_cache::details::COMPONENT_NAME.size())},
5355
string{tlb.data(), static_cast<string::size_type>(tlb.size())})};
54-
5556
if (query.is_null()) [[unlikely]] {
5657
co_return false;
5758
}
5859

59-
auto resp{co_await f$component_client_fetch_response(query)};
60+
auto response{co_await f$component_client_fetch_response(std::move(query))};
6061
tlb.clean();
61-
tlb.store_bytes({resp.c_str(), static_cast<size_t>(resp.size())});
62-
tl::Bool tl_resp;
63-
kphp::log::assertion(tl_resp.fetch(tlb));
64-
InstanceCacheInstanceState::get().request_cache[key] = f$to_mixed(instance);
65-
co_return tl_resp.value;
62+
tlb.store_bytes({response.c_str(), static_cast<size_t>(response.size())});
63+
64+
tl::Bool tl_bool{};
65+
kphp::log::assertion(tl_bool.fetch(tlb));
66+
InstanceCacheInstanceState::get().request_cache.emplace(std::move(key), std::move(instance));
67+
co_return tl_bool.value;
6668
}
6769

68-
template<typename ClassInstanceType>
69-
kphp::coro::task<ClassInstanceType> f$instance_cache_fetch(string /*class_name*/, string key, bool /*even_if_expired*/ = false) noexcept {
70+
template<typename InstanceType>
71+
kphp::coro::task<InstanceType> f$instance_cache_fetch(string /*class_name*/, string key, bool /*even_if_expired*/ = false) noexcept {
7072
auto& request_cache{InstanceCacheInstanceState::get().request_cache};
71-
if (auto cached{from_mixed<ClassInstanceType>(request_cache[key], string())}; !cached.is_null()) {
72-
co_return std::move(cached);
73+
if (auto it{request_cache.find(key)}; it != request_cache.end()) {
74+
auto cached_instance{from_mixed<InstanceType>(it->second, {})};
75+
kphp::log::assertion(!cached_instance.is_null());
76+
co_return std::move(cached_instance);
7377
}
7478

7579
tl::TLBuffer tlb;
76-
tl::CacheFetch{.key{tl::string{std::string_view{key.c_str(), key.size()}}}}.store(tlb);
80+
tl::CacheFetch{.key = tl::string{.value = {key.c_str(), key.size()}}}.store(tlb);
7781

7882
auto query{co_await f$component_client_send_request(
7983
string{kphp::instance_cache::details::COMPONENT_NAME.data(), static_cast<string::size_type>(kphp::instance_cache::details::COMPONENT_NAME.size())},
8084
string{tlb.data(), static_cast<string::size_type>(tlb.size())})};
81-
8285
if (query.is_null()) [[unlikely]] {
83-
co_return ClassInstanceType{};
86+
co_return InstanceType{};
8487
}
8588

86-
auto resp{co_await f$component_client_fetch_response(query)};
89+
auto response{co_await f$component_client_fetch_response(std::move(query))};
8790
tlb.clean();
88-
tlb.store_bytes({resp.c_str(), static_cast<size_t>(resp.size())});
91+
tlb.store_bytes({response.c_str(), static_cast<size_t>(response.size())});
8992

90-
tl::Maybe<tl::string> tl_resp;
91-
kphp::log::assertion(tl_resp.fetch(tlb));
92-
if (!tl_resp.opt_value.has_value()) [[unlikely]] {
93-
co_return ClassInstanceType{};
93+
tl::Maybe<tl::string> maybe_string{};
94+
kphp::log::assertion(maybe_string.fetch(tlb));
95+
if (!maybe_string.opt_value) [[unlikely]] {
96+
co_return InstanceType{};
9497
}
9598

96-
auto response{f$instance_deserialize<ClassInstanceType>(string(tl_resp.opt_value->value.data(), tl_resp.opt_value->value.size()), {})};
97-
request_cache[key] = response;
98-
co_return std::move(response);
99+
auto cached_instance{f$instance_deserialize<InstanceType>(
100+
string{(*maybe_string.opt_value).value.data(), static_cast<string::size_type>((*maybe_string.opt_value).value.size())}, {})};
101+
request_cache.emplace(std::move(key), cached_instance);
102+
co_return std::move(cached_instance);
99103
}
100104

101105
inline kphp::coro::task<bool> f$instance_cache_update_ttl(string key, int64_t ttl = 0) noexcept {
102106
if (ttl < 0) [[unlikely]] {
103-
kphp::log::warning("ttl value '{}' < 0 is noop for key '{}'", ttl, key.c_str());
107+
kphp::log::warning("ttl can't be negative: ttl -> {}, key -> {}", ttl, key.c_str());
104108
co_return false;
105109
}
106-
107110
if (ttl > std::numeric_limits<uint32_t>::max()) [[unlikely]] {
108-
kphp::log::warning("ttl value '{}' exceeds maximum allowed value '{}', it will be stored forever for key: {}", ttl, std::numeric_limits<uint32_t>::max(),
109-
key.c_str());
111+
kphp::log::warning("ttl exceeds maximum allowed value, key will be stored forever: ttl -> {}, max -> {}, key -> {}", ttl,
112+
std::numeric_limits<uint32_t>::max(), key.c_str());
110113
ttl = 0;
111114
}
115+
112116
tl::TLBuffer tlb;
113-
tl::CacheUpdateTtl{.key{tl::string{std::string_view{key.c_str(), key.size()}}}, .ttl{tl::u32{static_cast<uint32_t>(ttl)}}}.store(tlb);
117+
tl::CacheUpdateTtl{.key = tl::string{.value = {key.c_str(), key.size()}}, .ttl = tl::u32{.value = static_cast<uint32_t>(ttl)}}.store(tlb);
114118

115119
auto query{co_await f$component_client_send_request(
116120
string{kphp::instance_cache::details::COMPONENT_NAME.data(), static_cast<string::size_type>(kphp::instance_cache::details::COMPONENT_NAME.size())},
117121
string{tlb.data(), static_cast<string::size_type>(tlb.size())})};
118-
119122
if (query.is_null()) [[unlikely]] {
120123
co_return false;
121124
}
122125

123-
auto resp{co_await f$component_client_fetch_response(query)};
126+
auto response{co_await f$component_client_fetch_response(std::move(query))};
124127
tlb.clean();
125-
tlb.store_bytes({resp.c_str(), static_cast<size_t>(resp.size())});
126-
tl::Bool tl_resp;
127-
kphp::log::assertion(tl_resp.fetch(tlb));
128+
tlb.store_bytes({response.c_str(), static_cast<size_t>(response.size())});
128129

129-
co_return tl_resp.value;
130+
tl::Bool tl_bool{};
131+
kphp::log::assertion(tl_bool.fetch(tlb));
132+
co_return tl_bool.value;
130133
}
131134

132135
inline kphp::coro::task<bool> f$instance_cache_delete(string key) noexcept {
133136
InstanceCacheInstanceState::get().request_cache.erase(key);
134137

135138
tl::TLBuffer tlb;
136-
tl::CacheDelete{.key{tl::string{std::string_view{key.c_str(), key.size()}}}}.store(tlb);
139+
tl::CacheDelete{.key = tl::string{.value = {key.c_str(), key.size()}}}.store(tlb);
137140

138141
auto query{co_await f$component_client_send_request(
139142
string{kphp::instance_cache::details::COMPONENT_NAME.data(), static_cast<string::size_type>(kphp::instance_cache::details::COMPONENT_NAME.size())},
140143
string{tlb.data(), static_cast<string::size_type>(tlb.size())})};
141-
142144
if (query.is_null()) [[unlikely]] {
143145
co_return false;
144146
}
145147

146-
auto resp{co_await f$component_client_fetch_response(query)};
148+
auto response{co_await f$component_client_fetch_response(std::move(query))};
147149
tlb.clean();
148-
tlb.store_bytes({resp.c_str(), static_cast<size_t>(resp.size())});
149-
tl::Bool tl_resp;
150-
kphp::log::assertion(tl_resp.fetch(tlb));
150+
tlb.store_bytes({response.c_str(), static_cast<size_t>(response.size())});
151151

152-
co_return tl_resp.value;
152+
tl::Bool tl_bool{};
153+
kphp::log::assertion(tl_bool.fetch(tlb));
154+
co_return tl_bool.value;
153155
}

runtime-light/stdlib/instance-cache/instance-cache-state.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
#include "runtime-light/state/instance-state.h"
88

99
InstanceCacheInstanceState& InstanceCacheInstanceState::get() noexcept {
10-
return InstanceState::get().instance_cache_state;
10+
return InstanceState::get().instance_cache_instance_state;
1111
}

runtime-light/stdlib/instance-cache/instance-cache-state.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44

55
#pragma once
66

7-
#include "common/mixin/not_copyable.h"
7+
#include <cstddef>
88

9+
#include "common/mixin/not_copyable.h"
910
#include "runtime-common/core/allocator/script-allocator.h"
1011
#include "runtime-common/core/runtime-core.h"
1112
#include "runtime-common/core/std/containers.h"
1213

1314
struct InstanceCacheInstanceState final : private vk::not_copyable {
14-
InstanceCacheInstanceState() noexcept = default;
15-
16-
kphp::stl::unordered_map<string, mixed, kphp::memory::script_allocator, decltype([](const string& s) { return static_cast<size_t>(s.hash()); })>
15+
kphp::stl::unordered_map<string, mixed, kphp::memory::script_allocator, decltype([](const string& s) noexcept { return static_cast<size_t>(s.hash()); })>
1716
request_cache;
17+
18+
InstanceCacheInstanceState() noexcept = default;
1819
static InstanceCacheInstanceState& get() noexcept;
1920
};

runtime-light/tl/tl-functions.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -159,29 +159,4 @@ bool rpcInvokeReq::fetch(tl::TLBuffer& tlb) noexcept {
159159
return ok;
160160
}
161161

162-
// ===== CACHE =====
163-
164-
void CacheStore::store(TLBuffer& tlb) const noexcept {
165-
tl::details::magic{.value = CACHE_STORE_MAGIC}.store(tlb);
166-
key.store(tlb);
167-
value.store(tlb);
168-
ttl.store(tlb);
169-
}
170-
171-
void CacheUpdateTtl::store(TLBuffer& tlb) const noexcept {
172-
tl::details::magic{.value = CACHE_UPDATE_TTL_MAGIC}.store(tlb);
173-
key.store(tlb);
174-
ttl.store(tlb);
175-
}
176-
177-
void CacheDelete::store(TLBuffer& tlb) const noexcept {
178-
tl::details::magic{.value = CACHE_DELETE_MAGIC}.store(tlb);
179-
key.store(tlb);
180-
}
181-
182-
void CacheFetch::store(TLBuffer& tlb) const noexcept {
183-
tl::details::magic{.value = CACHE_FETCH_MAGIC}.store(tlb);
184-
key.store(tlb);
185-
}
186-
187162
} // namespace tl

runtime-light/tl/tl-functions.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,26 +195,41 @@ struct CacheStore final {
195195
tl::string value;
196196
tl::u32 ttl;
197197

198-
void store(TLBuffer& tlb) const noexcept;
198+
void store(TLBuffer& tlb) const noexcept {
199+
tl::details::magic{.value = CACHE_STORE_MAGIC}.store(tlb);
200+
key.store(tlb);
201+
value.store(tlb);
202+
ttl.store(tlb);
203+
}
199204
};
200205

201206
struct CacheUpdateTtl final {
202207
tl::string key;
203208
tl::u32 ttl;
204209

205-
void store(TLBuffer& tlb) const noexcept;
210+
void store(TLBuffer& tlb) const noexcept {
211+
tl::details::magic{.value = CACHE_UPDATE_TTL_MAGIC}.store(tlb);
212+
key.store(tlb);
213+
ttl.store(tlb);
214+
}
206215
};
207216

208217
struct CacheDelete final {
209218
tl::string key;
210219

211-
void store(TLBuffer& tlb) const noexcept;
220+
void store(TLBuffer& tlb) const noexcept {
221+
tl::details::magic{.value = CACHE_DELETE_MAGIC}.store(tlb);
222+
key.store(tlb);
223+
}
212224
};
213225

214226
struct CacheFetch final {
215227
tl::string key;
216228

217-
void store(TLBuffer& tlb) const noexcept;
229+
void store(TLBuffer& tlb) const noexcept {
230+
tl::details::magic{.value = CACHE_FETCH_MAGIC}.store(tlb);
231+
key.store(tlb);
232+
}
218233
};
219234

220235
} // namespace tl

0 commit comments

Comments
 (0)