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

expand_as_op_1
Zeng Jinle 6 years ago committed by GitHub
parent 619c797a7f
commit 8359b415e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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>;

Loading…
Cancel
Save