未验证 提交 8359b415 编写于 作者: Z Zeng Jinle 提交者: GitHub

add free chunks to auto growth allocator, test=develop (#19890)

上级 619c797a
......@@ -57,6 +57,7 @@ Allocation *AutoGrowthBestFitAllocator::AllocateImpl(size_t size) {
block_it->is_free_ = false;
}
} else {
FreeIdleChunks();
size_t realloc_size = std::max(size, chunk_size_);
try {
......@@ -119,6 +120,20 @@ void AutoGrowthBestFitAllocator::FreeImpl(Allocation *allocation) {
delete allocation;
}
void AutoGrowthBestFitAllocator::FreeIdleChunks() {
for (auto chunk_it = chunks_.begin(); chunk_it != chunks_.end();) {
auto &blocks = chunk_it->blocks_;
if (blocks.size() == 1 && blocks.begin()->is_free_) {
auto &block = *blocks.begin();
VLOG(2) << "Free chunk with size " << block.size_;
free_blocks_.erase(std::make_pair(block.size_, block.ptr_));
chunk_it = chunks_.erase(chunk_it);
} else {
++chunk_it;
}
}
}
} // namespace allocation
} // namespace memory
} // namespace paddle
......@@ -27,7 +27,7 @@ namespace allocation {
class AutoGrowthBestFitAllocator : public Allocator {
public:
explicit AutoGrowthBestFitAllocator(
AutoGrowthBestFitAllocator(
const std::shared_ptr<Allocator> &underlying_allocator, size_t alignment,
size_t chunk_size = 0);
......@@ -39,6 +39,8 @@ class AutoGrowthBestFitAllocator : public Allocator {
void FreeImpl(Allocation *allocation) override;
private:
void FreeIdleChunks();
template <typename T>
using List = std::list<T>;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册