Repository navigation
ARROW-3133: [C++] Remove allocations from InvertKernel #3287
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
Changes from all commits
f328e43
206187f
90570aa
931195f
5bc4f89
26367e6
f907cdb
9dbeb50
2362722
d9644e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,34 +43,35 @@ namespace compute { | |
| class InvertKernel : public UnaryKernel { | ||
| Status Call(FunctionContext* ctx, const Datum& input, Datum* out) override { | ||
| DCHECK_EQ(Datum::ARRAY, input.kind()); | ||
| constexpr int64_t kZeroDestOffset = 0; | ||
|
|
||
| const ArrayData& in_data = *input.array(); | ||
| ArrayData* result; | ||
|
|
||
| out->value = ArrayData::Make(boolean(), in_data.length); | ||
| result = out->array().get(); | ||
| std::shared_ptr<ArrayData> result = out->array(); | ||
| result->type = boolean(); | ||
|
|
||
| // Allocate or copy bitmap | ||
| // Handle validity bitmap | ||
| result->null_count = in_data.null_count; | ||
| std::shared_ptr<Buffer> validity_bitmap = in_data.buffers[0]; | ||
| const std::shared_ptr<Buffer>& validity_bitmap = in_data.buffers[0]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case I need the reference to the shared_ptr because I copy it below (I suppose I could refactor slightly to avoid this). I changed the one below since it is not needed there. |
||
| if (in_data.offset != 0) { | ||
| RETURN_NOT_OK(CopyBitmap(ctx->memory_pool(), validity_bitmap->data(), | ||
| in_data.offset, in_data.length, &validity_bitmap)); | ||
| DCHECK_LE(BitUtil::BytesForBits(in_data.length), validity_bitmap->size()); | ||
| CopyBitmap(validity_bitmap->data(), in_data.offset, in_data.length, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The validity bitmap may not be allocated at all if the null count is 0, so this will crash |
||
| result->buffers[0]->mutable_data(), kZeroDestOffset); | ||
| } else { | ||
| result->buffers[0] = validity_bitmap; | ||
| } | ||
| result->buffers.push_back(validity_bitmap); | ||
|
|
||
| // Allocate output data buffer | ||
| std::shared_ptr<Buffer> data_buffer; | ||
| RETURN_NOT_OK(InvertBitmap(ctx->memory_pool(), in_data.buffers[1]->data(), | ||
| in_data.offset, in_data.length, &data_buffer)); | ||
| result->buffers.push_back(data_buffer); | ||
|
|
||
| // Handle output data buffer | ||
| const std::shared_ptr<Buffer>& data_buffer = in_data.buffers[1]; | ||
| DCHECK_LE(BitUtil::BytesForBits(in_data.length), data_buffer->size()); | ||
| InvertBitmap(data_buffer->data(), in_data.offset, in_data.length, | ||
| result->buffers[1]->mutable_data(), kZeroDestOffset); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code should probably not be executed if the length is 0. Technically it is not invalid for a buffer to be null if it is zero length. This suggests that the kernel should exit early if the length is 0 and avoid special casing here |
||
| return Status::OK(); | ||
| } | ||
| }; | ||
|
|
||
| Status Invert(FunctionContext* ctx, const Datum& value, Datum* out) { | ||
| InvertKernel kernel; | ||
| detail::PrimitiveAllocatingUnaryKernel kernel( | ||
| std::unique_ptr<UnaryKernel>(new InvertKernel())); | ||
|
|
||
| std::vector<Datum> result; | ||
| RETURN_NOT_OK(detail::InvokeUnaryArrayKernel(ctx, &kernel, value, &result)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,13 +20,15 @@ | |
| #include <algorithm> | ||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "arrow/array.h" | ||
| #include "arrow/status.h" | ||
| #include "arrow/table.h" | ||
| #include "arrow/util/logging.h" | ||
|
|
||
| #include "arrow/compute/context.h" | ||
| #include "arrow/compute/kernel.h" | ||
|
|
||
| namespace arrow { | ||
|
|
@@ -162,6 +164,47 @@ Datum WrapDatumsLike(const Datum& value, const std::vector<Datum>& datums) { | |
| } | ||
| } | ||
|
|
||
| PrimitiveAllocatingUnaryKernel::PrimitiveAllocatingUnaryKernel( | ||
| std::unique_ptr<UnaryKernel> delegate) | ||
| : delegate_(std::move(delegate)) {} | ||
|
|
||
| inline void ZeroLastByte(Buffer* buffer) { | ||
| *(buffer->mutable_data() + (buffer->size() - 1)) = 0; | ||
| } | ||
|
|
||
| Status PrimitiveAllocatingUnaryKernel::Call(FunctionContext* ctx, const Datum& input, | ||
| Datum* out) { | ||
| std::vector<std::shared_ptr<Buffer>> data_buffers; | ||
| const ArrayData& in_data = *input.array(); | ||
| MemoryPool* pool = ctx->memory_pool(); | ||
|
|
||
| // Handle the validity buffer. | ||
| if (in_data.offset == 0) { | ||
| // Validity bitmap will be zero copied | ||
| data_buffers.emplace_back(); | ||
| } else { | ||
| std::shared_ptr<Buffer> buffer; | ||
| RETURN_NOT_OK(AllocateBitmap(pool, in_data.length, &buffer)); | ||
| // Per spec all trailing bits should indicate nullness, since | ||
| // the last byte might only be partially set, we ensure the | ||
| // remaining bit is set. | ||
| ZeroLastByte(buffer.get()); | ||
| buffer->ZeroPadding(); | ||
| data_buffers.push_back(buffer); | ||
| } | ||
| // Allocate the boolean value buffer. | ||
| std::shared_ptr<Buffer> buffer; | ||
| RETURN_NOT_OK(AllocateBitmap(pool, in_data.length, &buffer)); | ||
| // Some utility methods access the last byte before it might be | ||
| // initialized this makes valgrind/asan unhappy, so we proactively | ||
| // zero it. | ||
| ZeroLastByte(buffer.get()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I learned recently that valgrind can detect bit-level access of uninitialized memory so that may be part of it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case byte level was sufficient. I suppose I could have been more fine-grained with only zeroing out untouched bits but that doesn't seem worth it. |
||
| data_buffers.push_back(buffer); | ||
| out->value = ArrayData::Make(null(), in_data.length, data_buffers); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There may be a better way to deal with assignment of output types, but we'll need more "Operator" infrastructure to be able to refactor
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By operator infrastructure, do you mean something like my initial attempts at the PR? Or some of the questions asked in ARROW-4333?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean "operator infrastructure" similar to TensorFlow's operators https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/ops
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Operators compose with each other, on execution they dispatch to kernels |
||
|
|
||
| return delegate_->Call(ctx, input, out); | ||
| } | ||
|
|
||
| } // namespace detail | ||
| } // namespace compute | ||
| } // namespace arrow | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this is not part of the PR, I think it's worth returning a
Status::Invalid. In production this would likely trigger a segfault further down while invokinginput.array().There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is fair, i tried to keep this style in the code I added. I would prefer to tackle this in one comprehensive cleanup if that is OK with you (https://issues.apache.org/jira/browse/ARROW-4264)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general I think we should type check as high in the call stack as possible. If this were being invoked inside a query engine I do not think it should be possible to pass the wrong kind of argument unless there is an error with the implementation. These kinds of "can't fail" errors (the result of Arrow developer error, not user input) should be caught with DCHECK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a comment on the follow-up JIRA. I would lean slightly towards returning a bad-status but I could go either way, has this been discussed on the e-mail list? is it worth discussing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really think we are putting the cart before the horse. These kernels will be invoked at a higher layer in general, and IMHO the type checking should occur there. I'm overwhelmed with other projects right now but I hope to shift my focus to the operator algebra (basically I plan to build a C++ version of https://github.com/ibis-project/ibis) before the end of quarter which will help resolve this uncertainty