You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/05/04 20:27:48 UTC

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #6236: Simplify and speed up MemoryExec insert

alamb commented on code in PR #6236:
URL: https://github.com/apache/arrow-datafusion/pull/6236#discussion_r1185481665


##########
datafusion/core/src/physical_plan/memory.rs:
##########
@@ -327,43 +327,29 @@ impl ExecutionPlan for MemoryWriteExec {
         let batch_count = self.batches.len();
         let data = self.input.execute(partition, context)?;
         let schema = self.schema.clone();
-        let state = (data, self.batches[partition % batch_count].clone());
-
-        let adapter = if batch_count >= self.input.output_partitioning().partition_count()
-        {
-            // If the number of input partitions matches the number of MemTable partitions,
-            // use a lightweight implementation that doesn't utilize as many locks.
-            let stream = futures::stream::unfold(state, |mut state| async move {
-                // hold lock during the entire write
-                let mut locked = state.1.write().await;
-                loop {
-                    let batch = match state.0.next().await {
-                        Some(Ok(batch)) => batch,
-                        Some(Err(e)) => {
-                            drop(locked);
-                            return Some((Err(e), state));
-                        }
-                        None => return None,
-                    };
-                    locked.push(batch)
-                }
-            });
-            Box::pin(RecordBatchStreamAdapter::new(schema, stream))
-                as SendableRecordBatchStream
-        } else {
-            let stream = futures::stream::unfold(state, |mut state| async move {
-                loop {
-                    let batch = match state.0.next().await {
-                        Some(Ok(batch)) => batch,
-                        Some(Err(e)) => return Some((Err(e), state)),
-                        None => return None,
-                    };
-                    state.1.write().await.push(batch)
-                }
-            });
-            Box::pin(RecordBatchStreamAdapter::new(schema, stream))
-                as SendableRecordBatchStream
+        let state = StreamState {
+            data,
+            buffer: vec![],
+            batch: self.batches[partition % batch_count].clone(),
         };
+
+        let stream = futures::stream::unfold(state, |mut state| async move {
+            loop {
+                match state.data.next().await {

Review Comment:
   This version of the loop does not hold the lock at all until the end. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org