Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-1712: [C++] Add method to BinaryBuilder to reserve space for value data #1481

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c2f8dc4
Merge pull request #1 from apache/master
xuepanchen Jan 15, 2018
232024e
Update BinaryBuilder::Resize(int64_t capacity) in builder.cc
xuepanchen Jan 15, 2018
d021c54
Merge pull request #2 from xuepanchen/xuepanchen-arrow-1712
xuepanchen Jan 15, 2018
5b73c1c
Update again BinaryBuilder::Resize(int64_t capacity) in builder.cc
xuepanchen Jan 15, 2018
5ebfb32
Add capacity() method for TypedBufferBuilder
xuepanchen Jan 16, 2018
e0434e6
Add ReserveData(int64_t) and value_data_capacity() for methods for Bi…
xuepanchen Jan 16, 2018
de318f4
Implement ReserveData(int64_t) method for BinaryBuilder
xuepanchen Jan 16, 2018
b002e0b
Remove override keyword from ReserveData(int64_t) method for BinaryBu…
xuepanchen Jan 16, 2018
8dd5eaa
Update builder.cc
xuepanchen Jan 17, 2018
9b5e805
Update ReserveData(int64_t) method signature for BinaryBuilder
xuepanchen Jan 17, 2018
5a5593e
Update again ReserveData(int64_t) method for BinaryBuilder
xuepanchen Jan 17, 2018
15e045c
Add test case for array-test.cc
xuepanchen Jan 18, 2018
bbc6527
ARROW-1945: [C++] Update test case for BinaryBuild data value space r…
xuepanchen Jan 18, 2018
18f90fb
ARROW-1945: [C++] Add data_capacity_ to track capacity of value data
xuepanchen Jan 18, 2018
0b07895
ARROW-1945: [C++] Add data_capacity_ to track capacity of value data
xuepanchen Jan 18, 2018
d3c8202
ARROW-1945: [C++] Fix a small typo
xuepanchen Jan 18, 2018
8e4c892
Merge pull request #3 from xuepanchen/xuepanchen-arrow-1712
xuepanchen Jan 19, 2018
5a5b70e
Merge pull request #4 from apache/master
xuepanchen Jan 19, 2018
bc5db7d
ARROW-1712: [C++] Remove unneeded data member in BinaryBuilder and mo…
xuepanchen Jan 20, 2018
77f8f3c
Merge pull request #5 from apache/master
xuepanchen Jan 22, 2018
d4bbd15
ARROW-1712: [C++] Modify test case for BinaryBuilder::ReserveData() a…
xuepanchen Jan 22, 2018
360e601
Merge branch 'master' of https://github.com/xuepanchen/arrow
xuepanchen Jan 22, 2018
707b67b
ARROW-1712: [C++] Fix lint errors
xuepanchen Jan 23, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions cpp/src/arrow/array-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,54 @@ TEST_F(TestBinaryBuilder, TestScalarAppend) {
}
}
}

