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-
2713RuntimeAllocator& RuntimeAllocator::get () noexcept {
2814 return AllocatorState::get_mutable ().allocator ;
2915}
3016
31- RuntimeAllocator::RuntimeAllocator (size_t script_mem_size, size_t oom_handling_mem_size) {
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) {
3219 kphp::log::debug (" create runtime allocator -> {:p}: script memory -> {}, oom handling size -> {}" , reinterpret_cast <void *>(this ), script_mem_size,
3320 oom_handling_mem_size);
3421 void * buffer{alloc_global_memory (script_mem_size)};
@@ -57,7 +44,7 @@ void* RuntimeAllocator::alloc_script_memory(size_t size) noexcept {
5744 kphp::log::assertion (size != 0 );
5845 void * mem{memory_resource.allocate (size)};
5946 if (mem == nullptr ) [[unlikely]] {
60- request_extra_memory (size);
47+ request_extra_memory (size, min_extra_mem_size );
6148 mem = memory_resource.allocate (size);
6249 kphp::log::assertion (mem != nullptr );
6350 }
@@ -68,7 +55,7 @@ void* RuntimeAllocator::alloc0_script_memory(size_t size) noexcept {
6855 kphp::log::assertion (size != 0 );
6956 void * mem{memory_resource.allocate0 (size)};
7057 if (mem == nullptr ) [[unlikely]] {
71- request_extra_memory (size);
58+ request_extra_memory (size, min_extra_mem_size );
7259 mem = memory_resource.allocate0 (size);
7360 kphp::log::assertion (mem != nullptr );
7461 }
@@ -79,7 +66,7 @@ void* RuntimeAllocator::realloc_script_memory(void* old_mem, size_t new_size, si
7966 kphp::log::assertion (new_size > old_size);
8067 void * new_mem{memory_resource.reallocate (old_mem, new_size, old_size)};
8168 if (new_mem == nullptr ) [[unlikely]] {
82- request_extra_memory (new_size * 2 );
69+ request_extra_memory (new_size * 2 , min_extra_mem_size );
8370 new_mem = memory_resource.reallocate (old_mem, new_size, old_size);
8471 kphp::log::assertion (new_mem != nullptr );
8572 }
@@ -113,3 +100,20 @@ void* RuntimeAllocator::realloc_global_memory(void* old_mem, size_t new_size, si
113100void RuntimeAllocator::free_global_memory (void * mem, size_t /* unused*/ ) noexcept {
114101 k2::free (mem);
115102}
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