未验证 提交 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) { ...@@ -57,6 +57,7 @@ Allocation *AutoGrowthBestFitAllocator::AllocateImpl(size_t size) {
block_it->is_free_ = false; block_it->is_free_ = false;
} }
} else { } else {
FreeIdleChunks();
size_t realloc_size = std::max(size, chunk_size_); size_t realloc_size = std::max(size, chunk_size_);
try { try {
...@@ -119,6 +120,20 @@ void AutoGrowthBestFitAllocator::FreeImpl(Allocation *allocation) { ...@@ -119,6 +120,20 @@ void AutoGrowthBestFitAllocator::FreeImpl(Allocation *allocation) {
delete 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 allocation
} // namespace memory } // namespace memory
} // namespace paddle } // namespace paddle
...@@ -27,7 +27,7 @@ namespace allocation { ...@@ -27,7 +27,7 @@ namespace allocation {
class AutoGrowthBestFitAllocator : public Allocator { class AutoGrowthBestFitAllocator : public Allocator {
public: public:
explicit AutoGrowthBestFitAllocator( AutoGrowthBestFitAllocator(
const std::shared_ptr<Allocator> &underlying_allocator, size_t alignment, const std::shared_ptr<Allocator> &underlying_allocator, size_t alignment,
size_t chunk_size = 0); size_t chunk_size = 0);
...@@ -39,6 +39,8 @@ class AutoGrowthBestFitAllocator : public Allocator { ...@@ -39,6 +39,8 @@ class AutoGrowthBestFitAllocator : public Allocator {
void FreeImpl(Allocation *allocation) override; void FreeImpl(Allocation *allocation) override;
private: private:
void FreeIdleChunks();
template <typename T> template <typename T>
using List = std::list<T>; using List = std::list<T>;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册