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 1 commit
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
30 changes: 17 additions & 13 deletions cpp/src/arrow/array-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1154,40 +1154,44 @@ TEST_F(TestBinaryBuilder, TestScalarAppend) {
}
}
}

TEST_F(TestBinaryBuilder, TestCapacityReserve) {
vector<string> strings = {"aaaaa", "bbbbbbbbbb", "ccccccccccccccc", "dddddddddddddddddddd", "eeeeeeeeee"};
vector<string> strings = {"aaaaa", "bbbbbbbbbb", "ccccccccccccccc", "dddddddddd"};
Copy link
Member

Choose a reason for hiding this comment

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

In the future, you can run make format (which uses clang-format) to fix these long lines without having to make code changes

int N = static_cast<int>(strings.size());
int reps = 10;
int reps = 15;
int64_t length = 0;
int64_t capacity = 1000;

int64_t expected_capacity = BitUtil::RoundUpToMultipleOf64(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(length, builder_->value_data_length());
ASSERT_EQ(BitUtil::RoundUpToMultipleOf64(capacity), builder_->value_data_capacity());
ASSERT_EQ(expected_capacity, builder_->value_data_capacity());

for (int j = 0; j < reps; ++j) {
for (int i = 0; i < N; ++i) {
ASSERT_OK(builder_->Append(strings[i]));
length += static_cast<int>(strings[i].size());

ASSERT_EQ(length, builder_->value_data_length());
ASSERT_EQ(BitUtil::RoundUpToMultipleOf64(capacity), builder_->value_data_capacity());
ASSERT_EQ(expected_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 = 500;
expected_capacity = BitUtil::RoundUpToMultipleOf64(length + extra_capacity);

ASSERT_OK(builder_->ReserveData(extra_capacity));

ASSERT_EQ(length, builder_->value_data_length());
ASSERT_EQ(BitUtil::RoundUpToMultipleOf64(length + extra_capacity), builder_->value_data_capacity());
ASSERT_EQ(expected_capacity, builder_->value_data_capacity());

Done();

ASSERT_EQ(reps * N, result_->length());
ASSERT_EQ(0, result_->null_count());
ASSERT_EQ(reps * 60, result_->value_data()->size());
ASSERT_EQ(BitUtil::RoundUpToMultipleOf64(length + extra_capacity), result_->value_data()->capacity());
ASSERT_EQ(reps * 40, result_->value_data()->size());
ASSERT_EQ(expected_capacity, result_->value_data()->capacity());
}

TEST_F(TestBinaryBuilder, TestZeroLength) {
Expand Down
6 changes: 3 additions & 3 deletions cpp/src/arrow/builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1225,14 +1225,14 @@ Status BinaryBuilder::Resize(int64_t capacity) {
RETURN_NOT_OK(offsets_builder_.Resize((capacity + 1) * sizeof(int32_t)));
return ArrayBuilder::Resize(capacity);
}

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

Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ class ARROW_EXPORT BinaryBuilder : public ArrayBuilder {

Status Init(int64_t elements) override;
Status Resize(int64_t capacity) override;
/// \brief Ensures there is enough allocated capacity to append the indicated
/// \brief Ensures there is enough allocated capacity to append the indicated
/// number of bytes to the value data buffer without additional allocations
Status ReserveData(int64_t elements);
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
Expand Down