Skip to content

Commit fa80570

Browse files
authored
fix(orc): Optimize OrcMemoryPool (alibaba#90)
1 parent 11973b2 commit fa80570

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

src/paimon/format/orc/orc_memory_pool.h

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,35 @@ namespace paimon::orc {
2626

2727
class OrcMemoryPool : public ::orc::MemoryPool {
2828
public:
29+
using SizeType = uint64_t;
2930
explicit OrcMemoryPool(const std::shared_ptr<paimon::MemoryPool>& pool) : pool_(pool) {}
30-
char* malloc(uint64_t size) override {
31-
char* ret = reinterpret_cast<char*>(pool_->Malloc(size));
32-
alloc_map_.Insert(reinterpret_cast<size_t>(ret), size);
33-
return ret;
31+
char* malloc(SizeType size) override {
32+
if (size == 0) {
33+
return ZERO_SIZE_AREA;
34+
}
35+
if (size > std::numeric_limits<SizeType>::max() - HEADER_SIZE) {
36+
return nullptr;
37+
}
38+
if (void* ret = pool_->Malloc(size + HEADER_SIZE)) {
39+
*reinterpret_cast<SizeType*>(ret) = size;
40+
return reinterpret_cast<char*>(ret) + HEADER_SIZE;
41+
}
42+
return nullptr;
3443
}
3544
void free(char* p) override {
36-
std::optional<size_t> size = alloc_map_.Find(reinterpret_cast<size_t>(p));
37-
if (size) {
38-
pool_->Free(p, size.value());
39-
alloc_map_.Erase(reinterpret_cast<size_t>(p));
40-
} else {
41-
assert(false);
42-
pool_->Free(p, /*size=*/0);
45+
if (p == nullptr || p == ZERO_SIZE_AREA) {
46+
return;
4347
}
48+
char* raw = p - HEADER_SIZE;
49+
SizeType size = *reinterpret_cast<SizeType*>(raw);
50+
pool_->Free(raw, size + HEADER_SIZE);
4451
}
4552

4653
private:
47-
ConcurrentHashMap<size_t, uint64_t> alloc_map_;
54+
static constexpr size_t ALIGNMENT = 64;
55+
static constexpr size_t HEADER_SIZE = (sizeof(SizeType) + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
56+
alignas(ALIGNMENT) inline static char ZERO_SIZE_AREA[1];
57+
4858
std::shared_ptr<paimon::MemoryPool> pool_;
4959
};
5060

0 commit comments

Comments
 (0)