Skip to content

Commit a04a2ea

Browse files
joe-spiraljoseph-isaacs
authored andcommitted
refactor: use collect_bool in BitBuffer From<Vec<bool>> impl
Simplified the `From<&[bool]>` implementation for `BitBufferMut` to directly call the existing optimized `BitBuffer::collect_bool` function instead of duplicating its logic. **Before**: Duplicated the 64-bit packing logic inline (34 lines) **After**: Call the existing function (1 line) ```rust BitBuffer::collect_bool(value.len(), |i| value[i]).into_mut() ``` This maintains the same performance optimization while reducing code duplication and improving maintainability. Any future improvements to `collect_bool` will automatically benefit this code path as well.
1 parent 8d70278 commit a04a2ea

File tree

1 file changed

+2
-32
lines changed

1 file changed

+2
-32
lines changed

vortex-buffer/src/bit/buf_mut.rs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -541,38 +541,8 @@ impl Not for BitBufferMut {
541541

542542
impl From<&[bool]> for BitBufferMut {
543543
fn from(value: &[bool]) -> Self {
544-
// Use the same optimized approach as BitBuffer::collect_bool
545-
// Pack 64 booleans at a time into u64 words for better performance
546-
let len = value.len();
547-
let mut buffer = BufferMut::with_capacity(len.div_ceil(64) * 8);
548-
549-
let chunks = len / 64;
550-
let remainder = len % 64;
551-
for chunk in 0..chunks {
552-
let mut packed = 0u64;
553-
for bit_idx in 0..64 {
554-
let i = bit_idx + chunk * 64;
555-
packed |= (value[i] as u64) << bit_idx;
556-
}
557-
558-
// SAFETY: Already allocated sufficient capacity
559-
unsafe { buffer.push_unchecked(packed) }
560-
}
561-
562-
if remainder != 0 {
563-
let mut packed = 0u64;
564-
for bit_idx in 0..remainder {
565-
let i = bit_idx + chunks * 64;
566-
packed |= (value[i] as u64) << bit_idx;
567-
}
568-
569-
// SAFETY: Already allocated sufficient capacity
570-
unsafe { buffer.push_unchecked(packed) }
571-
}
572-
573-
buffer.truncate(len.div_ceil(8));
574-
575-
Self::from_buffer(buffer.freeze().into_byte_buffer().into_mut(), 0, len)
544+
// Use the optimized collect_bool to pack 64 booleans at a time
545+
BitBuffer::collect_bool(value.len(), |i| value[i]).into_mut()
576546
}
577547
}
578548

0 commit comments

Comments
 (0)