Skip to content

Commit e71e7a3

Browse files
authored
chore: Cleanup code to use repeat_n in a few places (#20527)
## Which issue does this PR close? N/A ## Rationale for this change Using `repeat_n` is more readable and slightly faster than `(0..n).map(|_| ...)`. ## What changes are included in this PR? ## Are these changes tested? Yes. ## Are there any user-facing changes? No.
1 parent 670dbf4 commit e71e7a3

6 files changed

Lines changed: 18 additions & 16 deletions

File tree

datafusion/core/benches/spm.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ fn generate_spm_for_round_robin_tie_breaker(
6666
RecordBatch::try_from_iter(vec![("a", a), ("b", b), ("c", c)]).unwrap()
6767
};
6868

69-
let rbs = (0..batch_count).map(|_| rb.clone()).collect::<Vec<_>>();
70-
let partitions = vec![rbs.clone(); partition_count];
71-
7269
let schema = rb.schema();
70+
let rbs = std::iter::repeat_n(rb, batch_count).collect::<Vec<_>>();
71+
let partitions = vec![rbs.clone(); partition_count];
7372
let sort = [
7473
PhysicalSortExpr {
7574
expr: col("b", &schema).unwrap(),

datafusion/core/tests/fuzz_cases/spilling_fuzz_in_memory_constrained_env.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,11 @@ async fn run_sort_test_with_limited_memory(
278278

279279
let string_item_size =
280280
record_batch_memory_size / record_batch_size as usize;
281-
let string_array = Arc::new(StringArray::from_iter_values(
282-
(0..record_batch_size).map(|_| "a".repeat(string_item_size)),
283-
));
281+
let string_array =
282+
Arc::new(StringArray::from_iter_values(std::iter::repeat_n(
283+
"a".repeat(string_item_size),
284+
record_batch_size as usize,
285+
)));
284286

285287
RecordBatch::try_new(
286288
Arc::clone(&schema),
@@ -536,9 +538,11 @@ async fn run_test_aggregate_with_high_cardinality(
536538

537539
let string_item_size =
538540
record_batch_memory_size / record_batch_size as usize;
539-
let string_array = Arc::new(StringArray::from_iter_values(
540-
(0..record_batch_size).map(|_| "a".repeat(string_item_size)),
541-
));
541+
let string_array =
542+
Arc::new(StringArray::from_iter_values(std::iter::repeat_n(
543+
"a".repeat(string_item_size),
544+
record_batch_size as usize,
545+
)));
542546

543547
RecordBatch::try_new(
544548
Arc::clone(&schema),

datafusion/physical-plan/src/async_func.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ mod tests {
392392
vec![Arc::new(UInt32Array::from(vec![1, 2, 3, 4, 5, 6]))],
393393
)?;
394394

395-
let batches: Vec<RecordBatch> = (0..50).map(|_| batch.clone()).collect();
395+
let batches: Vec<RecordBatch> = std::iter::repeat_n(batch, 50).collect();
396396

397397
let session_config = SessionConfig::new().with_batch_size(200);
398398
let task_ctx = TaskContext::default().with_session_config(session_config);

datafusion/physical-plan/src/repartition/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2483,7 +2483,7 @@ mod tests {
24832483
/// Create vector batches
24842484
fn create_vec_batches(n: usize) -> Vec<RecordBatch> {
24852485
let batch = create_batch();
2486-
(0..n).map(|_| batch.clone()).collect()
2486+
std::iter::repeat_n(batch, n).collect()
24872487
}
24882488

24892489
/// Create batch

datafusion/physical-plan/src/sorts/sort_preserving_merge.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,9 @@ mod tests {
475475
let b: ArrayRef = Arc::new(StringArray::from_iter(vec![Some("a"); row_size]));
476476
let c: ArrayRef = Arc::new(Int64Array::from_iter(vec![0; row_size]));
477477
let rb = RecordBatch::try_from_iter(vec![("a", a), ("b", b), ("c", c)])?;
478-
479-
let rbs = (0..1024).map(|_| rb.clone()).collect::<Vec<_>>();
480-
481478
let schema = rb.schema();
479+
480+
let rbs = std::iter::repeat_n(rb, 1024).collect::<Vec<_>>();
482481
let sort = [
483482
PhysicalSortExpr {
484483
expr: col("b", &schema)?,

datafusion/spark/src/function/map/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ fn map_deduplicate_keys(
181181
let num_keys_entries = *next_keys_offset as usize - cur_keys_offset;
182182
let num_values_entries = *next_values_offset as usize - cur_values_offset;
183183

184-
let mut keys_mask_one = [false].repeat(num_keys_entries);
185-
let mut values_mask_one = [false].repeat(num_values_entries);
184+
let mut keys_mask_one = vec![false; num_keys_entries];
185+
let mut values_mask_one = vec![false; num_values_entries];
186186

187187
let key_is_valid = keys_nulls.is_none_or(|buf| buf.is_valid(row_idx));
188188
let value_is_valid = values_nulls.is_none_or(|buf| buf.is_valid(row_idx));

0 commit comments

Comments
 (0)