alamb commented on code in PR #11818:
URL: https://github.com/apache/arrow-rs/pull/11818#discussion_r4431650167


##########
arrow-buffer/src/util/bit_chunk_iterator.rs:
##########
@@ -168,10 +170,46 @@ impl<'a> UnalignedBitChunk<'a> {
 
     /// Counts the number of ones
     pub fn count_ones(&self) -> usize {
-        self.iter().map(|x| x.count_ones() as usize).sum()
+        let edges = self.prefix.into_iter().chain(self.suffix);
+        edges.map(|x| x.count_ones() as usize).sum::<usize>() + 
count_ones_words(self.chunks)

Review Comment:
   does it matter that this seemingly will now read the first byte/word and 
then the last byte/word and then the middle? Like would it be better to keep 
the same order `prefix`, then `chunks` and then `suffix`?
   
   Something like
   
   ```rust
   self.prefix.into_iter().map(|x|.count_ones()).sum() + // prefix
   count_ones_words(self.chunks) + // chunks
   self.suffix.count_one() // suffix
   ``` ?
   
   



##########
arrow-buffer/src/util/bit_chunk_iterator.rs:
##########
@@ -168,10 +170,46 @@ impl<'a> UnalignedBitChunk<'a> {
 
     /// Counts the number of ones
     pub fn count_ones(&self) -> usize {
-        self.iter().map(|x| x.count_ones() as usize).sum()
+        let edges = self.prefix.into_iter().chain(self.suffix);
+        edges.map(|x| x.count_ones() as usize).sum::<usize>() + 
count_ones_words(self.chunks)
     }
 }
 
+#[cfg(not(target_arch = "aarch64"))]
+fn count_ones_words(words: &[u64]) -> usize {
+    words.iter().map(|x| x.count_ones() as usize).sum()
+}
+
+/// Summing four byte popcounts in `u8` lanes before widening lets LLVM emit 
one
+/// widening per 64 bytes. A `u64::count_ones` sum widens every 16 bytes. On 
x86
+/// the `u64` sum measured faster (#11776), so this is only used on aarch64.
+#[cfg(target_arch = "aarch64")]
+fn count_ones_words(words: &[u64]) -> usize {
+    let (chunks, tail) = words.as_chunks::<8>();

Review Comment:
   chunks is somewhat overloaded here -- like the input was self.chunks, and 
now I think this is making chunks 8 64-bit words (512 bit) and then `chunks` is 
divided by `chunks` again. Maybe a comment would help, or finding a more 
specifi cname



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

Reply via email to