1010#include " runtime-light/k2-platform/k2-api.h"
1111#include " runtime-light/stdlib/diagnostics/logs.h"
1212
13+ namespace {
14+ // TODO: make it depend on max chunk size, e.g. MIN_EXTRA_MEM_SIZE = f(MAX_CHUNK_SIZE);
15+ constexpr auto MIN_EXTRA_MEM_SIZE = static_cast <size_t >(1024U * 1024U ); // extra mem size should be greater than max chunk block size
16+ constexpr auto EXTRA_MEMORY_MULTIPLIER = 2 ;
17+
18+ void request_extra_memory (size_t requested_size) noexcept {
19+ const size_t extra_mem_size{std::max (MIN_EXTRA_MEM_SIZE, EXTRA_MEMORY_MULTIPLIER * requested_size)};
20+ auto & allocator{RuntimeAllocator::get ()};
21+ auto * extra_mem{allocator.alloc_global_memory (extra_mem_size)};
22+ allocator.memory_resource .add_extra_memory (new (extra_mem) memory_resource::extra_memory_pool{extra_mem_size});
23+ }
24+
25+ } // namespace
26+
1327RuntimeAllocator& RuntimeAllocator::get () noexcept {
1428 return AllocatorState::get_mutable ().allocator ;
1529}
1630
17- RuntimeAllocator::RuntimeAllocator (size_t script_mem_size, size_t min_extra_mem_size, size_t oom_handling_mem_size)
18- : min_extra_mem_size(min_extra_mem_size) {
31+ RuntimeAllocator::RuntimeAllocator (size_t script_mem_size, size_t oom_handling_mem_size) {
1932 kphp::log::debug (" create runtime allocator -> {:p}: script memory -> {}, oom handling size -> {}" , reinterpret_cast <void *>(this ), script_mem_size,
2033 oom_handling_mem_size);
2134 void * buffer{alloc_global_memory (script_mem_size)};
@@ -44,7 +57,7 @@ void* RuntimeAllocator::alloc_script_memory(size_t size) noexcept {
4457 kphp::log::assertion (size != 0 );
4558 void * mem{memory_resource.allocate (size)};
4659 if (mem == nullptr ) [[unlikely]] {
47- request_extra_memory (size, min_extra_mem_size );
60+ request_extra_memory (size);
4861 mem = memory_resource.allocate (size);
4962 kphp::log::assertion (mem != nullptr );
5063 }
@@ -55,7 +68,7 @@ void* RuntimeAllocator::alloc0_script_memory(size_t size) noexcept {
5568 kphp::log::assertion (size != 0 );
5669 void * mem{memory_resource.allocate0 (size)};
5770 if (mem == nullptr ) [[unlikely]] {
58- request_extra_memory (size, min_extra_mem_size );
71+ request_extra_memory (size);
5972 mem = memory_resource.allocate0 (size);
6073 kphp::log::assertion (mem != nullptr );
6174 }
@@ -66,7 +79,7 @@ void* RuntimeAllocator::realloc_script_memory(void* old_mem, size_t new_size, si
6679 kphp::log::assertion (new_size > old_size);
6780 void * new_mem{memory_resource.reallocate (old_mem, new_size, old_size)};
6881 if (new_mem == nullptr ) [[unlikely]] {
69- request_extra_memory (new_size * 2 , min_extra_mem_size );
82+ request_extra_memory (new_size * 2 );
7083 new_mem = memory_resource.reallocate (old_mem, new_size, old_size);
7184 kphp::log::assertion (new_mem != nullptr );
7285 }
@@ -100,20 +113,3 @@ void* RuntimeAllocator::realloc_global_memory(void* old_mem, size_t new_size, si
100113void RuntimeAllocator::free_global_memory (void * mem, size_t /* unused*/ ) noexcept {
101114 k2::free (mem);
102115}
103-
104- void RuntimeAllocator::request_extra_memory (size_t requested_size, size_t min_extra_mem_size) noexcept {
105- // Extra mem size have to be greater than max chunk block
106- min_extra_mem_size = std::max (min_extra_mem_size, memory_resource::unsynchronized_pool_resource::MAX_CHUNK_BLOCK_SIZE);
107-
108- // If the requested size is greater than or equal to half of min_extra_mem_size, it’s more efficient to allocate a multiple of the requested size.
109- size_t extra_mem_size{std::max (min_extra_mem_size, 2 * requested_size)};
110-
111- // Take into account internal layout of `memory_resource::extra_memory_pool`
112- extra_mem_size += sizeof (memory_resource::extra_memory_pool);
113-
114- kphp::log::debug (" requested extra memory pool with size {} bytes, will be allocated {} bytes" , requested_size, extra_mem_size);
115-
116- auto * extra_mem{alloc_global_memory (extra_mem_size)};
117- kphp::log::assertion (extra_mem != nullptr );
118- memory_resource.add_extra_memory (new (extra_mem) memory_resource::extra_memory_pool{extra_mem_size});
119- }
0 commit comments