Skip to content

Commit cc76736

Browse files
authored
ARROW-17824: [C++][Gandiva] Implement preallocation for variable length output buffer (#14230)
When the output type of an expression is of variable length, e.g. string, Gandiva would realloc the output buffer to make space for new outputs for each row. When num of rows is high some memory allocators perform poorly. We can use the std::vector like approach to amortize the allcation cost. First allocate some initial space depending on the input size. Each time we run out of space, double the buffer size. In the end shrink it to fit the actual size. Arrow string builder also uses this approach. Authored-by: Jin Shang <[email protected]> Signed-off-by: Sutou Kouhei <[email protected]>
1 parent b76ddf1 commit cc76736

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

‎cpp/src/gandiva/gdv_function_stubs.cc‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,21 @@ int32_t gdv_fn_populate_varlen_vector(int64_t context_ptr, int8_t* data_ptr,
129129
auto buffer = reinterpret_cast<arrow::ResizableBuffer*>(data_ptr);
130130
int32_t offset = static_cast<int32_t>(buffer->size());
131131

132-
// This also sets the size in the buffer.
133-
auto status = buffer->Resize(offset + entry_len, false /*shrink*/);
132+
auto new_size = offset + entry_len;
133+
// preallocation, double the size to amortize costs
134+
if (buffer->capacity() < new_size) {
135+
auto status =
136+
buffer->Reserve(std::max(buffer->capacity() * 2, static_cast<int64_t>(new_size)));
137+
if (!status.ok()) {
138+
auto context = reinterpret_cast<gandiva::ExecutionContext*>(context_ptr);
139+
140+
context->set_error_msg(status.message().c_str());
141+
return -1;
142+
}
143+
}
144+
145+
// This only sets the size in the buffer due to preallocation.
146+
auto status = buffer->Resize(new_size, false /*shrink*/);
134147
if (!status.ok()) {
135148
gandiva::ExecutionContext* context =
136149
reinterpret_cast<gandiva::ExecutionContext*>(context_ptr);

‎cpp/src/gandiva/tests/micro_benchmarks.cc‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <stdlib.h>
1919
#include "arrow/memory_pool.h"
2020
#include "arrow/status.h"
21+
#include "arrow/testing/gtest_util.h"
22+
#include "arrow/type_fwd.h"
2123
#include "benchmark/benchmark.h"
2224
#include "gandiva/decimal_type_util.h"
2325
#include "gandiva/projector.h"
@@ -249,6 +251,30 @@ static void TimedTestAllocs(benchmark::State& state) {
249251
schema, evaluator, data_generator, pool_, 1 * MILLION, 16 * THOUSAND, state);
250252
ASSERT_TRUE(status.ok());
251253
}
254+
255+
static void TimedTestOutputStringAllocs(benchmark::State& state) {
256+
// schema for input fields
257+
auto field_a = field("abcdefghijklmnopqrstuvwxyz", arrow::utf8());
258+
auto schema = arrow::schema({field_a});
259+
auto pool_ = arrow::default_memory_pool();
260+
// output field
261+
auto field_res = field("res", utf8());
262+
263+
// Build expression
264+
auto node_a = TreeExprBuilder::MakeField(field_a);
265+
auto upper = TreeExprBuilder::MakeFunction("upper", {node_a}, utf8());
266+
auto length = TreeExprBuilder::MakeFunction("octet_length", {upper}, int32());
267+
auto expr = TreeExprBuilder::MakeExpression(upper, field_res);
268+
269+
std::shared_ptr<Projector> projector;
270+
ASSERT_OK(Projector::Make(schema, {expr}, TestConfiguration(), &projector));
271+
272+
FastUtf8DataGenerator data_generator(64);
273+
ProjectEvaluator evaluator(projector);
274+
275+
ASSERT_OK((TimedEvaluate<arrow::StringType, std::string>(
276+
schema, evaluator, data_generator, pool_, 1 * MILLION, 16 * THOUSAND, state)));
277+
}
252278
// following two tests are for benchmark optimization of
253279
// in expr. will be used in follow-up PRs to optimize in expr.
254280

@@ -442,6 +468,7 @@ BENCHMARK(TimedTestFilterLike)->MinTime(1.0)->Unit(benchmark::kMicrosecond);
442468
BENCHMARK(TimedTestCastFloatFromString)->MinTime(1.0)->Unit(benchmark::kMicrosecond);
443469
BENCHMARK(TimedTestCastIntFromString)->MinTime(1.0)->Unit(benchmark::kMicrosecond);
444470
BENCHMARK(TimedTestAllocs)->MinTime(1.0)->Unit(benchmark::kMicrosecond);
471+
BENCHMARK(TimedTestOutputStringAllocs)->MinTime(1.0)->Unit(benchmark::kMicrosecond);
445472
BENCHMARK(TimedTestMultiOr)->MinTime(1.0)->Unit(benchmark::kMicrosecond);
446473
BENCHMARK(TimedTestInExpr)->MinTime(1.0)->Unit(benchmark::kMicrosecond);
447474
BENCHMARK(DecimalAdd2Fast)->MinTime(1.0)->Unit(benchmark::kMicrosecond);

0 commit comments

Comments
 (0)