Apache DataFusion SQL Query Engine
perf: Optimize heap handling in TopK operator (#20556)
Adam Gutglicka026e7da2fe8
diff --git a/datafusion/physical-plan/src/topk/mod.rs b/datafusion/physical-plan/src/topk/mod.rsindex 4b93e6a188..e0b91f2516 100644--- a/datafusion/physical-plan/src/topk/mod.rs+++ b/datafusion/physical-plan/src/topk/mod.rs@@ -724,8 +724,8 @@ impl TopKHeap {let row = row.as_ref();// Reuse storage for evicted item if possible- let new_top_k = if self.inner.len() == self.k {- let prev_min = self.inner.pop().unwrap();+ if self.inner.len() == self.k {+ let mut prev_min = self.inner.peek_mut().unwrap();// Update batch useif prev_min.batch_id == batch_entry.id {@@ -736,15 +736,16 @@ impl TopKHeap {// update memory accountingself.owned_bytes -= prev_min.owned_size();- prev_min.with_new_row(row, batch_id, index)- } else {- TopKRow::new(row, batch_id, index)- };- self.owned_bytes += new_top_k.owned_size();+ prev_min.replace_with(row, batch_id, index);- // put the new row into the heap- self.inner.push(new_top_k)+ self.owned_bytes += prev_min.owned_size();+ } else {+ let new_row = TopKRow::new(row, batch_id, index);+ self.owned_bytes += new_row.owned_size();+ // put the new row into the heap+ self.inner.push(new_row);+ };}/// Returns the values stored in this heap, from values low to@@ -911,26 +912,13 @@ impl TopKRow {}}- /// Create a new TopKRow reusing the existing allocation- fn with_new_row(- self,- new_row: impl AsRef<[u8]>,- batch_id: u32,- index: usize,- ) -> Self {- let Self {- mut row,- batch_id: _,- index: _,- } = self;- row.clear();- row.extend_from_slice(new_row.as_ref());+ // Replace the existing row capacity with new values+ fn replace_with(&mut self, new_row: impl AsRef<[u8]>, batch_id: u32, index: usize) {+ self.row.clear();+ self.row.extend_from_slice(new_row.as_ref());- Self {- row,- batch_id,- index,- }+ self.batch_id = batch_id;+ self.index = index;}/// Returns the number of bytes owned by this row in the heap (notdiff --git a/datafusion/sqllogictest/test_files/limit.slt b/datafusion/sqllogictest/test_files/limit.sltindex ec8363f51a..ff3c49485a 100644--- a/datafusion/sqllogictest/test_files/limit.slt+++ b/datafusion/sqllogictest/test_files/limit.slt@@ -679,19 +679,19 @@ ON t1.b = t2.bORDER BY t1.b desc, c desc, c2 desc;----3 98 96-3 98 89+3 98 873 98 823 98 793 97 96-3 97 89+3 97 873 97 823 97 793 96 96-3 96 89+3 96 873 96 823 96 793 95 96-3 95 89+3 95 873 95 823 95 79diff --git a/datafusion/sqllogictest/test_files/window.slt b/datafusion/sqllogictest/test_files/window.sltindex 8ac8724683..c3e6f39adb 100644--- a/datafusion/sqllogictest/test_files/window.slt+++ b/datafusion/sqllogictest/test_files/window.slt@@ -4387,9 +4387,9 @@ LIMIT 5;----78 5063 38-3 53+NULL 1924 31-14 94+24 56# result should be same with above, when LAG/LEAD algorithm work with pruned data.# decreasing batch size, causes data to be produced in smaller chunks at the source.@@ -4406,9 +4406,9 @@ LIMIT 5;----78 5063 38-3 53+NULL 1924 31-14 94+24 56statement okset datafusion.execution.batch_size = 100;