JakeDern commented on code in PR #10128:
URL: https://github.com/apache/arrow-rs/pull/10128#discussion_r3653724591
##########
arrow-ipc/src/writer.rs:
##########
@@ -1836,41 +1885,56 @@ pub fn write_message<W: Write>(
));
}
+ let aligned_size = write_message_header(&mut writer, &encoded.ipc_message,
write_options)?;
+
+ // write arrow data
+ let body_len = if arrow_data_len > 0 {
+ write_body_buffer(&mut writer, &encoded.arrow_data,
write_options.alignment)?
+ } else {
+ 0
+ };
+
+ Ok((aligned_size, body_len))
+}
+
+/// Write the encapsulated-message header to `writer`: the continuation marker
and
+/// metadata length, the flatbuffer metadata (`ipc_message`), and the alignment
+/// padding that follows it.
+///
+/// Returns the padded header length (continuation prefix + metadata +
padding),
+/// which is also the value encoded in the continuation length field.
+fn write_message_header<W: Write>(
+ writer: &mut W,
+ ipc_message: &[u8],
+ write_options: &IpcWriteOptions,
+) -> Result<usize, ArrowError> {
let a = usize::from(write_options.alignment - 1);
- let buffer = encoded.ipc_message;
- let flatbuf_size = buffer.len();
+ let flatbuf_size = ipc_message.len();
let prefix_size = if write_options.write_legacy_ipc_format {
4
} else {
8
};
let aligned_size = (flatbuf_size + prefix_size + a) & !a;
- let padding_bytes = aligned_size - flatbuf_size - prefix_size;
- write_continuation(
- &mut writer,
+ write_continuation_and_meta_size(
+ &mut *writer,
write_options,
(aligned_size - prefix_size) as i32,
)?;
// write the flatbuf
if flatbuf_size > 0 {
- writer.write_all(&buffer)?;
+ writer.write_all(ipc_message)?;
}
- // write padding
- writer.write_all(&PADDING[..padding_bytes])?;
- // write arrow data
- let body_len = if arrow_data_len > 0 {
- write_body_buffers(&mut writer, &encoded.arrow_data,
write_options.alignment)?
- } else {
- 0
- };
+ // write padding
+ writer.write_all(&PADDING[..aligned_size - flatbuf_size - prefix_size])?;
Review Comment:
I added a couple more comments throughout the function to improve the
readability. I think reading the entire function including the comment at the
top explains the math pretty clearly.
We write three parts for the message - The prefix, the encoded flatbuffer,
and padding. This is explained by the function comment.
I added comments explaining how we compute the alignment.
This is the remaining padding required hit the aligned size, which is
clearly the aligned size minus the size of the other two parts.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]