lean4-htt/src/util/memory_pool.cpp
Leonardo de Moura cad379333d feat(util/memory_pool): put limit on the size of memory_pool free_lists
See #1405

Memory consumption is still high, but I didn't manage to cross the 2Gb
limit anymore with this commit even after hundreds of modifications.

@gebner I'm not seeing a big difference betwee Lean without memory_pool,
with bounded memory_pool and unbounded memory_pool. We may even consider
removing it in the future after a more careful benchmarking.

In the benchmark (https://gist.github.com/leodemoura/b27fb4203a13a67274b388a602149303),
I'm getting the following numbers:

- No memory_pool: runtimes between 3.532s - 3.556s

- With memory_pool bounded by 8192: runtimes between 3.32s - 3.44s

- With memory_pool (with no limit): runtimes between 3.32s - 3.44s

On the other hand, the small object allocator makes a big difference.
I used your list_rev.lean example.

- with:    2.62s
- without: 3.75s
2017-02-28 15:16:43 -08:00

57 lines
1.5 KiB
C++

/*
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include <vector>
#include "util/interrupt.h"
#include "util/thread.h"
#include "util/memory_pool.h"
namespace lean {
memory_pool::~memory_pool() {
while (m_free_list != nullptr) {
void * r = m_free_list;
m_free_list = *(reinterpret_cast<void **>(r));
free(r);
lean_report_memory_deallocated(m_size);
}
}
void * memory_pool::allocate() {
inc_heartbeat();
if (m_free_list != nullptr) {
void * r = m_free_list;
m_free_list = *(reinterpret_cast<void **>(r));
m_free_list_size--;
return r;
} else {
return malloc(m_size);
}
}
typedef std::vector<memory_pool*> memory_pools;
LEAN_THREAD_PTR(memory_pools, g_thread_pools);
static void thread_finalize_memory_pool(void * p) {
std::vector<memory_pool*> * thread_pools = reinterpret_cast<std::vector<memory_pool*>*>(p);
unsigned i = thread_pools->size();
while (i > 0) {
--i;
delete (*thread_pools)[i];
}
delete thread_pools;
g_thread_pools = nullptr;
}
memory_pool * allocate_thread_memory_pool(unsigned sz) {
if (!g_thread_pools) {
g_thread_pools = new std::vector<memory_pool*>();
register_post_thread_finalizer(thread_finalize_memory_pool, g_thread_pools);
}
memory_pool * r = new memory_pool(sz);
g_thread_pools->push_back(r);
return r;
}
}