Skip to content

Commit 195a136

Browse files
committed
implementing feedback
1 parent 0645e50 commit 195a136

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

cpp/src/arrow/memory_pool-test.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,21 @@ TEST(LoggingMemoryPool, Logging) {
9595
TEST(ProxyMemoryPool, Logging) {
9696
MemoryPool* pool = default_memory_pool();
9797

98-
ProxyMemoryPool pp = ProxyMemoryPool(pool);
98+
ProxyMemoryPool* pp = new ProxyMemoryPool(pool);
9999

100100
uint8_t* data;
101101
ASSERT_OK(pool->Allocate(100, &data));
102102

103103
uint8_t* data2;
104-
ASSERT_OK(pp.Allocate(300, &data2));
104+
ASSERT_OK(pp->Allocate(300, &data2));
105105

106106
ASSERT_EQ(400, pool->bytes_allocated());
107-
ASSERT_EQ(300, pp.bytes_allocated());
107+
ASSERT_EQ(300, pp->bytes_allocated());
108108

109109
pool->Free(data, 100);
110-
pp.Free(data2, 300);
110+
pp->Free(data2, 300);
111111

112112
ASSERT_EQ(0, pool->bytes_allocated());
113-
ASSERT_EQ(0, pp.bytes_allocated());
113+
ASSERT_EQ(0, pp->bytes_allocated());
114114
}
115115
} // namespace arrow

cpp/src/arrow/memory_pool.cc

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,29 +205,36 @@ int64_t LoggingMemoryPool::max_memory() const {
205205
ProxyMemoryPool::ProxyMemoryPool(MemoryPool* pool) : pool_(pool) {}
206206

207207
Status ProxyMemoryPool::Allocate(int64_t size, uint8_t** out) {
208-
Status s = pool_->Allocate(size, out);
208+
RETURN_NOT_OK(pool_->Allocate(size, out));
209209
bytes_allocated_ += size;
210-
return s;
210+
{
211+
std::lock_guard<std::mutex> guard(lock_);
212+
if (bytes_allocated_ > max_memory_) {
213+
max_memory_ = bytes_allocated_.load();
214+
}
215+
}
216+
return Status::OK();
211217
}
212218

213219
Status ProxyMemoryPool::Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) {
214-
Status s = pool_->Reallocate(old_size, new_size, ptr);
220+
RETURN_NOT_OK(pool_->Reallocate(old_size, new_size, ptr));
215221
bytes_allocated_ += new_size - old_size;
216-
return s;
222+
{
223+
std::lock_guard<std::mutex> guard(lock_);
224+
if (bytes_allocated_ > max_memory_) {
225+
max_memory_ = bytes_allocated_.load();
226+
}
227+
}
228+
return Status::OK();
217229
}
218230

219231
void ProxyMemoryPool::Free(uint8_t* buffer, int64_t size) {
220232
pool_->Free(buffer, size);
221233
bytes_allocated_ -= size;
222234
}
223235

224-
int64_t ProxyMemoryPool::bytes_allocated() const {
225-
int64_t nb_bytes = bytes_allocated_;
226-
return nb_bytes;
227-
}
236+
int64_t ProxyMemoryPool::bytes_allocated() const { return bytes_allocated_.load(); }
237+
238+
int64_t ProxyMemoryPool::max_memory() const { return max_memory_.load(); }
228239

229-
int64_t ProxyMemoryPool::max_memory() const {
230-
int64_t mem = pool_->max_memory();
231-
return mem;
232-
}
233240
} // namespace arrow

cpp/src/arrow/memory_pool.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define ARROW_MEMORY_POOL_H
2020

2121
#include <atomic>
22+
#include <mutex>
2223
#include <cstdint>
2324

2425
#include "arrow/util/visibility.h"
@@ -86,10 +87,13 @@ class ARROW_EXPORT LoggingMemoryPool : public MemoryPool {
8687
MemoryPool* pool_;
8788
};
8889

90+
/// Derived class for memory allocation.
91+
///
92+
/// Tracks the number of bytes and maximum mmeory allocated through its direct
93+
/// calls. Actual allocation is delegated to MemoryPool class.
8994
class ARROW_EXPORT ProxyMemoryPool : public MemoryPool {
9095
public:
9196
explicit ProxyMemoryPool(MemoryPool* pool);
92-
~ProxyMemoryPool() override = default;
9397

9498
Status Allocate(int64_t size, uint8_t** out) override;
9599
Status Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) override;
@@ -101,8 +105,10 @@ class ARROW_EXPORT ProxyMemoryPool : public MemoryPool {
101105
int64_t max_memory() const override;
102106

103107
private:
108+
mutable std::mutex lock_;
104109
MemoryPool* pool_;
105-
int64_t bytes_allocated_ = 0;
110+
std::atomic<int64_t> bytes_allocated_{0};
111+
std::atomic<int64_t> max_memory_{0};
106112
};
107113

108114
ARROW_EXPORT MemoryPool* default_memory_pool();

python/pyarrow/includes/libarrow.pxd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
194194

195195
cdef cppclass CProxyMemoryPool" arrow::ProxyMemoryPool"(CMemoryPool):
196196
CProxyMemoryPool(CMemoryPool*)
197-
int64_t bytes_allocated()
198197

199198
cdef cppclass CBuffer" arrow::Buffer":
200199
CBuffer(const uint8_t* data, int64_t size)

python/pyarrow/memory.pxi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ def set_memory_pool(MemoryPool pool):
6767
cdef MemoryPool _default_memory_pool = default_memory_pool()
6868
cdef LoggingMemoryPool _logging_memory_pool = (
6969
LoggingMemoryPool(_default_memory_pool))
70-
cdef ProxyMemoryPool _proxy_memory_pool = (
71-
ProxyMemoryPool(_default_memory_pool))
7270

7371

7472
def log_memory_allocations(enable=True):

0 commit comments

Comments
 (0)