Skip to content

Commit 64b1109

Browse files
authored
apacheGH-41899: [C++] IPC: Minor enhance the code of writer (apache#41900)
### Rationale for this change Enhance the code of IPC writer ### What changes are included in this PR? 1. memcpy rather than reinterpret_cast 2. move the buffer rather than copying ### Are these changes tested? Covered by existing ### Are there any user-facing changes? No * GitHub Issue: apache#41899 Authored-by: mwish <[email protected]> Signed-off-by: mwish <[email protected]>
1 parent 03a960d commit 64b1109

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

cpp/src/arrow/ipc/writer.cc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class RecordBatchSerializer {
164164
std::shared_ptr<Buffer> bitmap;
165165
RETURN_NOT_OK(GetTruncatedBitmap(arr.offset(), arr.length(), arr.null_bitmap(),
166166
options_.memory_pool, &bitmap));
167-
out_->body_buffers.emplace_back(bitmap);
167+
out_->body_buffers.emplace_back(std::move(bitmap));
168168
} else {
169169
// Push a dummy zero-length buffer, not to be copied
170170
out_->body_buffers.emplace_back(kNullBuffer);
@@ -222,8 +222,9 @@ class RecordBatchSerializer {
222222
RETURN_NOT_OK(
223223
result->Resize(actual_length + sizeof(int64_t), /* shrink_to_fit= */ true));
224224
}
225-
*reinterpret_cast<int64_t*>(result->mutable_data()) =
226-
bit_util::ToLittleEndian(prefixed_length);
225+
int64_t prefixed_length_little_endian = bit_util::ToLittleEndian(prefixed_length);
226+
util::SafeStore(result->mutable_data(), prefixed_length_little_endian);
227+
227228
*out = SliceBuffer(std::move(result), /*offset=*/0, actual_length + sizeof(int64_t));
228229

229230
return Status::OK();
@@ -415,7 +416,7 @@ class RecordBatchSerializer {
415416
std::shared_ptr<Buffer> data;
416417
RETURN_NOT_OK(GetTruncatedBitmap(array.offset(), array.length(), array.values(),
417418
options_.memory_pool, &data));
418-
out_->body_buffers.emplace_back(data);
419+
out_->body_buffers.emplace_back(std::move(data));
419420
return Status::OK();
420421
}
421422

@@ -442,7 +443,7 @@ class RecordBatchSerializer {
442443
data->size() - byte_offset);
443444
data = SliceBuffer(data, byte_offset, buffer_length);
444445
}
445-
out_->body_buffers.emplace_back(data);
446+
out_->body_buffers.emplace_back(std::move(data));
446447
return Status::OK();
447448
}
448449

@@ -464,8 +465,8 @@ class RecordBatchSerializer {
464465
data = SliceBuffer(data, start_offset, slice_length);
465466
}
466467

467-
out_->body_buffers.emplace_back(value_offsets);
468-
out_->body_buffers.emplace_back(data);
468+
out_->body_buffers.emplace_back(std::move(value_offsets));
469+
out_->body_buffers.emplace_back(std::move(data));
469470
return Status::OK();
470471
}
471472

@@ -566,7 +567,7 @@ class RecordBatchSerializer {
566567
RETURN_NOT_OK(GetTruncatedBuffer(
567568
offset, length, static_cast<int32_t>(sizeof(UnionArray::type_code_t)),
568569
array.type_codes(), options_.memory_pool, &type_codes));
569-
out_->body_buffers.emplace_back(type_codes);
570+
out_->body_buffers.emplace_back(std::move(type_codes));
570571

571572
--max_recursion_depth_;
572573
for (int i = 0; i < array.num_fields(); ++i) {
@@ -585,7 +586,7 @@ class RecordBatchSerializer {
585586
RETURN_NOT_OK(GetTruncatedBuffer(
586587
offset, length, static_cast<int32_t>(sizeof(UnionArray::type_code_t)),
587588
array.type_codes(), options_.memory_pool, &type_codes));
588-
out_->body_buffers.emplace_back(type_codes);
589+
out_->body_buffers.emplace_back(std::move(type_codes));
589590

590591
--max_recursion_depth_;
591592
const auto& type = checked_cast<const UnionType&>(*array.type());
@@ -640,7 +641,7 @@ class RecordBatchSerializer {
640641

641642
value_offsets = std::move(shifted_offsets_buffer);
642643
}
643-
out_->body_buffers.emplace_back(value_offsets);
644+
out_->body_buffers.emplace_back(std::move(value_offsets));
644645

645646
// Visit children and slice accordingly
646647
for (int i = 0; i < type.num_fields(); ++i) {

0 commit comments

Comments
 (0)