Skip to content

Commit 8a77885

Browse files
authored
GH-49065: [C++] Remove unnecessary copies of shared_ptr in Type::BOOL and Type::NA at GrouperImpl (#49066)
### Rationale for this change The grouper code was creating a `shared_ptr<DataType>` for every key type, even when it wasn't needed. This resulted in unnecessary reference counting operations. For example, `BooleanKeyEncoder` and `NullKeyEncoder` don't require a `shared_ptr` in their constructors, yet we were creating one for every key of those types. ### What changes are included in this PR? Changed `GrouperImpl::Make()` to use `TypeHolder` references directly and only call `GetSharedPtr()` when needed by encoder constructors. This eliminates `shared_ptr` creation for `Type::BOOL` and `Type::NA` cases. Other encoder types (dictionary, fixed-width, binary) still require `shared_ptr` since their constructors take `shared_ptr<DataType>` parameters for ownership. ### Are these changes tested? Yes, existing tests. ### Are there any user-facing changes? No. * GitHub Issue: #49065 Authored-by: Hyukjin Kwon <gurwls223@apache.org> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent 12cdb09 commit 8a77885

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

cpp/src/arrow/compute/row/grouper.cc

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -341,43 +341,44 @@ struct GrouperImpl : public Grouper {
341341
impl->ctx_ = ctx;
342342

343343
for (size_t i = 0; i < key_types.size(); ++i) {
344-
// TODO(wesm): eliminate this probably unneeded shared_ptr copy
345-
std::shared_ptr<DataType> key = key_types[i].GetSharedPtr();
344+
const auto& key_type = key_types[i];
346345

347-
if (key->id() == Type::BOOL) {
346+
if (key_type.id() == Type::BOOL) {
348347
impl->encoders_[i] = std::make_unique<internal::BooleanKeyEncoder>();
349348
continue;
350349
}
351350

352-
if (key->id() == Type::DICTIONARY) {
353-
impl->encoders_[i] =
354-
std::make_unique<internal::DictionaryKeyEncoder>(key, ctx->memory_pool());
351+
if (key_type.id() == Type::DICTIONARY) {
352+
impl->encoders_[i] = std::make_unique<internal::DictionaryKeyEncoder>(
353+
key_type.GetSharedPtr(), ctx->memory_pool());
355354
continue;
356355
}
357356

358-
if (is_fixed_width(key->id())) {
359-
impl->encoders_[i] = std::make_unique<internal::FixedWidthKeyEncoder>(key);
357+
if (is_fixed_width(key_type.id())) {
358+
impl->encoders_[i] =
359+
std::make_unique<internal::FixedWidthKeyEncoder>(key_type.GetSharedPtr());
360360
continue;
361361
}
362362

363-
if (is_binary_like(key->id())) {
364-
impl->encoders_[i] =
365-
std::make_unique<internal::VarLengthKeyEncoder<BinaryType>>(key);
363+
if (is_binary_like(key_type.id())) {
364+
impl->encoders_[i] = std::make_unique<internal::VarLengthKeyEncoder<BinaryType>>(
365+
key_type.GetSharedPtr());
366366
continue;
367367
}
368368

369-
if (is_large_binary_like(key->id())) {
369+
if (is_large_binary_like(key_type.id())) {
370370
impl->encoders_[i] =
371-
std::make_unique<internal::VarLengthKeyEncoder<LargeBinaryType>>(key);
371+
std::make_unique<internal::VarLengthKeyEncoder<LargeBinaryType>>(
372+
key_type.GetSharedPtr());
372373
continue;
373374
}
374375

375-
if (key->id() == Type::NA) {
376+
if (key_type.id() == Type::NA) {
376377
impl->encoders_[i] = std::make_unique<internal::NullKeyEncoder>();
377378
continue;
378379
}
379380

380-
return Status::NotImplemented("Keys of type ", *key);
381+
return Status::NotImplemented("Keys of type ", *key_type);
381382
}
382383

383384
return impl;

0 commit comments

Comments
 (0)