@@ -21,8 +21,8 @@ use crate::arrays::PrimitiveArray;
2121use crate :: arrays:: VarBin ;
2222use crate :: arrays:: VarBinArray ;
2323use crate :: arrays:: dict:: TakeExecute ;
24+ use crate :: arrays:: piecewise_sequence:: SpareBufferWriter ;
2425use crate :: arrays:: piecewise_sequence:: constant_unsigned_usize;
25- use crate :: arrays:: piecewise_sequence:: copy_slice_to_spare;
2626use crate :: arrays:: piecewise_sequence:: maybe_contiguous_slices;
2727use crate :: arrays:: primitive:: PrimitiveArrayExt ;
2828use crate :: arrays:: varbin:: VarBinArrayExt ;
@@ -475,7 +475,7 @@ where
475475 }
476476
477477 let mut new_data = ByteBufferMut :: with_capacity ( output_bytes) ;
478- let mut cursor = 0usize ;
478+ let mut writer = SpareBufferWriter :: new ( & mut new_data , output_bytes ) ? ;
479479 for & start in starts {
480480 let start = start. as_ ( ) ;
481481 if length == 0 {
@@ -485,20 +485,10 @@ where
485485 let offset_range = & offsets[ start..] [ ..=length] ;
486486 let byte_start = offset_range[ 0 ] . as_ ( ) ;
487487 let byte_end = offset_range[ length] . as_ ( ) ;
488- cursor = copy_slice_to_spare (
489- & mut new_data,
490- cursor,
491- & data[ byte_start..] [ ..byte_end - byte_start] ,
492- output_bytes,
493- ) ?;
488+ // SAFETY: the first pass computed `output_bytes` from these same byte ranges.
489+ unsafe { writer. copy_slice_unchecked ( & data[ byte_start..] [ ..byte_end - byte_start] ) } ;
494490 }
495- vortex_ensure ! (
496- cursor == output_bytes,
497- "VarBin byte copy length {cursor} does not match computed byte length {output_bytes}"
498- ) ;
499- // SAFETY: `copy_slice_to_spare` checked every write against `output_bytes`, and `cursor ==
500- // output_bytes` proves all bytes were initialized.
501- unsafe { new_data. set_len ( output_bytes) } ;
491+ writer. finish ( ) ?;
502492
503493 let offsets = PrimitiveArray :: new ( new_offsets. freeze ( ) , Validity :: NonNullable )
504494 . reinterpret_cast ( out_offset_ptype)
@@ -565,7 +555,7 @@ where
565555 ) ;
566556
567557 let mut new_data = ByteBufferMut :: with_capacity ( output_bytes) ;
568- let mut cursor = 0usize ;
558+ let mut writer = SpareBufferWriter :: new ( & mut new_data , output_bytes ) ? ;
569559 for ( & start, & length) in starts. iter ( ) . zip_eq ( lengths) {
570560 let start = start. as_ ( ) ;
571561 let length = length. as_ ( ) ;
@@ -576,20 +566,10 @@ where
576566 let offset_range = & offsets[ start..] [ ..=length] ;
577567 let byte_start = offset_range[ 0 ] . as_ ( ) ;
578568 let byte_end = offset_range[ length] . as_ ( ) ;
579- cursor = copy_slice_to_spare (
580- & mut new_data,
581- cursor,
582- & data[ byte_start..] [ ..byte_end - byte_start] ,
583- output_bytes,
584- ) ?;
569+ // SAFETY: the first pass computed `output_bytes` from these same byte ranges.
570+ unsafe { writer. copy_slice_unchecked ( & data[ byte_start..] [ ..byte_end - byte_start] ) } ;
585571 }
586- vortex_ensure ! (
587- cursor == output_bytes,
588- "VarBin byte copy length {cursor} does not match computed byte length {output_bytes}"
589- ) ;
590- // SAFETY: `copy_slice_to_spare` checked every write against `output_bytes`, and `cursor ==
591- // output_bytes` proves all bytes were initialized.
592- unsafe { new_data. set_len ( output_bytes) } ;
572+ writer. finish ( ) ?;
593573
594574 let offsets = PrimitiveArray :: new ( new_offsets. freeze ( ) , Validity :: NonNullable )
595575 . reinterpret_cast ( out_offset_ptype)
0 commit comments