Skip to content

Commit abca6eb

Browse files
committed
Add a MemoryPool abstract interface, change builder instances to request memory from pool via Buffer subclass
1 parent a385622 commit abca6eb

20 files changed

Lines changed: 285 additions & 54 deletions

cpp/src/arrow/array-test.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "arrow/types/integer.h"
2929
#include "arrow/types/primitive.h"
3030
#include "arrow/util/buffer.h"
31+
#include "arrow/util/memory-pool.h"
3132

3233
using std::string;
3334
using std::vector;
@@ -41,8 +42,10 @@ static TypePtr int32_nn = TypePtr(new Int32Type(false));
4142
class TestArray : public ::testing::Test {
4243
public:
4344
void SetUp() {
44-
auto data = std::make_shared<OwnedMutableBuffer>();
45-
auto nulls = std::make_shared<OwnedMutableBuffer>();
45+
pool_ = GetDefaultMemoryPool();
46+
47+
auto data = std::make_shared<PoolBuffer>(pool_);
48+
auto nulls = std::make_shared<PoolBuffer>(pool_);
4649

4750
ASSERT_OK(data->Resize(400));
4851
ASSERT_OK(nulls->Resize(128));
@@ -51,6 +54,7 @@ class TestArray : public ::testing::Test {
5154
}
5255

5356
protected:
57+
MemoryPool* pool_;
5458
std::unique_ptr<Int32Array> arr_;
5559
};
5660

cpp/src/arrow/builder.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Status ArrayBuilder::Init(int64_t capacity) {
3030

3131
if (nullable_) {
3232
int64_t to_alloc = util::ceil_byte(capacity) / 8;
33-
nulls_ = std::make_shared<OwnedMutableBuffer>();
33+
nulls_ = std::make_shared<PoolBuffer>(pool_);
3434
RETURN_NOT_OK(nulls_->Resize(to_alloc));
3535
null_bits_ = nulls_->mutable_data();
3636
memset(null_bits_, 0, to_alloc);

cpp/src/arrow/builder.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@
3030
namespace arrow {
3131

3232
class Array;
33+
class MemoryPool;
3334

3435
static constexpr int64_t MIN_BUILDER_CAPACITY = 1 << 8;
3536

3637
// Base class for all data array builders
3738
class ArrayBuilder {
3839
public:
39-
explicit ArrayBuilder(const TypePtr& type)
40-
: type_(type),
41-
nullable_(type_->nullable),
42-
nulls_(nullptr), null_bits_(nullptr),
43-
length_(0),
44-
capacity_(0) {}
40+
explicit ArrayBuilder(MemoryPool* pool, const TypePtr& type) :
41+
pool_(pool),
42+
type_(type),
43+
nullable_(type_->nullable),
44+
nulls_(nullptr), null_bits_(nullptr),
45+
length_(0),
46+
capacity_(0) {}
4547

4648
virtual ~ArrayBuilder() {}
4749

@@ -71,18 +73,20 @@ class ArrayBuilder {
7173
// this function responsibly.
7274
Status Advance(int64_t elements);
7375

74-
const std::shared_ptr<OwnedMutableBuffer>& nulls() const { return nulls_;}
76+
const std::shared_ptr<PoolBuffer>& nulls() const { return nulls_;}
7577

7678
// Creates new array object to hold the contents of the builder and transfers
7779
// ownership of the data
7880
virtual Status ToArray(Array** out) = 0;
7981

8082
protected:
83+
MemoryPool* pool_;
84+
8185
TypePtr type_;
8286
bool nullable_;
8387

8488
// If the type is not nullable, then null_ is nullptr after initialization
85-
std::shared_ptr<OwnedMutableBuffer> nulls_;
89+
std::shared_ptr<PoolBuffer> nulls_;
8690
uint8_t* null_bits_;
8791

8892
// Array length, so far. Also, the index of the next element to be added

cpp/src/arrow/types/construct.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ class ArrayBuilder;
3232
// Initially looked at doing this with vtables, but shared pointers makes it
3333
// difficult
3434

35-
#define BUILDER_CASE(ENUM, BuilderType) \
36-
case TypeEnum::ENUM: \
37-
*out = static_cast<ArrayBuilder*>(new BuilderType(type)); \
35+
#define BUILDER_CASE(ENUM, BuilderType) \
36+
case TypeEnum::ENUM: \
37+
*out = static_cast<ArrayBuilder*>(new BuilderType(pool, type)); \
3838
return Status::OK();
3939

40-
Status make_builder(const TypePtr& type, ArrayBuilder** out) {
40+
Status make_builder(MemoryPool* pool, const TypePtr& type,
41+
ArrayBuilder** out) {
4142
switch (type->type) {
4243
BUILDER_CASE(UINT8, UInt8Builder);
4344
BUILDER_CASE(INT8, Int8Builder);
@@ -59,10 +60,10 @@ Status make_builder(const TypePtr& type, ArrayBuilder** out) {
5960
{
6061
ListType* list_type = static_cast<ListType*>(type.get());
6162
ArrayBuilder* value_builder;
62-
RETURN_NOT_OK(make_builder(list_type->value_type, &value_builder));
63+
RETURN_NOT_OK(make_builder(pool, list_type->value_type, &value_builder));
6364

6465
// The ListBuilder takes ownership of the value_builder
65-
ListBuilder* builder = new ListBuilder(type, value_builder);
66+
ListBuilder* builder = new ListBuilder(pool, type, value_builder);
6667
*out = static_cast<ArrayBuilder*>(builder);
6768
return Status::OK();
6869
}

cpp/src/arrow/types/construct.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
namespace arrow {
2424

2525
class ArrayBuilder;
26+
class MemoryPool;
2627
class Status;
2728

28-
Status make_builder(const TypePtr& type, ArrayBuilder** out);
29+
Status make_builder(MemoryPool* pool, const TypePtr& type,
30+
ArrayBuilder** out);
2931

3032
} // namespace arrow
3133

cpp/src/arrow/types/list-test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class TestListBuilder : public TestBuilder {
7676
type_ = TypePtr(new ListType(value_type_));
7777

7878
ArrayBuilder* tmp;
79-
ASSERT_OK(make_builder(type_, &tmp));
79+
ASSERT_OK(make_builder(pool_, type_, &tmp));
8080
builder_.reset(static_cast<ListBuilder*>(tmp));
8181
}
8282

cpp/src/arrow/types/list.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
namespace arrow {
3636

37+
class MemoryPool;
38+
3739
struct ListType : public DataType {
3840
// List can contain any other logical value type
3941
TypePtr value_type;
@@ -100,8 +102,9 @@ class ListArray : public Array {
100102
// have been appended to the child array)
101103
class ListBuilder : public Int32Builder {
102104
public:
103-
ListBuilder(const TypePtr& type, ArrayBuilder* value_builder)
104-
: Int32Builder(type) {
105+
ListBuilder(MemoryPool* pool, const TypePtr& type,
106+
ArrayBuilder* value_builder)
107+
: Int32Builder(pool, type) {
105108
value_builder_.reset(value_builder);
106109
}
107110

cpp/src/arrow/types/primitive-test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ class TestPrimitiveBuilder : public TestBuilder {
104104
type_nn_ = Attrs::type(false);
105105

106106
ArrayBuilder* tmp;
107-
ASSERT_OK(make_builder(type_, &tmp));
107+
ASSERT_OK(make_builder(pool_, type_, &tmp));
108108
builder_.reset(static_cast<BuilderType*>(tmp));
109109

110-
ASSERT_OK(make_builder(type_nn_, &tmp));
110+
ASSERT_OK(make_builder(pool_, type_nn_, &tmp));
111111
builder_nn_.reset(static_cast<BuilderType*>(tmp));
112112
}
113113

cpp/src/arrow/types/primitive.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
namespace arrow {
3333

34+
class MemoryPool;
35+
3436
template <typename Derived>
3537
struct PrimitiveType : public DataType {
3638
explicit PrimitiveType(bool nullable = true)
@@ -113,8 +115,9 @@ class PrimitiveBuilder : public ArrayBuilder {
113115
public:
114116
typedef typename Type::c_type T;
115117

116-
explicit PrimitiveBuilder(const TypePtr& type)
117-
: ArrayBuilder(type), values_(nullptr) {
118+
explicit PrimitiveBuilder(MemoryPool* pool, const TypePtr& type) :
119+
ArrayBuilder(pool, type),
120+
values_(nullptr) {
118121
elsize_ = sizeof(T);
119122
}
120123

@@ -139,7 +142,7 @@ class PrimitiveBuilder : public ArrayBuilder {
139142
Status Init(int64_t capacity) {
140143
RETURN_NOT_OK(ArrayBuilder::Init(capacity));
141144

142-
values_ = std::make_shared<OwnedMutableBuffer>();
145+
values_ = std::make_shared<PoolBuffer>(pool_);
143146
return values_->Resize(capacity * elsize_);
144147
}
145148

@@ -231,7 +234,7 @@ class PrimitiveBuilder : public ArrayBuilder {
231234
}
232235

233236
protected:
234-
std::shared_ptr<OwnedMutableBuffer> values_;
237+
std::shared_ptr<PoolBuffer> values_;
235238
int64_t elsize_;
236239
};
237240

cpp/src/arrow/types/string-test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class TestStringBuilder : public TestBuilder {
175175
type_ = TypePtr(new StringType());
176176

177177
ArrayBuilder* tmp;
178-
ASSERT_OK(make_builder(type_, &tmp));
178+
ASSERT_OK(make_builder(pool_, type_, &tmp));
179179
builder_.reset(static_cast<StringBuilder*>(tmp));
180180
}
181181

0 commit comments

Comments
 (0)