You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "tustvold (via GitHub)" <gi...@apache.org> on 2023/03/28 11:22:13 UTC

[GitHub] [arrow-rs] tustvold opened a new pull request, #3967: Async writer tweaks

tustvold opened a new pull request, #3967:
URL: https://github.com/apache/arrow-rs/pull/3967

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #.
   
   # Rationale for this change
    
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   More consistent buffer handling
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   This makes two relatively minor tweaks to the AsyncArrowWriter added in https://github.com/apache/arrow-rs/pull/3957 by @ShiKaiWi:
   
   * Uses a futures::Mutex to avoid needing to take and replace the `SharedBuffer`
   * Explicit sizing of intermediate buffer, with eager allocation, to avoid expensive bump allocation
   
   # Are there any user-facing changes?
   
   No changes to released APIs
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please add the `breaking change` label.
   -->
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] ShiKaiWi commented on a diff in pull request #3967: Async writer tweaks

Posted by "ShiKaiWi (via GitHub)" <gi...@apache.org>.
ShiKaiWi commented on code in PR #3967:
URL: https://github.com/apache/arrow-rs/pull/3967#discussion_r1150469408


##########
parquet/src/arrow/async_writer/mod.rs:
##########
@@ -88,22 +85,25 @@ pub struct AsyncArrowWriter<W> {
 impl<W: AsyncWrite + Unpin + Send> AsyncArrowWriter<W> {
     /// Try to create a new Async Arrow Writer.
     ///
-    /// `buffer_flush_threshold` will be used to trigger flush of the inner buffer.
+    /// `buffer_size` determines the size of the intermediate buffer
+    ///
+    /// Flush will automatically be called by [`Self::write`] if
+    /// the buffer is at least half full
     pub fn try_new(
         writer: W,
         arrow_schema: SchemaRef,
-        buffer_flush_threshold: usize,
+        buffer_size: usize,
         props: Option<WriterProperties>,
     ) -> Result<Self> {
-        let shared_buffer = SharedBuffer::default();
+        let shared_buffer = SharedBuffer::new(buffer_size);

Review Comment:
   Actually, in the #3957, `buffer_flush_threshold` is designed to be able to be `usize::MAX` in order to let the async writer not flush until all the encoding work is done. And for this reason, the buffer can't be pre-allocated at initialization.
   
   And I think it is good here because of its efficiency, and it may be a fake feature to let the writer do flush only when all encoded bytes are ready. :satisfied:



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3967: Async writer tweaks

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #3967:
URL: https://github.com/apache/arrow-rs/pull/3967#discussion_r1150438584


##########
parquet/src/arrow/async_writer/mod.rs:
##########
@@ -135,64 +135,68 @@ impl<W: AsyncWrite + Unpin + Send> AsyncArrowWriter<W> {
         let metadata = self.sync_writer.close()?;
 
         // Force to flush the remaining data.
-        Self::try_flush(&self.shared_buffer, &mut self.async_writer, 0).await?;
+        Self::try_flush(&mut self.shared_buffer, &mut self.async_writer, 0).await?;
 
         Ok(metadata)
     }
 
     /// Flush the data in the [`SharedBuffer`] into the `async_writer` if its size
     /// exceeds the threshold.
     async fn try_flush(
-        shared_buffer: &SharedBuffer,
+        shared_buffer: &mut SharedBuffer,

Review Comment:
   A mutable reference isn't technically required here, but acts as a lint that shared_buffer shouldn't be shared



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] thinkharderdev commented on a diff in pull request #3967: Async writer tweaks

Posted by "thinkharderdev (via GitHub)" <gi...@apache.org>.
thinkharderdev commented on code in PR #3967:
URL: https://github.com/apache/arrow-rs/pull/3967#discussion_r1150759812


##########
parquet/src/arrow/async_writer/mod.rs:
##########
@@ -135,64 +135,68 @@ impl<W: AsyncWrite + Unpin + Send> AsyncArrowWriter<W> {
         let metadata = self.sync_writer.close()?;
 
         // Force to flush the remaining data.
-        Self::try_flush(&self.shared_buffer, &mut self.async_writer, 0).await?;
+        Self::try_flush(&mut self.shared_buffer, &mut self.async_writer, 0).await?;
 
         Ok(metadata)
     }
 
     /// Flush the data in the [`SharedBuffer`] into the `async_writer` if its size
     /// exceeds the threshold.
     async fn try_flush(
-        shared_buffer: &SharedBuffer,
+        shared_buffer: &mut SharedBuffer,

Review Comment:
   Could we actually just remove the `Mutex` entirely? Hold a `Arc<SharedBuffer>` and use `Arc::get_mut` to grab a mutable reference



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3967: Async writer tweaks

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #3967:
URL: https://github.com/apache/arrow-rs/pull/3967#discussion_r1150822070


##########
parquet/src/arrow/async_writer/mod.rs:
##########
@@ -135,64 +135,68 @@ impl<W: AsyncWrite + Unpin + Send> AsyncArrowWriter<W> {
         let metadata = self.sync_writer.close()?;
 
         // Force to flush the remaining data.
-        Self::try_flush(&self.shared_buffer, &mut self.async_writer, 0).await?;
+        Self::try_flush(&mut self.shared_buffer, &mut self.async_writer, 0).await?;
 
         Ok(metadata)
     }
 
     /// Flush the data in the [`SharedBuffer`] into the `async_writer` if its size
     /// exceeds the threshold.
     async fn try_flush(
-        shared_buffer: &SharedBuffer,
+        shared_buffer: &mut SharedBuffer,

Review Comment:
   The use of try_lock here boils down to much the same thing - https://docs.rs/futures-util/0.3.27/src/futures_util/lock/mutex.rs.html#103



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3967: Async writer tweaks

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #3967:
URL: https://github.com/apache/arrow-rs/pull/3967#discussion_r1150479860


##########
parquet/src/arrow/async_writer/mod.rs:
##########
@@ -88,22 +85,25 @@ pub struct AsyncArrowWriter<W> {
 impl<W: AsyncWrite + Unpin + Send> AsyncArrowWriter<W> {
     /// Try to create a new Async Arrow Writer.
     ///
-    /// `buffer_flush_threshold` will be used to trigger flush of the inner buffer.
+    /// `buffer_size` determines the size of the intermediate buffer
+    ///
+    /// Flush will automatically be called by [`Self::write`] if
+    /// the buffer is at least half full
     pub fn try_new(
         writer: W,
         arrow_schema: SchemaRef,
-        buffer_flush_threshold: usize,
+        buffer_size: usize,
         props: Option<WriterProperties>,
     ) -> Result<Self> {
-        let shared_buffer = SharedBuffer::default();
+        let shared_buffer = SharedBuffer::new(buffer_size);

Review Comment:
   > fake feature to let the writer do flush only when all encoded bytes are read
   
   Yeah, at that point you might as well just use the sync writer 😅



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3967: Async writer tweaks

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #3967:
URL: https://github.com/apache/arrow-rs/pull/3967#discussion_r1150436804


##########
parquet/src/arrow/async_writer/mod.rs:
##########
@@ -88,22 +85,25 @@ pub struct AsyncArrowWriter<W> {
 impl<W: AsyncWrite + Unpin + Send> AsyncArrowWriter<W> {
     /// Try to create a new Async Arrow Writer.
     ///
-    /// `buffer_flush_threshold` will be used to trigger flush of the inner buffer.
+    /// `buffer_size` determines the size of the intermediate buffer
+    ///
+    /// Flush will automatically be called by [`Self::write`] if
+    /// the buffer is at least half full
     pub fn try_new(
         writer: W,
         arrow_schema: SchemaRef,
-        buffer_flush_threshold: usize,
+        buffer_size: usize,
         props: Option<WriterProperties>,
     ) -> Result<Self> {
-        let shared_buffer = SharedBuffer::default();
+        let shared_buffer = SharedBuffer::new(buffer_size);

Review Comment:
   This is the major motivation for this PR, being able to avoid bump allocation where the Vec is repeatedly resized is important for performance



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3967: Async writer tweaks

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #3967:
URL: https://github.com/apache/arrow-rs/pull/3967#discussion_r1150436804


##########
parquet/src/arrow/async_writer/mod.rs:
##########
@@ -88,22 +85,25 @@ pub struct AsyncArrowWriter<W> {
 impl<W: AsyncWrite + Unpin + Send> AsyncArrowWriter<W> {
     /// Try to create a new Async Arrow Writer.
     ///
-    /// `buffer_flush_threshold` will be used to trigger flush of the inner buffer.
+    /// `buffer_size` determines the size of the intermediate buffer
+    ///
+    /// Flush will automatically be called by [`Self::write`] if
+    /// the buffer is at least half full
     pub fn try_new(
         writer: W,
         arrow_schema: SchemaRef,
-        buffer_flush_threshold: usize,
+        buffer_size: usize,
         props: Option<WriterProperties>,
     ) -> Result<Self> {
-        let shared_buffer = SharedBuffer::default();
+        let shared_buffer = SharedBuffer::new(buffer_size);

Review Comment:
   This is the major motivation for this PR, avoiding bump allocation where the Vec is repeatedly resized is important for performance



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3967: Async writer tweaks

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #3967:
URL: https://github.com/apache/arrow-rs/pull/3967#discussion_r1150780629


##########
parquet/src/arrow/async_writer/mod.rs:
##########
@@ -135,64 +135,68 @@ impl<W: AsyncWrite + Unpin + Send> AsyncArrowWriter<W> {
         let metadata = self.sync_writer.close()?;
 
         // Force to flush the remaining data.
-        Self::try_flush(&self.shared_buffer, &mut self.async_writer, 0).await?;
+        Self::try_flush(&mut self.shared_buffer, &mut self.async_writer, 0).await?;
 
         Ok(metadata)
     }
 
     /// Flush the data in the [`SharedBuffer`] into the `async_writer` if its size
     /// exceeds the threshold.
     async fn try_flush(
-        shared_buffer: &SharedBuffer,
+        shared_buffer: &mut SharedBuffer,

Review Comment:
   `Arc::get_mut` only works if there are no other `Arc` references, which in this case wouldn't be the case



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold merged pull request #3967: Async writer tweaks

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold merged PR #3967:
URL: https://github.com/apache/arrow-rs/pull/3967


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] ShiKaiWi commented on a diff in pull request #3967: Async writer tweaks

Posted by "ShiKaiWi (via GitHub)" <gi...@apache.org>.
ShiKaiWi commented on code in PR #3967:
URL: https://github.com/apache/arrow-rs/pull/3967#discussion_r1150469408


##########
parquet/src/arrow/async_writer/mod.rs:
##########
@@ -88,22 +85,25 @@ pub struct AsyncArrowWriter<W> {
 impl<W: AsyncWrite + Unpin + Send> AsyncArrowWriter<W> {
     /// Try to create a new Async Arrow Writer.
     ///
-    /// `buffer_flush_threshold` will be used to trigger flush of the inner buffer.
+    /// `buffer_size` determines the size of the intermediate buffer
+    ///
+    /// Flush will automatically be called by [`Self::write`] if
+    /// the buffer is at least half full
     pub fn try_new(
         writer: W,
         arrow_schema: SchemaRef,
-        buffer_flush_threshold: usize,
+        buffer_size: usize,
         props: Option<WriterProperties>,
     ) -> Result<Self> {
-        let shared_buffer = SharedBuffer::default();
+        let shared_buffer = SharedBuffer::new(buffer_size);

Review Comment:
   Actually, in the #3957, `buffer_flush_threshold` is designed to be able to be `usize::MAX` in order to let the async writer not flush until all the encoding work is done. And for this reason, the buffer can't be pre-allocated at initialization.
   
   And now I think it looks good here because of its efficiency, and it may be a fake feature to let the writer do flush only when all encoded bytes are ready. :satisfied:



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on pull request #3967: Async writer tweaks

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #3967:
URL: https://github.com/apache/arrow-rs/pull/3967#issuecomment-1486678650

   @ShiKaiWi perhaps you could take a look and let me know what you think


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] ShiKaiWi commented on pull request #3967: Async writer tweaks

Posted by "ShiKaiWi (via GitHub)" <gi...@apache.org>.
ShiKaiWi commented on PR #3967:
URL: https://github.com/apache/arrow-rs/pull/3967#issuecomment-1486729544

   > @ShiKaiWi perhaps you could take a look and let me know what you think
   
   This PR looks fairly pretty for me.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] thinkharderdev commented on a diff in pull request #3967: Async writer tweaks

Posted by "thinkharderdev (via GitHub)" <gi...@apache.org>.
thinkharderdev commented on code in PR #3967:
URL: https://github.com/apache/arrow-rs/pull/3967#discussion_r1150820905


##########
parquet/src/arrow/async_writer/mod.rs:
##########
@@ -135,64 +135,68 @@ impl<W: AsyncWrite + Unpin + Send> AsyncArrowWriter<W> {
         let metadata = self.sync_writer.close()?;
 
         // Force to flush the remaining data.
-        Self::try_flush(&self.shared_buffer, &mut self.async_writer, 0).await?;
+        Self::try_flush(&mut self.shared_buffer, &mut self.async_writer, 0).await?;
 
         Ok(metadata)
     }
 
     /// Flush the data in the [`SharedBuffer`] into the `async_writer` if its size
     /// exceeds the threshold.
     async fn try_flush(
-        shared_buffer: &SharedBuffer,
+        shared_buffer: &mut SharedBuffer,

Review Comment:
   Ah right the async writer would also need a reference. I suppose you could hold an `Arc<Vec<u8>>` in the the async writer and then have `SharedBuffer` hold a `Weak<Vec<u8>>`. Not sure that would end up pencilling out just to remove an uncontended mutex lock though.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org