TEST_F(TestBinaryBuilder, TestCapacityReserve) {
vector<string> strings = {"a", "bb", "cc", "ddddd", "eeeee"};
int64_t N = static_cast<int>(strings.size());
int64_t length = 0;
int64_t data_length = 0;
int64_t capacity = N;

ASSERT_OK(builder_->Reserve(capacity));
ASSERT_OK(builder_->ReserveData(capacity));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These reservations should be viewed as different, because the size of the offsets buffer and the data buffer grow at different rates. The way this unit test should read is:

  • Call ReserveData with enough space for some large-ish amount of data (say 4K bytes or so)
  • Append <= N bytes incrementally
  • Check that the capacity remains invariant at the end (i.e. the initial ReserveData made sure that no additional reallocations took place)


ASSERT_EQ(builder_->length(), length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In googletest, the 1st parameter passed to ASSERT_EQ should be the expected result, so flip the argument order here and below

ASSERT_EQ(builder_->capacity(), BitUtil::NextPower2(capacity));
ASSERT_EQ(builder_->value_data_length(), data_length);
ASSERT_EQ(builder_->value_data_capacity(), capacity);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not a power of 2?


for(const string& str : strings) {
ASSERT_OK(builder_->Append(str));
length++;
data_length += static_cast<int>(str.size());

ASSERT_EQ(builder_->length(), length);
ASSERT_EQ(builder_->capacity(), BitUtil::NextPower2(capacity));
ASSERT_EQ(builder_->value_data_length(), data_length);
if (data_length <= capacity) {
ASSERT_EQ(builder_->value_data_capacity(), capacity);
} else {
ASSERT_EQ(builder_->value_data_capacity(), data_length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me why these assertions would hold true

ASSERT_EQ(builder_->capacity(), BitUtil::NextPower2(capacity));
ASSERT_EQ(builder_->value_data_capacity(), capacity);

I would think that value_data_capacity() is always the power of 2 greater than or equal to the amount of data appended so far, i.e. ASSERT_EQ(BitUtil::NextPower2(data_length), builder_->value_data_capacity())

Can you make the strings you are appending much larger, at least 10 length each?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wesm value_data_capacity() is actually always a multiple of 64 greater than or equal to the amount of data appended so far because the underlying buffer size is set to ensure that the capacity of the buffer is a multiple of 64 bytes as defined in Layout.md, i.e.

ASSERT_EQ(BitUtil::RoundUpToMultipleOf64(data_length), builder_->value_data_capacity())

So if you call ReserveData(capacity) at the very beginning, then we have

ASSERT_EQ(BitUtil::RoundUpToMultipleOf64(capacity), builder_->value_data_capacity())

}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add another call to ReserveData here, like builder_->ReserveData(500) to show that the input argument is an incremental amount rather than an absolute amount?


int extra_capacity = 20;

ASSERT_OK(builder_->Reserve(extra_capacity));
ASSERT_OK(builder_->ReserveData(extra_capacity));

ASSERT_EQ(builder_->length(), length);
ASSERT_EQ(builder_->capacity(), BitUtil::NextPower2(length + extra_capacity));
ASSERT_EQ(builder_->value_data_length(), data_length);
ASSERT_EQ(builder_->value_data_capacity(), data_length + extra_capacity);

Done();

ASSERT_EQ(result_->length(), N);
ASSERT_EQ(result_->null_count(), 0);
ASSERT_EQ(result_->value_data()->size(), data_length);
ASSERT_EQ(result_->value_data()->capacity(), BitUtil::RoundUpToMultipleOf64(data_length + extra_capacity));
}

TEST_F(TestBinaryBuilder, TestZeroLength) {
// All buffers are null
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ class ARROW_EXPORT TypedBufferBuilder : public BufferBuilder {

const T* data() const { return reinterpret_cast<const T*>(data_); }
int64_t length() const { return size_ / sizeof(T); }
int64_t capacity() const { return capacity_ / sizeof(T); }
};

/// \brief Allocate a fixed size mutable buffer from a memory pool
Expand Down
19 changes: 17 additions & 2 deletions cpp/src/arrow/builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ ArrayBuilder* ListBuilder::value_builder() const {
// String and binary

BinaryBuilder::BinaryBuilder(const std::shared_ptr<DataType>& type, MemoryPool* pool)
: ArrayBuilder(type, pool), offsets_builder_(pool), value_data_builder_(pool) {}
: ArrayBuilder(type, pool), offsets_builder_(pool), value_data_builder_(pool), data_capacity_(0) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this extra member; we can just use value_data_capacity() wherever data_capacity_ is currently being used


BinaryBuilder::BinaryBuilder(MemoryPool* pool) : BinaryBuilder(binary(), pool) {}

Expand All @@ -1222,9 +1222,21 @@ Status BinaryBuilder::Init(int64_t elements) {
Status BinaryBuilder::Resize(int64_t capacity) {
DCHECK_LT(capacity, std::numeric_limits<int32_t>::max());
// one more then requested for offsets
RETURN_NOT_OK(offsets_builder_.Resize((capacity + 1) * sizeof(int64_t)));
RETURN_NOT_OK(offsets_builder_.Resize((capacity + 1) * sizeof(int32_t)));
return ArrayBuilder::Resize(capacity);
}

Status BinaryBuilder::ReserveData(int64_t capacity) {
if (value_data_length() + capacity > data_capacity_) {
if (value_data_length() + capacity > std::numeric_limits<int32_t>::max()) {
return Status::Invalid("Cannot reserve capacity larger than 2^31 - 1 in length for binary data");
}

RETURN_NOT_OK(value_data_builder_.Resize(value_data_length() + capacity));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you rebase on master, you can use value_data_builder_.Reserve(capacity) here

data_capacity_ = value_data_length() + capacity;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed

}
return Status::OK();
}

Status BinaryBuilder::AppendNextOffset() {
const int64_t num_bytes = value_data_builder_.length();
Expand All @@ -1241,6 +1253,9 @@ Status BinaryBuilder::Append(const uint8_t* value, int32_t length) {
RETURN_NOT_OK(Reserve(1));
RETURN_NOT_OK(AppendNextOffset());
RETURN_NOT_OK(value_data_builder_.Append(value, length));
if (data_capacity_ < value_data_length()) {
data_capacity_ = value_data_length();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this

UnsafeAppendToBitmap(true);
return Status::OK();
}
Expand Down
4 changes: 4 additions & 0 deletions cpp/src/arrow/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -682,10 +682,13 @@ class ARROW_EXPORT BinaryBuilder : public ArrayBuilder {

Status Init(int64_t elements) override;
Status Resize(int64_t capacity) override;
Status ReserveData(int64_t capacity);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a doxygen comment, since this is a new API

I note that the Reserve-type methods in this header have different semantics from their STL counterparts. They are reserving additional space rather than absolute space (e.g. std::vector::reserve takes an absolute length as argument)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also call the argument to ReserveData size (or elements) instead of capacity (to avoid confusion about whether we are passing an incremental value vs. absolute)

Status FinishInternal(std::shared_ptr<ArrayData>* out) override;

/// \return size of values buffer so far
int64_t value_data_length() const { return value_data_builder_.length(); }
/// \return capacity of values buffer
int64_t value_data_capacity() const { return data_capacity_; }

/// Temporary access to a value.
///
Expand All @@ -696,6 +699,7 @@ class ARROW_EXPORT BinaryBuilder : public ArrayBuilder {
TypedBufferBuilder<int32_t> offsets_builder_;
TypedBufferBuilder<uint8_t> value_data_builder_;

int64_t data_capacity_;
static constexpr int64_t kMaximumCapacity = std::numeric_limits<int32_t>::max() - 1;

Status AppendNextOffset();
Expand Down