-
Notifications
You must be signed in to change notification settings - Fork 114
[k2] speed up compiling #1659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shamzik
wants to merge
6
commits into
master
Choose a base branch
from
kshamazov/speed-up
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[k2] speed up compiling #1659
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Compiler for PHP (aka KPHP) | ||
| // Copyright (c) 2026 LLC «V Kontakte» | ||
| // Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
|
||
| #include "runtime-light/stdlib/diagnostics/detail/logs.h" | ||
|
|
||
| #include <cstddef> | ||
| #include <format> | ||
| #include <iterator> | ||
| #include <span> | ||
| #include <string_view> | ||
|
|
||
| #include "runtime-light/stdlib/diagnostics/backtrace.h" | ||
|
|
||
| namespace kphp::log::impl { | ||
|
|
||
| namespace { | ||
|
|
||
| struct truncating_iterator { | ||
|
Shamzik marked this conversation as resolved.
|
||
| using iterator_category = std::output_iterator_tag; | ||
| using value_type = void; | ||
| using difference_type = std::ptrdiff_t; | ||
| using pointer = void; | ||
| using reference = void; | ||
|
|
||
| char* dst; | ||
| size_t max_size; | ||
| size_t count = 0; | ||
|
PetrShumilov marked this conversation as resolved.
Outdated
|
||
|
|
||
| struct proxy { | ||
| truncating_iterator& it; | ||
| void operator=(char c) const { // NOLINT | ||
| if (it.count < it.max_size) { | ||
| *it.dst++ = c; | ||
| } | ||
| it.count++; | ||
| } | ||
| }; | ||
|
|
||
| proxy operator*() { | ||
| return proxy{*this}; | ||
| } | ||
| truncating_iterator& operator++() { | ||
| return *this; | ||
| } | ||
| truncating_iterator operator++(int) { // NOLINT | ||
| return *this; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| size_t resolve_log_trace(std::span<char> trace_buffer, std::span<void* const> raw_trace) noexcept { | ||
| if (trace_buffer.empty()) { | ||
| return 0; | ||
| } | ||
|
|
||
| if (auto backtrace_symbols{kphp::diagnostic::backtrace_symbols(raw_trace)}; !backtrace_symbols.empty()) { | ||
| const auto [trace_out, _]{std::format_to_n(trace_buffer.data(), trace_buffer.size() - 1, "\n{}", backtrace_symbols)}; | ||
| *trace_out = '\0'; | ||
| return std::distance(trace_buffer.data(), trace_out); | ||
| } else if (auto backtrace_addresses{kphp::diagnostic::backtrace_addresses(raw_trace)}; !backtrace_addresses.empty()) { | ||
| const auto [trace_out, _]{std::format_to_n(trace_buffer.data(), trace_buffer.size() - 1, "{}", backtrace_addresses)}; | ||
| *trace_out = '\0'; | ||
| return std::distance(trace_buffer.data(), trace_out); | ||
| } else { | ||
| static constexpr std::string_view DEFAULT_TRACE = "[]"; | ||
| const auto [trace_out, _]{std::format_to_n(trace_buffer.data(), trace_buffer.size() - 1, "{}", DEFAULT_TRACE)}; | ||
| *trace_out = '\0'; | ||
| return std::distance(trace_buffer.data(), trace_out); | ||
| } | ||
| } | ||
|
|
||
| size_t format_log_message(std::span<char> message_buffer, std::string_view fmt, std::format_args args) noexcept { | ||
| auto it{std::vformat_to(truncating_iterator{message_buffer.data(), message_buffer.size() - 1}, fmt, args)}; | ||
| *it.dst = '\0'; | ||
| return std::distance(message_buffer.data(), it.dst); | ||
| } | ||
|
|
||
| } // namespace kphp::log::impl | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Compiler for PHP (aka KPHP) | ||
| // Copyright (c) 2026 LLC «V Kontakte» | ||
| // Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
|
||
| #include "runtime-light/stdlib/diagnostics/logs.h" | ||
|
|
||
| #include <array> | ||
| #include <cstddef> | ||
| #include <source_location> | ||
| #include <string_view> | ||
| #include <utility> | ||
|
|
||
| #include "runtime-light/k2-platform/k2-api.h" | ||
| #include "runtime-light/stdlib/diagnostics/detail/logs.h" | ||
|
|
||
| void kphp::log::assertion(bool condition, const std::source_location& location) noexcept { | ||
| if (!condition) [[unlikely]] { | ||
| std::array<char, impl::DEFAULT_LOG_BUFFER_SIZE> log_buffer; // NOLINT | ||
| size_t message_size{ | ||
| impl::format_log_message(log_buffer, "assertion failed at {}:{}", | ||
| std::make_format_args(kphp::log::impl::unmove(location.file_name()), kphp::log::impl::unmove(location.line())))}; | ||
| auto message{std::string_view{log_buffer.data(), static_cast<std::string_view::size_type>(message_size)}}; | ||
| k2::log(std::to_underlying(level::error), message, {}); | ||
| k2::exit(1); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,10 +23,16 @@ namespace impl { | |
|
|
||
| static constexpr size_t DEFAULT_LOG_BUFFER_SIZE = 2048UZ; | ||
|
|
||
| template<typename T> | ||
| const T& unmove(T&& x) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't look good at all. It may lead to dangling references all around. Let's try to get rid of it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed |
||
| return x; | ||
| } | ||
|
|
||
| template<typename... Args> | ||
| void log(level level, std::optional<std::span<void* const>> trace, std::format_string<impl::wrapped_arg_t<Args>...> fmt, Args&&... args) noexcept { | ||
| std::array<char, DEFAULT_LOG_BUFFER_SIZE> log_buffer; // NOLINT | ||
| size_t message_size{impl::format_log_message(log_buffer, fmt, std::forward<Args>(args)...)}; | ||
| size_t message_size{ | ||
| impl::format_log_message(log_buffer, fmt.get(), std::make_format_args(kphp::log::impl::unmove(impl::wrap_log_argument(std::forward<Args>(args)))...))}; | ||
| auto message{std::string_view{log_buffer.data(), static_cast<std::string_view::size_type>(message_size)}}; | ||
|
|
||
| auto opt_tags{ | ||
|
|
@@ -66,15 +72,7 @@ void log(level level, std::optional<std::span<void* const>> trace, std::format_s | |
|
|
||
| // The backtrace algorithm relies on the fact that assertion does not call backtrace. | ||
| // If assertion is modified, the backtrace algorithm should be updated accordingly | ||
| inline void assertion(bool condition, const std::source_location& location = std::source_location::current()) noexcept { | ||
| if (!condition) [[unlikely]] { | ||
| std::array<char, impl::DEFAULT_LOG_BUFFER_SIZE> log_buffer; // NOLINT | ||
| size_t message_size{impl::format_log_message(log_buffer, "assertion failed at {}:{}", location.file_name(), location.line())}; | ||
| auto message{std::string_view{log_buffer.data(), static_cast<std::string_view::size_type>(message_size)}}; | ||
| k2::log(std::to_underlying(level::error), message, {}); | ||
| k2::exit(1); | ||
| } | ||
| } | ||
| void assertion(bool condition, const std::source_location& location = std::source_location::current()) noexcept; | ||
|
|
||
| template<typename... Args> | ||
| [[noreturn]] void error(std::format_string<impl::wrapped_arg_t<Args>...> fmt, Args&&... args) noexcept { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.