Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions runtime-common/core/core-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct RuntimeContext final : vk::not_copyable {
int32_t show_migration_php8_warning{};
int32_t php_disable_warnings{};
uint32_t empty_obj_count{};
void* empty_values{nullptr};

string_buffer_lib_context sb_lib_context{};
string_buffer static_SB{};
Expand Down
37 changes: 28 additions & 9 deletions runtime-common/core/core-types/definition/mixed.inl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

#pragma once

#include <cstddef>
#include <typeindex>

#include "common/algorithms/find.h"
#include "common/smart_ptrs/intrusive_ptr.h"

#include "runtime-common/core/allocator/script-allocator.h"
#include "runtime-common/core/class-instance/refcountable-php-classes.h"
#include "runtime-common/core/runtime-core.h"
#include "runtime-common/core/std/containers.h"
#include "runtime-common/core/utils/kphp-assert-core.h"
#include "runtime-common/core/utils/migration-php8.h"

#ifndef INCLUDED_FROM_KPHP_CORE
Expand Down Expand Up @@ -177,18 +184,30 @@ template<typename T>
T& mixed::empty_value() noexcept {
static_assert(vk::is_type_in_list<T, bool, int64_t, double, string, mixed, array<mixed>>{} || is_type_acceptable_for_mixed<T>::value, "unsupported type");

static T value;
value = T{};
return value;
auto& ctx{RuntimeContext::get()};

using type2value_t = kphp::stl::unordered_map<std::type_index, void*, kphp::memory::script_allocator>;
if (ctx.empty_values == nullptr) {
auto* raw_mem{RuntimeAllocator::get().alloc_script_memory(sizeof(type2value_t))};
php_assert(raw_mem);
ctx.empty_values = new (raw_mem) type2value_t{};
reinterpret_cast<type2value_t*>(ctx.empty_values)->reserve(6); // bool, int64_t, double, string, mixed, array<mixed>
}

auto* type2value{reinterpret_cast<type2value_t*>(ctx.empty_values)};
const auto it{type2value->find(std::type_index(typeid(T)))};
if (it != type2value->end()) {
*reinterpret_cast<T*>(it->second) = T{};
return *reinterpret_cast<T*>(it->second);
}

auto* raw_mem{RuntimeAllocator::get().alloc_script_memory(sizeof(T))};
php_assert(raw_mem);
return *reinterpret_cast<T*>(type2value->insert({std::type_index(typeid(T)), new (raw_mem) T{}}).first->second);
}

inline void mixed::reset_empty_values() noexcept {
empty_value<bool>();
empty_value<int64_t>();
empty_value<double>();
empty_value<string>();
empty_value<mixed>();
empty_value<array<mixed>>();
RuntimeContext::get().empty_values = nullptr;
}

template<class T>
Expand Down
Loading