Apache DataFusion SQL Query Engine
Perf: Optimize CursorValues compare performance for StringViewArray (1.4X faster for sort-tpch Q11) (#16509)
Qi Zhu15a8738d0678
diff --git a/datafusion/physical-plan/src/sorts/cursor.rs b/datafusion/physical-plan/src/sorts/cursor.rsindex efb9c0a47b..17033e6a31 100644--- a/datafusion/physical-plan/src/sorts/cursor.rs+++ b/datafusion/physical-plan/src/sorts/cursor.rs@@ -293,14 +293,19 @@ impl CursorValues for StringViewArray {self.views().len()}+ #[inline(always)]fn eq(l: &Self, l_idx: usize, r: &Self, r_idx: usize) -> bool {// SAFETY: Both l_idx and r_idx are guaranteed to be within bounds,// and any null-checks are handled in the outer layers.// Fast path: Compare the lengths before full byte comparison.-let l_view = unsafe { l.views().get_unchecked(l_idx) };- let l_len = *l_view as u32;let r_view = unsafe { r.views().get_unchecked(r_idx) };++ if l.data_buffers().is_empty() && r.data_buffers().is_empty() {+ return l_view.eq(r_view);+ }++ let l_len = *l_view as u32;let r_len = *r_view as u32;if l_len != r_len {return false;@@ -309,13 +314,19 @@ impl CursorValues for StringViewArray {unsafe { GenericByteViewArray::compare_unchecked(l, l_idx, r, r_idx).is_eq() }}+ #[inline(always)]fn eq_to_previous(cursor: &Self, idx: usize) -> bool {// SAFETY: The caller guarantees that idx > 0 and the indices are valid.// Already checked it in is_eq_to_prev_one function// Fast path: Compare the lengths of the current and previous views.let l_view = unsafe { cursor.views().get_unchecked(idx) };- let l_len = *l_view as u32;let r_view = unsafe { cursor.views().get_unchecked(idx - 1) };+ if cursor.data_buffers().is_empty() {+ return l_view.eq(r_view);+ }++ let l_len = *l_view as u32;+let r_len = *r_view as u32;if l_len != r_len {return false;@@ -326,10 +337,21 @@ impl CursorValues for StringViewArray {}}+ #[inline(always)]fn compare(l: &Self, l_idx: usize, r: &Self, r_idx: usize) -> Ordering {// SAFETY: Prior assertions guarantee that l_idx and r_idx are valid indices.// Null-checks are assumed to have been handled in the wrapper (e.g., ArrayValues).// And the bound is checked in is_finished, it is safe to call get_unchecked+ if l.data_buffers().is_empty() && r.data_buffers().is_empty() {+ let l_view = unsafe { l.views().get_unchecked(l_idx) };+ let r_view = unsafe { r.views().get_unchecked(r_idx) };+ let l_len = *l_view as u32;+ let r_len = *r_view as u32;+ let l_data = unsafe { StringViewArray::inline_value(l_view, l_len as usize) };+ let r_data = unsafe { StringViewArray::inline_value(r_view, r_len as usize) };+ return l_data.cmp(r_data);+ }+unsafe { GenericByteViewArray::compare_unchecked(l, l_idx, r, r_idx) }}}