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

[C++] ConcatenateImpl Constructor re-uses moved argument "const ArrayDataVector & in" #36446

Closed
sjperkins opened this issue Jul 4, 2023 · 2 comments · Fixed by #36463
Closed
Assignees
Milestone

Comments

@sjperkins
Copy link
Contributor

Describe the bug, including details regarding any error messages, version, and platform.

In cpp/src/arrow/array/concatenate.cc , the const ArrayDataVector & in argument is referenced after being moved into the in_ within the member initializer list:

class ConcatenateImpl {
public:
ConcatenateImpl(const ArrayDataVector& in, MemoryPool* pool)
: in_(std::move(in)), pool_(pool), out_(std::make_shared<ArrayData>()) {
out_->type = in[0]->type;
for (size_t i = 0; i < in_.size(); ++i) {
out_->length = SafeSignedAdd(out_->length, in[i]->length);
if (out_->null_count == kUnknownNullCount ||
in[i]->null_count == kUnknownNullCount) {
out_->null_count = kUnknownNullCount;
continue;
}
out_->null_count = SafeSignedAdd(out_->null_count.load(), in[i]->null_count.load());
}
out_->buffers.resize(in[0]->buffers.size());
out_->child_data.resize(in[0]->child_data.size());
for (auto& data : out_->child_data) {
data = std::make_shared<ArrayData>();
}
}

where using ArrayDataVector = std::vector<std::shared_ptr<ArrayData>>; in type_fwd.h.

I was surprised the code works and that this seems to result in a copy of in to in_ . But I suspect that either

  1. The signature should be ConcatenateImpl(ArrayDataVector in, MemoryPool* pool) and in should not be re-used after the move, or:
  2. The member initializer list should be : in_(in), pool_(pool), out_(std::make_shared<ArrayData>())

Component(s)

C++

@sjperkins sjperkins changed the title ConcatenateImpl Constructor re-uses moved argument "const ArrayDataVector & in" [C++] ConcatenateImpl Constructor re-uses moved argument "const ArrayDataVector & in" Jul 4, 2023
@mapleFU
Copy link
Member

mapleFU commented Jul 4, 2023

Using std::move on a constant lvalue reference might not causing a bug, because it might just copy the vector. But it's worthing to fix it.

@sjperkins
Copy link
Contributor Author

Using std::move on a constant lvalue reference might not causing a bug, because it might just copy the vector. But it's worthing to fix it.

Yes you're right, I'd forgotten that std::move instructs the compiler to try a move, but it's not guaranteed. This SO explains it well https://stackoverflow.com/a/28595207.

pitrou pushed a commit that referenced this issue Jul 6, 2023
### Rationale for this change

Fixing style in `ConcatenateImpl::ConcatenateImpl`

### What changes are included in this PR?

1. Remove `std::move` on a const-ref
2. Use a foreach loop

### Are these changes tested?

Yes, by existing tests.

### Are there any user-facing changes?

No.

* Closes: #36446

Authored-by: mwish <maplewish117@gmail.com>
Signed-off-by: Antoine Pitrou <antoine@python.org>
@pitrou pitrou added this to the 13.0.0 milestone Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants