|
4 | 4 |
|
5 | 5 | #pragma once |
6 | 6 |
|
| 7 | +#include <cstddef> |
7 | 8 | #include <cstdint> |
| 9 | +#include <functional> |
8 | 10 | #include <optional> |
9 | 11 | #include <string_view> |
10 | 12 |
|
11 | 13 | #include "runtime-common/core/allocator/script-allocator.h" |
12 | 14 | #include "runtime-common/core/std/containers.h" |
13 | 15 |
|
14 | | -#define SAVE_BUILTIN_CALL_STATS(builtin_name, builtin_call) \ |
15 | | - (!runtime_builtins_stats::impl_::is_request_stats_enabled ? 0 : (*runtime_builtins_stats::request_stats.stats)[std::string_view(builtin_name)]++, \ |
16 | | - builtin_call) |
| 16 | +template<> |
| 17 | +struct std::hash<kphp::stl::string<kphp::memory::script_allocator>> { |
| 18 | + std::size_t operator()(const kphp::stl::string<kphp::memory::script_allocator>& s) const noexcept { |
| 19 | + std::string_view view{s.c_str(), s.size()}; |
| 20 | + return std::hash<std::string_view>{}(view); |
| 21 | + } |
| 22 | +}; |
17 | 23 |
|
18 | 24 | namespace runtime_builtins_stats { |
19 | | - |
20 | | -namespace impl_ { |
21 | | -inline bool is_request_stats_enabled = false; |
22 | | -} // namespace impl_ |
23 | | - |
24 | 25 | inline bool is_server_option_enabled = false; |
25 | 26 |
|
26 | 27 | struct request_stats_t { |
27 | | - std::optional<kphp::stl::unordered_map<std::string_view, int64_t, kphp::memory::script_allocator>> stats{}; |
| 28 | + kphp::stl::unordered_map<std::string_view, int64_t, kphp::memory::script_allocator> builtin_stats{}; |
| 29 | + kphp::stl::unordered_map<kphp::stl::string<kphp::memory::script_allocator>, int64_t, kphp::memory::script_allocator> virtual_builtin_stats{}; |
28 | 30 | }; |
29 | 31 |
|
30 | | -inline request_stats_t request_stats; |
| 32 | +inline std::optional<request_stats_t> request_stats; |
31 | 33 |
|
32 | 34 | inline void init_request_stats() noexcept { |
33 | | - php_assert(!request_stats.stats.has_value()); |
34 | | - impl_::is_request_stats_enabled = is_server_option_enabled; |
35 | | - request_stats.stats.emplace(); |
| 35 | + php_assert(!request_stats.has_value()); |
| 36 | + if (is_server_option_enabled) { |
| 37 | + request_stats.emplace(); |
| 38 | + } |
36 | 39 | } |
37 | 40 |
|
38 | 41 | inline void reset_request_stats() noexcept { |
39 | | - php_assert(request_stats.stats.has_value()); |
40 | | - impl_::is_request_stats_enabled = false; |
41 | | - request_stats.stats.reset(); |
| 42 | + request_stats.reset(); |
42 | 43 | } |
43 | 44 |
|
| 45 | +inline void save_virtual_builtin_call_stats(const kphp::stl::string<kphp::memory::script_allocator>& builtin_call) noexcept { |
| 46 | + if (runtime_builtins_stats::request_stats.has_value()) { |
| 47 | + (*runtime_builtins_stats::request_stats).virtual_builtin_stats[builtin_call]++; |
| 48 | + } |
| 49 | +} |
44 | 50 | } // namespace runtime_builtins_stats |
| 51 | + |
| 52 | +#define SAVE_BUILTIN_CALL_STATS(builtin_name, builtin_call) \ |
| 53 | + (!runtime_builtins_stats::request_stats.has_value() ? 0 : (*runtime_builtins_stats::request_stats).builtin_stats[std::string_view(builtin_name)]++, \ |
| 54 | + builtin_call) |
0 commit comments