@@ -253,11 +253,7 @@ impl<T> Buffer<T> {
253253 pub fn empty_aligned ( alignment : Alignment ) -> Self {
254254 const { assert ! ( size_of:: <T >( ) != 0 , "ZSTs are not supported" ) } ;
255255 if !alignment. is_aligned_to ( Alignment :: of :: < T > ( ) ) {
256- vortex_panic ! (
257- "Alignment {} must align to the scalar type's alignment {}" ,
258- alignment,
259- Alignment :: of:: <T >( ) ,
260- ) ;
256+ misaligned_scalar_type ( alignment, Alignment :: of :: < T > ( ) ) ;
261257 }
262258 Self {
263259 ptr : empty_ptr ( ) ,
@@ -303,21 +299,13 @@ impl<T> Buffer<T> {
303299 pub fn from_byte_buffer_aligned ( buffer : ByteBuffer , alignment : Alignment ) -> Self {
304300 const { assert ! ( size_of:: <T >( ) != 0 , "ZSTs are not supported" ) } ;
305301 if !alignment. is_aligned_to ( Alignment :: of :: < T > ( ) ) {
306- vortex_panic ! (
307- "Alignment {} must be compatible with the scalar type's alignment {}" ,
308- alignment,
309- Alignment :: of:: <T >( ) ,
310- ) ;
302+ incompatible_scalar_alignment ( alignment, Alignment :: of :: < T > ( ) ) ;
311303 }
312304 if !alignment. is_ptr_aligned ( buffer. as_ptr ( ) ) {
313- vortex_panic ! ( "Buffer must align to the requested alignment {}" , alignment) ;
305+ buffer_not_aligned ( alignment) ;
314306 }
315307 if !buffer. len ( ) . is_multiple_of ( size_of :: < T > ( ) ) {
316- vortex_panic ! (
317- "Buffer length {} must be a multiple of the scalar type's size {}" ,
318- buffer. len( ) ,
319- size_of:: <T >( )
320- ) ;
308+ buffer_len_not_multiple ( buffer. len ( ) , size_of :: < T > ( ) ) ;
321309 }
322310 Self {
323311 ptr : buffer. ptr . cast ( ) ,
@@ -336,24 +324,13 @@ impl<T> Buffer<T> {
336324 pub fn from_bytes_aligned ( bytes : Bytes , alignment : Alignment ) -> Self {
337325 const { assert ! ( size_of:: <T >( ) != 0 , "ZSTs are not supported" ) } ;
338326 if !alignment. is_aligned_to ( Alignment :: of :: < T > ( ) ) {
339- vortex_panic ! (
340- "Alignment {} must be compatible with the scalar type's alignment {}" ,
341- alignment,
342- Alignment :: of:: <T >( ) ,
343- ) ;
327+ incompatible_scalar_alignment ( alignment, Alignment :: of :: < T > ( ) ) ;
344328 }
345329 if !alignment. is_ptr_aligned ( bytes. as_ptr ( ) ) {
346- vortex_panic ! (
347- "Bytes alignment must align to the requested alignment {}" ,
348- alignment,
349- ) ;
330+ bytes_not_aligned ( alignment) ;
350331 }
351332 if !bytes. len ( ) . is_multiple_of ( size_of :: < T > ( ) ) {
352- vortex_panic ! (
353- "Bytes length {} must be a multiple of the scalar type's size {}" ,
354- bytes. len( ) ,
355- size_of:: <T >( )
356- ) ;
333+ bytes_len_not_multiple ( bytes. len ( ) , size_of :: < T > ( ) ) ;
357334 }
358335 Self :: from_bytes ( bytes, alignment)
359336 }
@@ -504,14 +481,10 @@ impl<T> Buffer<T> {
504481 } ;
505482
506483 if begin > end {
507- vortex_panic ! (
508- "range start must not be greater than end: {:?} <= {:?}" ,
509- begin,
510- end
511- ) ;
484+ slice_range_inverted ( begin, end) ;
512485 }
513486 if end > len {
514- vortex_panic ! ( "range end out of bounds: {:?} > {:?}" , end, len) ;
487+ slice_range_out_of_bounds ( end, len) ;
515488 }
516489
517490 if end == begin {
@@ -522,10 +495,7 @@ impl<T> Buffer<T> {
522495
523496 let begin_byte = begin * size_of :: < T > ( ) ;
524497 if !alignment. is_offset_aligned ( begin_byte) {
525- vortex_panic ! (
526- "range start must be aligned to {alignment:?}, byte {}" ,
527- begin_byte
528- ) ;
498+ slice_start_misaligned ( alignment, begin_byte) ;
529499 }
530500 if !alignment. is_aligned_to ( Alignment :: of :: < T > ( ) ) {
531501 vortex_panic ! ( "Slice alignment must at least align to type T" )
@@ -972,6 +942,66 @@ impl<T> From<BufferMut<T>> for Buffer<T> {
972942 }
973943}
974944
945+ // If panic helpers are inlined, this makes callers of BufferMut's functions
946+ // not inline-eligible. This is more ugly than direct panics but helps with
947+ // performance
948+
949+ #[ cold]
950+ #[ inline( never) ]
951+ fn misaligned_scalar_type ( alignment : Alignment , scalar_align : Alignment ) -> ! {
952+ vortex_panic ! ( "Alignment {alignment} must align to the scalar type's alignment {scalar_align}" )
953+ }
954+
955+ #[ cold]
956+ #[ inline( never) ]
957+ fn incompatible_scalar_alignment ( alignment : Alignment , scalar_align : Alignment ) -> ! {
958+ vortex_panic ! (
959+ "Alignment {alignment} must be compatible with the scalar type's alignment {scalar_align}"
960+ )
961+ }
962+
963+ #[ cold]
964+ #[ inline( never) ]
965+ fn buffer_not_aligned ( alignment : Alignment ) -> ! {
966+ vortex_panic ! ( "Buffer must align to the requested alignment {alignment}" )
967+ }
968+
969+ #[ cold]
970+ #[ inline( never) ]
971+ fn bytes_not_aligned ( alignment : Alignment ) -> ! {
972+ vortex_panic ! ( "Bytes alignment must align to the requested alignment {alignment}" )
973+ }
974+
975+ #[ cold]
976+ #[ inline( never) ]
977+ fn buffer_len_not_multiple ( len : usize , scalar_size : usize ) -> ! {
978+ vortex_panic ! ( "Buffer length {len} must be a multiple of the scalar type's size {scalar_size}" )
979+ }
980+
981+ #[ cold]
982+ #[ inline( never) ]
983+ fn bytes_len_not_multiple ( len : usize , scalar_size : usize ) -> ! {
984+ vortex_panic ! ( "Bytes length {len} must be a multiple of the scalar type's size {scalar_size}" )
985+ }
986+
987+ #[ cold]
988+ #[ inline( never) ]
989+ fn slice_range_inverted ( begin : usize , end : usize ) -> ! {
990+ vortex_panic ! ( "range start must not be greater than end: {begin:?} <= {end:?}" )
991+ }
992+
993+ #[ cold]
994+ #[ inline( never) ]
995+ fn slice_range_out_of_bounds ( end : usize , len : usize ) -> ! {
996+ vortex_panic ! ( "range end out of bounds: {end:?} > {len:?}" )
997+ }
998+
999+ #[ cold]
1000+ #[ inline( never) ]
1001+ fn slice_start_misaligned ( alignment : Alignment , begin_byte : usize ) -> ! {
1002+ vortex_panic ! ( "range start must be aligned to {alignment:?}, byte {begin_byte}" )
1003+ }
1004+
9751005#[ cfg( test) ]
9761006mod test {
9771007 use std:: mem:: align_of;
0 commit comments