Skip to content

Commit 2385034

Browse files
committed
Add more improvements
1 parent 7ca5b13 commit 2385034

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/serialize.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ namespace sparrow_ipc
100100

101101
// arrow_arr.buffers[0] is the validity bitmap
102102
// arrow_arr.buffers[1] is the data buffer
103-
const uint8_t* validity_bitmap = static_cast<const uint8_t*>(arrow_arr.buffers[0]);
104-
const uint8_t* data_buffer = static_cast<const uint8_t*>(arrow_arr.buffers[1]);
103+
const auto validity_bitmap = static_cast<const uint8_t*>(arrow_arr.buffers[0]);
104+
const auto data_buffer = static_cast<const uint8_t*>(arrow_arr.buffers[1]);
105105

106106
// Calculate the size of the validity and data buffers
107107
int64_t validity_size = (arrow_arr.length + arrow_alignment - 1) / arrow_alignment;
@@ -133,10 +133,10 @@ namespace sparrow_ipc
133133
batch_builder.Finish(batch_message_offset);
134134

135135
// III - Append the RecordBatch message to the final buffer
136-
uint32_t batch_meta_len = batch_builder.GetSize(); // Get the size of the batch metadata
137-
int64_t aligned_batch_meta_len = align_to_8(batch_meta_len); // Calculate the padded length
136+
const uint32_t batch_meta_len = batch_builder.GetSize(); // Get the size of the batch metadata
137+
const int64_t aligned_batch_meta_len = align_to_8(batch_meta_len); // Calculate the padded length
138138

139-
size_t current_size = final_buffer.size(); // Get the current size (which is the end of the Schema message)
139+
const size_t current_size = final_buffer.size(); // Get the current size (which is the end of the Schema message)
140140
// Resize the buffer to append the new message
141141
final_buffer.resize(current_size + sizeof(uint32_t) + aligned_batch_meta_len + body_len);
142142
uint8_t* dst = final_buffer.data() + current_size; // Get a pointer to where the new message will start
@@ -147,7 +147,15 @@ namespace sparrow_ipc
147147
// Copy the RecordBatch metadata into the buffer
148148
memcpy(dst, batch_builder.GetBufferPointer(), batch_meta_len);
149149
// Add padding to align the body to an 8-byte boundary
150-
memset(dst + batch_meta_len, 0, aligned_batch_meta_len - batch_meta_len);
150+
if (aligned_batch_meta_len >= batch_meta_len)
151+
{
152+
memset(dst + batch_meta_len, 0, aligned_batch_meta_len - batch_meta_len);
153+
}
154+
else
155+
{
156+
throw std::runtime_error("aligned_batch_meta_len should be greater than batch_meta_len");
157+
}
158+
151159
dst += aligned_batch_meta_len;
152160
// Copy the actual data buffers (the message body) into the buffer
153161
if (validity_bitmap)
@@ -157,7 +165,8 @@ namespace sparrow_ipc
157165
else
158166
{
159167
// If validity_bitmap is null, it means there are no nulls
160-
memset(dst, 0xFF, validity_size);
168+
constexpr uint8_t no_nulls_bitmap = 0xFF;
169+
memset(dst, no_nulls_bitmap, validity_size);
161170
}
162171
dst += validity_size;
163172
if (data_buffer)
@@ -180,7 +189,7 @@ sparrow::primitive_array<T> deserialize_primitive_array(const std::vector<uint8_
180189
size_t current_offset = 0;
181190

182191
// I - Deserialize the Schema message
183-
uint32_t schema_meta_len;
192+
uint32_t schema_meta_len = 0;
184193
memcpy(&schema_meta_len, buf_ptr + current_offset, sizeof(schema_meta_len));
185194
current_offset += sizeof(uint32_t);
186195
auto schema_message = org::apache::arrow::flatbuf::GetMessage(buf_ptr + current_offset);
@@ -198,7 +207,7 @@ sparrow::primitive_array<T> deserialize_primitive_array(const std::vector<uint8_
198207
current_offset += schema_meta_len;
199208

200209
// II - Deserialize the RecordBatch message
201-
uint32_t batch_meta_len;
210+
uint32_t batch_meta_len = 0;
202211
memcpy(&batch_meta_len, buf_ptr + current_offset, sizeof(batch_meta_len));
203212
current_offset += sizeof(uint32_t);
204213
auto batch_message = org::apache::arrow::flatbuf::GetMessage(buf_ptr + current_offset);
@@ -220,10 +229,10 @@ sparrow::primitive_array<T> deserialize_primitive_array(const std::vector<uint8_
220229
int64_t validity_len = buffers_meta->Get(0)->length();
221230
int64_t data_len = buffers_meta->Get(1)->length();
222231

223-
uint8_t* validity_buffer_copy = new uint8_t[validity_len];
232+
auto validity_buffer_copy = new uint8_t[validity_len];
224233
memcpy(validity_buffer_copy, body_ptr + buffers_meta->Get(0)->offset(), validity_len);
225234

226-
uint8_t* data_buffer_copy = new uint8_t[data_len];
235+
auto data_buffer_copy = new uint8_t[data_len];
227236
memcpy(data_buffer_copy, body_ptr + buffers_meta->Get(1)->offset(), data_len);
228237

229238
// Get name

0 commit comments

Comments
 (0)