You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/07/13 23:04:46 UTC

[GitHub] [arrow-rs] viirya opened a new pull request, #2061: Use `read_volatile` in `BitChunkIterator.next`

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

   # 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 #2060.
   
   # 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.
   -->
   
   
   ```
   boolean_append_packed   time:   [19.721 us 19.777 us 19.835 us]                                   
                           change: [-12.491% -10.883% -9.3860%] (p = 0.00 < 0.05)                                                                                                                                                         
                           Performance has improved.                                                                  
   Found 6 outliers among 100 measurements (6.00%)                                                                                                                                                                                        
     3 (3.00%) high mild                                                                                                                                                                                                                  
     3 (3.00%) high severe                                                                                                                                                                                                                
   ```
   
   The benchmark is somehow fluctuating. But mostly I see `read_volatile` is faster.
   
   # 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.
   -->
   
   # 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


[GitHub] [arrow-rs] viirya commented on a diff in pull request #2061: Use `read_volatile` in `BitChunkIterator.next`

Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2061:
URL: https://github.com/apache/arrow-rs/pull/2061#discussion_r921749773


##########
arrow/src/util/bit_chunk_iterator.rs:
##########
@@ -333,7 +333,7 @@ impl Iterator for BitChunkIterator<'_> {
             // the constructor ensures that bit_offset is in 0..8
             // that means we need to read at most one additional byte to fill in the high bits
             let next = unsafe {
-                std::ptr::read_unaligned(raw_data.add(index + 1) as *const u8) as u64
+                std::ptr::read_volatile(raw_data.add(index + 1) as *const u8) as u64

Review Comment:
   Hmm, I tried to code two mock versions of `next` function into `https://rust.godbolt.org/`. The compiled results look the same. I assume it is accurate so the difference might be just fluctuating results. I'm going to close this and investigate if there is other place for optimizing `BitChunkIterator`.



-- 
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] viirya commented on pull request #2061: Use `read_volatile` in `BitChunkIterator.next`

Posted by GitBox <gi...@apache.org>.
viirya commented on PR #2061:
URL: https://github.com/apache/arrow-rs/pull/2061#issuecomment-1183944715

   @sunchao Updated. Thanks.


-- 
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] viirya commented on a diff in pull request #2061: Use `read_volatile` in `BitChunkIterator.next`

Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2061:
URL: https://github.com/apache/arrow-rs/pull/2061#discussion_r920756928


##########
arrow/src/util/bit_chunk_iterator.rs:
##########
@@ -333,7 +333,7 @@ impl Iterator for BitChunkIterator<'_> {
             // the constructor ensures that bit_offset is in 0..8
             // that means we need to read at most one additional byte to fill in the high bits
             let next = unsafe {
-                std::ptr::read_unaligned(raw_data.add(index + 1) as *const u8) as u64
+                std::ptr::read_volatile(raw_data.add(index + 1) as *const u8) as u64

Review Comment:
   I think the doc is for `read_unaligned` usage that accesses the buffer pointer as u64:
   
   ```
   let current = unsafe { std::ptr::read_unaligned(raw_data.add(index)).to_le() };
   ```
   
   For accessing `next`, we only read a byte and case the byte to u64. So it is not a problem.
   
   



-- 
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] jhorstmann commented on a diff in pull request #2061: Use `read_volatile` in `BitChunkIterator.next`

Posted by GitBox <gi...@apache.org>.
jhorstmann commented on code in PR #2061:
URL: https://github.com/apache/arrow-rs/pull/2061#discussion_r920876190


##########
arrow/src/util/bit_chunk_iterator.rs:
##########
@@ -333,7 +333,7 @@ impl Iterator for BitChunkIterator<'_> {
             // the constructor ensures that bit_offset is in 0..8
             // that means we need to read at most one additional byte to fill in the high bits
             let next = unsafe {
-                std::ptr::read_unaligned(raw_data.add(index + 1) as *const u8) as u64
+                std::ptr::read_volatile(raw_data.add(index + 1) as *const u8) as u64

Review Comment:
   Plain `std::ptr::read` should then work just as well for reading a byte. I'm surprised this makes a difference because the compiler should know that bytes can't really be unaligned.
   
   The copy inside the implementation of `read_unaligned` is something that the compiler will optimize away since the size is statically known. My only guess would be that the slightly larger code before optimizations somehow influences inlining decisions somewhere else.



-- 
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] viirya commented on pull request #2061: Use `read_volatile` in `BitChunkIterator.next`

Posted by GitBox <gi...@apache.org>.
viirya commented on PR #2061:
URL: https://github.com/apache/arrow-rs/pull/2061#issuecomment-1183765514

   cc @sunchao 


-- 
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] sunchao commented on a diff in pull request #2061: Use `read_volatile` in `BitChunkIterator.next`

Posted by GitBox <gi...@apache.org>.
sunchao commented on code in PR #2061:
URL: https://github.com/apache/arrow-rs/pull/2061#discussion_r920727396


##########
arrow/src/util/bit_chunk_iterator.rs:
##########
@@ -333,7 +333,7 @@ impl Iterator for BitChunkIterator<'_> {
             // the constructor ensures that bit_offset is in 0..8
             // that means we need to read at most one additional byte to fill in the high bits
             let next = unsafe {
-                std::ptr::read_unaligned(raw_data.add(index + 1) as *const u8) as u64
+                std::ptr::read_volatile(raw_data.add(index + 1) as *const u8) as u64

Review Comment:
   I'm not sure if this breaks the alignment requirement since `read_volatile` requires `T` to be aligned while `read_unaligned` does not.
   
   At line 320, there is a comment:
   ```
   cast to *const u64 should be fine since we are using read_unaligned below
   ```
   and this is no longer valid. 



-- 
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] viirya commented on a diff in pull request #2061: Use `read_volatile` in `BitChunkIterator.next`

Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #2061:
URL: https://github.com/apache/arrow-rs/pull/2061#discussion_r921749773


##########
arrow/src/util/bit_chunk_iterator.rs:
##########
@@ -333,7 +333,7 @@ impl Iterator for BitChunkIterator<'_> {
             // the constructor ensures that bit_offset is in 0..8
             // that means we need to read at most one additional byte to fill in the high bits
             let next = unsafe {
-                std::ptr::read_unaligned(raw_data.add(index + 1) as *const u8) as u64
+                std::ptr::read_volatile(raw_data.add(index + 1) as *const u8) as u64

Review Comment:
   Hmm, I tried to code two mock versions of `next` function into `https://rust.godbolt.org/` (https://rust.godbolt.org/z/c4fEh45rb). The compiled results look the same. I assume it is accurate so the difference might be just fluctuating results. I'm going to close this and investigate if there is other place for optimizing `BitChunkIterator`.



-- 
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] viirya closed pull request #2061: Use `read_volatile` in `BitChunkIterator.next`

Posted by GitBox <gi...@apache.org>.
viirya closed pull request #2061: Use `read_volatile` in `BitChunkIterator.next`
URL: https://github.com/apache/arrow-rs/pull/2061


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