Skip to content

Commit 19ce609

Browse files
authored
[k2] remove useless heap allocations from kphp::log::assertion (#1621)
* Temporary disable all logging in runtime-allocator * Remove useless heap allocations from kphp::log::assertion --------- Signed-off-by: Petr Shumilov <p.shumilov@vkteam.ru>
1 parent 8dbaaa1 commit 19ce609

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

runtime-light/allocator/runtime-light-allocator.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ RuntimeAllocator& RuntimeAllocator::get() noexcept {
1616

1717
RuntimeAllocator::RuntimeAllocator(size_t script_mem_size, size_t min_extra_mem_size, size_t oom_handling_mem_size)
1818
: m_min_extra_mem_size(min_extra_mem_size) {
19-
kphp::log::debug("create runtime allocator -> {:p}: script memory -> {}, oom handling size -> {}", reinterpret_cast<void*>(this), script_mem_size,
20-
oom_handling_mem_size);
19+
// kphp::log::debug("create runtime allocator -> {:p}: script memory -> {}, oom handling size -> {}", reinterpret_cast<void*>(this), script_mem_size,
20+
// oom_handling_mem_size);
2121
void* buffer{alloc_global_memory(script_mem_size)};
2222
memory_resource.init(buffer, script_mem_size, oom_handling_mem_size);
2323
}
2424

2525
void RuntimeAllocator::init(void* buffer, size_t script_mem_size, size_t oom_handling_mem_size) {
2626
kphp::log::assertion(buffer != nullptr);
27-
kphp::log::debug("init runtime allocator -> {:p}: buffer -> {:p}, script memory -> {}, oom handling size -> {}", reinterpret_cast<void*>(this), buffer,
28-
script_mem_size, oom_handling_mem_size);
27+
// kphp::log::debug("init runtime allocator -> {:p}: buffer -> {:p}, script memory -> {}, oom handling size -> {}", reinterpret_cast<void*>(this), buffer,
28+
// script_mem_size, oom_handling_mem_size);
2929
memory_resource.init(buffer, script_mem_size, oom_handling_mem_size);
3030
}
3131

3232
void RuntimeAllocator::free() {
33-
kphp::log::debug("free runtime allocator -> {:p}", reinterpret_cast<void*>(this));
33+
// kphp::log::debug("free runtime allocator -> {:p}", reinterpret_cast<void*>(this));
3434
auto* extra_memory{memory_resource.get_extra_memory_head()};
3535
while (extra_memory->get_pool_payload_size() != 0) {
3636
auto* extra_memory_to_release{extra_memory};
@@ -111,7 +111,7 @@ void RuntimeAllocator::request_extra_memory(size_t requested_size) noexcept {
111111
// Take into account internal layout of `memory_resource::extra_memory_pool`
112112
extra_mem_size += sizeof(memory_resource::extra_memory_pool);
113113

114-
kphp::log::debug("requested extra memory pool with size {} bytes, will be allocated {} bytes", requested_size, extra_mem_size);
114+
// kphp::log::debug("requested extra memory pool with size {} bytes, will be allocated {} bytes", requested_size, extra_mem_size);
115115

116116
auto* extra_mem{alloc_global_memory(extra_mem_size)};
117117
memory_resource.add_extra_memory(new (extra_mem) memory_resource::extra_memory_pool{extra_mem_size});

runtime-light/stdlib/diagnostics/logs.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ namespace kphp::log {
2121

2222
namespace impl {
2323

24+
static constexpr size_t DEFAULT_LOG_BUFFER_SIZE = 2048UZ;
25+
2426
template<typename... Args>
2527
void log(level level, std::optional<std::span<void* const>> trace, std::format_string<impl::wrapped_arg_t<Args>...> fmt, Args&&... args) noexcept {
26-
static constexpr size_t LOG_BUFFER_SIZE = 2048UZ;
27-
std::array<char, LOG_BUFFER_SIZE> log_buffer; // NOLINT
28+
std::array<char, DEFAULT_LOG_BUFFER_SIZE> log_buffer; // NOLINT
2829
size_t message_size{impl::format_log_message(log_buffer, fmt, std::forward<Args>(args)...)};
2930
auto message{std::string_view{log_buffer.data(), static_cast<std::string_view::size_type>(message_size)}};
3031

@@ -67,7 +68,10 @@ void log(level level, std::optional<std::span<void* const>> trace, std::format_s
6768
// If assertion is modified, the backtrace algorithm should be updated accordingly
6869
inline void assertion(bool condition, const std::source_location& location = std::source_location::current()) noexcept {
6970
if (!condition) [[unlikely]] {
70-
impl::log(level::error, std::nullopt, "assertion failed at {}:{}", location.file_name(), location.line());
71+
std::array<char, impl::DEFAULT_LOG_BUFFER_SIZE> log_buffer; // NOLINT
72+
size_t message_size{impl::format_log_message(log_buffer, "assertion failed at {}:{}", location.file_name(), location.line())};
73+
auto message{std::string_view{log_buffer.data(), static_cast<std::string_view::size_type>(message_size)}};
74+
k2::log(std::to_underlying(level::error), message, {});
7175
k2::exit(1);
7276
}
7377
}

0 commit comments

Comments
 (0)