You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "Kikkon (via GitHub)" <gi...@apache.org> on 2024/03/03 08:32:18 UTC

[PR] Enhancement: Replace unsafe code [arrow-rs]

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

   # 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 #4937 .
   
   # 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.
   -->
   
   # What changes are included in this PR?
   
   Replace unsafe code with Vec::from_iter in Buffer::from_iter for improved safety and performance
   <!--
   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.
   -->
   
   # Are there any user-facing changes?
   
   
   <!--
   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


Re: [PR] Enhancement: Replace unsafe code [arrow-rs]

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

   Have you run any benchmarks to confirm this doesn't regress performance. The arithmetic kernels might be a good candidate


-- 
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


Re: [PR] Enhancement: Replace unsafe code [arrow-rs]

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

   @tustvold Thanks for your advice, I will first change it to the draft status. I will reopen it after I have finished supplementing the benchmark.


-- 
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


Re: [PR] Enhancement: Replace unsafe code [arrow-rs]

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


##########
arrow-buffer/src/buffer/immutable.rs:
##########
@@ -423,24 +423,11 @@ impl Buffer {
 
 impl<T: ArrowNativeType> FromIterator<T> for Buffer {
     fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
-        let mut iterator = iter.into_iter();
+        //let mut iterator = iter.into_iter();
         let size = std::mem::size_of::<T>();
-
-        // first iteration, which will likely reserve sufficient space for the buffer.
-        let mut buffer = match iterator.next() {
-            None => MutableBuffer::new(0),
-            Some(element) => {
-                let (lower, _) = iterator.size_hint();
-                let mut buffer = MutableBuffer::new(lower.saturating_add(1).saturating_mul(size));
-                unsafe {
-                    std::ptr::write(buffer.as_mut_ptr() as *mut T, element);
-                    buffer.set_len(size);
-                }
-                buffer
-            }
-        };
-
-        buffer.extend_from_iter(iterator);
+        let vec = Vec::from_iter(iter);

Review Comment:
   This method will now copy the data twice



##########
arrow-buffer/src/buffer/immutable.rs:
##########
@@ -423,24 +423,11 @@ impl Buffer {
 
 impl<T: ArrowNativeType> FromIterator<T> for Buffer {
     fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
-        let mut iterator = iter.into_iter();
+        //let mut iterator = iter.into_iter();
         let size = std::mem::size_of::<T>();
-
-        // first iteration, which will likely reserve sufficient space for the buffer.
-        let mut buffer = match iterator.next() {
-            None => MutableBuffer::new(0),
-            Some(element) => {
-                let (lower, _) = iterator.size_hint();
-                let mut buffer = MutableBuffer::new(lower.saturating_add(1).saturating_mul(size));
-                unsafe {
-                    std::ptr::write(buffer.as_mut_ptr() as *mut T, element);
-                    buffer.set_len(size);
-                }
-                buffer
-            }
-        };
-
-        buffer.extend_from_iter(iterator);
+        let vec = Vec::from_iter(iter);
+        let mut buffer = MutableBuffer::new(vec.len() * size);

Review Comment:
   You could use Buffer::from_vec here to avoid a copy



-- 
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


Re: [PR] Use Vec::from_iter in Buffer::from_iter [arrow-rs]

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


-- 
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


Re: [PR] Enhancement: Replace unsafe code [arrow-rs]

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

   Hi @tustvold ,
   After comparing with the baseline on the master branch, it appears that this change has no impact on performance. If there are no other concerns, perhaps it's ready for merging. By the way, are there any other issues suitable for newcomers? I think I might not be very familiar with this area yet.  (:


-- 
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