arrow icon indicating copy to clipboard operation
arrow copied to clipboard

[C++] Pass shared_ptr<DataType> by value to parametric type constructors

Open felipecrv opened this issue 2 years ago • 2 comments

Describe the enhancement requested

Types like ListType and factory functions like list take a const std::shared_ptr<DataType>& instead of a std::shared_ptr<DataType> that could be moved into the newly constructed ListType without the need of bumping a refcount and still support cases where caller can only give it a reference.

Currently:

std::shared_ptr<DataType> list(const std::shared_ptr<DataType>& value_type) {
  return std::make_shared<ListType>(value_type);
}

After this issue is fixed:

std::shared_ptr<DataType> list(std::shared_ptr<DataType> value_type) {
  return std::make_shared<ListType>(std::move(value_type));
}

When working on this, we should take the time to go through every callsite and decide if a std::move() is possible without breaking correctness — things could break if the parameter is used after it's moved into the new instance of a parametric type.

Component(s)

C++

felipecrv avatar Sep 26 '23 20:09 felipecrv

@felipecrv should we close this issue? I see that it was resolved by this PR

Andreas-Hadjiantoni avatar Jun 29 '24 10:06 Andreas-Hadjiantoni

@felipecrv should we close this issue? I see that it was resolved by this PR

Not really because there are many other functions and constructors that should go through to this. list and ListViewType were just examples.

The steps for working on this:

  1. Identify a function that could go from taking shared_ptr& to shared_ptr (usually constructors)
  2. find the callers and do the same transformation on them
  3. add std::move at all callsites but make sure (by code review) that the value that is std::move'd from is not used after the call

felipecrv avatar Jun 30 '24 23:06 felipecrv

@felipecrv If nobody is working on this issue and it's still needed, can I take over?

chrisxu333 avatar Jan 23 '25 08:01 chrisxu333

Of course.

felipecrv avatar Jan 30 '25 17:01 felipecrv

#45466 I've make an initial commit that does a similar change to the SliceBuffer function. @felipecrv Could you help check if I understand the intent of this issue correctly? If so I'll go ahead and make other changes accordingly.

chrisxu333 avatar Feb 07 '25 15:02 chrisxu333

@felipecrv Could you please assign this issue to me if it is still open?

Shivani622 avatar Feb 14 '25 03:02 Shivani622

@Shivani622 I'm still working on it.

chrisxu333 avatar Feb 14 '25 03:02 chrisxu333

Issue resolved by pull request 45466 https://github.com/apache/arrow/pull/45466

pitrou avatar Feb 24 '25 10:02 pitrou

I will re-open this issue because there many occurrences of const shared_ptr<T> & in constructors that should change to shared_ptr<T>. I don't expect a single PR to fix all of them.

felipecrv avatar Mar 11 '25 22:03 felipecrv

@felipecrv I would love to work on it as I am new to C++.

singh1203 avatar Mar 26 '25 16:03 singh1203

@singh1203 feel free to check and add some

mapleFU avatar Apr 04 '25 04:04 mapleFU

@felipecrv Should this be applied to other movable classes (e.g. std::string) or to std::shared_ptr only?

For other classes I think it'd be safe to only change it at the lowest level (constructors/factories). Sometimes internal references are kept as local variables, thus changing all of them requires extra care.

On the other hand, if some functions do not own the objects passed in, shouldn't we just use const/non-const reference to the type itself and dereference the std:shared_ptr when calling it rather than const reference to std::shared_ptr?

kapoisu avatar May 01 '25 19:05 kapoisu

Issue resolved by pull request 46202 https://github.com/apache/arrow/pull/46202

pitrou avatar Jun 19 '25 12:06 pitrou