-
Notifications
You must be signed in to change notification settings - Fork 116
add blazingly fast instance_cache implementation for K2 #1683
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
denisichh
wants to merge
37
commits into
master
Choose a base branch
from
dzubarev/k2/add-instance-cache
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
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
a5d5a51
added instance cache + fix tests
denisichh deed634
fix
denisichh e01271b
added kphp::visitors to C and C*
denisichh bf451d4
fix
denisichh 830c8b4
xif
denisichh 661a80d
fix?
denisichh 831c506
tmp fix
denisichh 2754ed3
added kphp::visitors to DummyVisitorMethods
denisichh 55367ae
removed useless warnings
denisichh c892496
minor fix
denisichh a8e1501
fix
denisichh 75894c6
code_style++?
denisichh f87c14a
fmt + minor fixes and optimisations
denisichh 55e951d
added alignment for ic_store
denisichh 540acad
sigsegv fix
denisichh d1951b1
fmt
denisichh fdc8bb3
fix
denisichh 923f0c6
fix
denisichh 4faef3c
added new test
denisichh 57b4641
-
denisichh 3d9988e
removed unused include
denisichh be85ca1
k2api updated
denisichh 7cdaf87
added: k2_free_shared_memory + removed: fetch optimisation, part of t…
denisichh 2ef75f8
fix
denisichh dd93926
update k2api
denisichh 710b601
fix instance_cache_store
denisichh 064bfc5
update k2api
denisichh 25a4529
fix
denisichh f4a9849
optional-based clone_in/alloc_in + get_class_name_hash()
denisichh 73134a1
update allocators
apolyakov c87d381
add missing define to compiler
apolyakov a82bd97
switch instance_deep_copy/estimate_visitor to allocator substitution
denisichh 2a52817
add missing include
apolyakov 475e2a6
remove smth
denisichh 9faa830
remove lambda from array<T>::copy_from
denisichh 74fe10b
move VISITED_INSTANCE_MASK
denisichh 7958a63
renames and minor fixes
denisichh 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 |
|---|---|---|
| @@ -1,13 +1,10 @@ | ||
| <?php | ||
|
|
||
| /** @kphp-extern-func-info cpp_template_call interruptible */ | ||
| /** @kphp-extern-func-info cpp_template_call */ | ||
| function instance_cache_fetch(string $type, string $key, bool $even_if_expired = false) ::: instance<^1>; | ||
|
|
||
| /** @kphp-extern-func-info interruptible */ | ||
| function instance_cache_store(string $key, object $value, int $ttl = 0) ::: bool; | ||
|
|
||
| /** @kphp-extern-func-info interruptible */ | ||
| function instance_cache_update_ttl(string $key, int $ttl = 0) ::: bool; | ||
|
|
||
| /** @kphp-extern-func-info interruptible */ | ||
| function instance_cache_delete(string $key) ::: bool; |
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
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
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 |
|---|---|---|
| @@ -1,32 +1,58 @@ | ||
| // Compiler for PHP (aka KPHP) | ||
| // Copyright (c) 2024 LLC «V Kontakte» | ||
| // Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
| // Compiler for PHP (aka KPHP) | ||
| // Copyright (c) 2024 LLC «V Kontakte» | ||
| // Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #ifdef RUNTIME_LIGHT | ||
| #include <cstdint> | ||
| #include <functional> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| #include "common/containers/final_action.h" | ||
| #include "runtime-common/core/allocator/pool-allocator.h" | ||
| #endif | ||
|
|
||
| struct RuntimeAllocator final { | ||
| #ifdef RUNTIME_LIGHT | ||
| private: | ||
| kphp::memory::pool_allocator m_allocator; | ||
| std::reference_wrapper<kphp::memory::pool_allocator> m_allocator_ref{m_allocator}; | ||
| #endif | ||
|
|
||
| public: | ||
| static auto get() noexcept -> RuntimeAllocator&; | ||
|
|
||
| RuntimeAllocator() = default; | ||
| #ifdef RUNTIME_LIGHT | ||
| RuntimeAllocator(size_t script_mem_size, size_t min_extra_mem_size, size_t oom_handling_mem_size) noexcept; | ||
|
|
||
| #else | ||
| RuntimeAllocator() = default; | ||
| auto init(void* buffer, size_t script_mem_size, size_t oom_handling_mem_size) noexcept -> void; | ||
| #endif | ||
|
|
||
| auto free() noexcept -> void; | ||
|
|
||
| auto alloc_script_memory(size_t size) noexcept -> void*; | ||
| auto calloc_script_memory(size_t size) noexcept -> void*; | ||
| auto realloc_script_memory(void* mem, size_t new_size, size_t old_size) noexcept -> void*; | ||
| auto free_script_memory(void* mem, size_t size) noexcept -> void; | ||
|
|
||
| #ifdef RUNTIME_LIGHT | ||
| auto get_memory_resource() noexcept -> memory_resource::unsynchronized_pool_resource& { | ||
| return m_allocator.get_memory_resource(); | ||
| return m_allocator_ref.get().get_memory_resource(); | ||
| } | ||
|
|
||
| // The callback must run synchronously without yielding. Objects allocated by it | ||
| // may outlive the scope, but later operations that allocate or deallocate their | ||
| // memory must install the same allocator. The replacement must outlive the callback. | ||
| template<typename callback_type, | ||
| std::enable_if_t<std::is_nothrow_invocable_v<callback_type> && std::is_same_v<std::invoke_result_t<callback_type>, void>, int32_t> = 0> | ||
| auto with_allocator(kphp::memory::pool_allocator& replacement, callback_type&& callback) noexcept -> void { | ||
| const auto previous_allocator{std::exchange(m_allocator_ref, std::ref(replacement))}; | ||
| const auto restore_allocator{vk::finally([this, previous_allocator]() noexcept { m_allocator_ref = previous_allocator; })}; | ||
| std::invoke(std::forward<callback_type>(callback)); | ||
| } | ||
| #endif | ||
| }; |
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
116 changes: 116 additions & 0 deletions
116
runtime-common/stdlib/visitors/instance-deep-basic-visitor.h
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,116 @@ | ||
| // Compiler for PHP (aka KPHP) | ||
| // Copyright (c) 2026 LLC «V Kontakte» | ||
| // Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdint> | ||
| #include <tuple> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| #include "common/mixin/not_copyable.h" | ||
| #include "common/type_traits/list_of_types.h" | ||
| #include "runtime-common/core/runtime-core.h" | ||
|
|
||
| namespace kphp::visitors { | ||
|
|
||
| // CRTP base for visitors that traverse an instance graph via compiler-generated accept() methods. | ||
| // Every field is dispatched to Child::process, and a false result from any field is accumulated into is_ok(). | ||
| template<typename Child> | ||
| class instance_deep_basic_visitor : vk::not_copyable { | ||
| public: | ||
| template<typename T> | ||
| void operator()(const char* /*unused*/, T&& value) noexcept { | ||
| const bool is_ok{child_.process(std::forward<T>(value))}; | ||
| is_ok_ = is_ok_ && is_ok; | ||
| } | ||
|
|
||
| template<typename T> | ||
| bool process(T& /*unused*/) noexcept { | ||
| return true; | ||
| } | ||
|
|
||
| template<typename T> | ||
| bool process(Optional<T>& value) noexcept { | ||
| return !value.has_value() || child_.process(value.val()); | ||
| } | ||
|
|
||
| template<typename I> | ||
| bool process(class_instance<I>& instance) noexcept { | ||
| if (!instance.is_null()) { | ||
| instance.get()->accept(child_); | ||
| return child_.is_ok(); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| template<typename... Args> | ||
| bool process(std::tuple<Args...>& value) noexcept { | ||
| return process_tuple(value); | ||
| } | ||
|
|
||
| template<size_t... Is, typename... T> | ||
| bool process(shape<std::index_sequence<Is...>, T...>& value) noexcept { | ||
| const bool child_res[]{child_.process(value.template get<Is>())...}; | ||
| return std::all_of(std::begin(child_res), std::end(child_res), [](bool r) noexcept { return r; }); | ||
| } | ||
|
|
||
| bool process(mixed& value) noexcept { | ||
| if (value.is_string()) { | ||
| return child_.process(value.as_string()); | ||
| } else if (value.is_array()) { | ||
| return child_.process(value.as_array()); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool is_ok() const noexcept { | ||
| return is_ok_; | ||
| } | ||
|
|
||
| ExtraRefCnt get_memory_ref_cnt() const noexcept { | ||
| return memory_ref_cnt_; | ||
| } | ||
|
|
||
| protected: | ||
| template<class T> | ||
| static constexpr bool is_primitive{vk::is_type_in_list<T, int64_t, double, bool, Optional<int64_t>, Optional<double>, Optional<bool>>::value}; | ||
|
|
||
| explicit instance_deep_basic_visitor(Child& child, ExtraRefCnt memory_ref_cnt = ExtraRefCnt::extra_ref_cnt_value(0)) noexcept | ||
| : memory_ref_cnt_{memory_ref_cnt}, | ||
| child_{child} {} | ||
|
|
||
| template<typename Iterator> | ||
| bool process_range(Iterator first, Iterator last) noexcept { | ||
| bool res{true}; | ||
| for (; first != last; ++first) { | ||
| if (!child_.process(first.get_value())) { | ||
| res = false; | ||
| } | ||
| if (first.is_string_key() && !child_.process(first.get_string_key())) { | ||
| res = false; | ||
| } | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| private: | ||
| template<size_t Index = 0, typename... Args> | ||
| std::enable_if_t<Index != sizeof...(Args), bool> process_tuple(std::tuple<Args...>& value) noexcept { | ||
| bool res = child_.process(std::get<Index>(value)); | ||
| return process_tuple<Index + 1>(value) && res; | ||
| } | ||
|
|
||
| template<size_t Index = 0, typename... Args> | ||
| std::enable_if_t<Index == sizeof...(Args), bool> process_tuple(std::tuple<Args...>& /*unused*/) noexcept { | ||
| return true; | ||
| } | ||
|
|
||
| bool is_ok_{true}; | ||
| const ExtraRefCnt memory_ref_cnt_{ExtraRefCnt::extra_ref_cnt_value(0)}; | ||
| Child& child_; | ||
| }; | ||
|
|
||
| } // namespace kphp::visitors |
Oops, something went wrong.
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.