forked from apache/datafusion-comet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_element_append.rs
More file actions
272 lines (237 loc) · 10.2 KB
/
array_element_append.rs
File metadata and controls
272 lines (237 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Micro-benchmarks for SparkUnsafeArray element iteration.
//!
//! This tests the low-level `append_to_builder` function which converts
//! SparkUnsafeArray elements to Arrow array builders. This is the inner loop
//! used when processing List/Array columns in JVM shuffle.
use arrow::array::builder::{
Date32Builder, Float64Builder, Int32Builder, Int64Builder, TimestampMicrosecondBuilder,
};
use arrow::datatypes::{DataType, TimeUnit};
use comet::execution::shuffle::spark_unsafe::list::{append_to_builder, SparkUnsafeArray};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
const NUM_ELEMENTS: usize = 10000;
/// Create a SparkUnsafeArray in memory with i32 elements.
/// Layout:
/// - 8 bytes: num_elements (i64)
/// - null bitset: 8 bytes per 64 elements
/// - element data: 4 bytes per element (i32)
fn create_spark_unsafe_array_i32(num_elements: usize, with_nulls: bool) -> Vec<u8> {
// Header size: 8 (num_elements) + ceil(num_elements/64) * 8 (null bitset)
let null_bitset_words = num_elements.div_ceil(64);
let header_size = 8 + null_bitset_words * 8;
let data_size = num_elements * 4; // i32 = 4 bytes
let total_size = header_size + data_size;
let mut buffer = vec![0u8; total_size];
// Write num_elements
buffer[0..8].copy_from_slice(&(num_elements as i64).to_le_bytes());
// Write null bitset (set every 10th element as null if with_nulls)
if with_nulls {
for i in (0..num_elements).step_by(10) {
let word_idx = i / 64;
let bit_idx = i % 64;
let word_offset = 8 + word_idx * 8;
let current_word =
i64::from_le_bytes(buffer[word_offset..word_offset + 8].try_into().unwrap());
let new_word = current_word | (1i64 << bit_idx);
buffer[word_offset..word_offset + 8].copy_from_slice(&new_word.to_le_bytes());
}
}
// Write element data
for i in 0..num_elements {
let offset = header_size + i * 4;
buffer[offset..offset + 4].copy_from_slice(&(i as i32).to_le_bytes());
}
buffer
}
/// Create a SparkUnsafeArray in memory with i64 elements.
fn create_spark_unsafe_array_i64(num_elements: usize, with_nulls: bool) -> Vec<u8> {
let null_bitset_words = num_elements.div_ceil(64);
let header_size = 8 + null_bitset_words * 8;
let data_size = num_elements * 8; // i64 = 8 bytes
let total_size = header_size + data_size;
let mut buffer = vec![0u8; total_size];
// Write num_elements
buffer[0..8].copy_from_slice(&(num_elements as i64).to_le_bytes());
// Write null bitset
if with_nulls {
for i in (0..num_elements).step_by(10) {
let word_idx = i / 64;
let bit_idx = i % 64;
let word_offset = 8 + word_idx * 8;
let current_word =
i64::from_le_bytes(buffer[word_offset..word_offset + 8].try_into().unwrap());
let new_word = current_word | (1i64 << bit_idx);
buffer[word_offset..word_offset + 8].copy_from_slice(&new_word.to_le_bytes());
}
}
// Write element data
for i in 0..num_elements {
let offset = header_size + i * 8;
buffer[offset..offset + 8].copy_from_slice(&(i as i64).to_le_bytes());
}
buffer
}
/// Create a SparkUnsafeArray in memory with f64 elements.
fn create_spark_unsafe_array_f64(num_elements: usize, with_nulls: bool) -> Vec<u8> {
let null_bitset_words = num_elements.div_ceil(64);
let header_size = 8 + null_bitset_words * 8;
let data_size = num_elements * 8; // f64 = 8 bytes
let total_size = header_size + data_size;
let mut buffer = vec![0u8; total_size];
// Write num_elements
buffer[0..8].copy_from_slice(&(num_elements as i64).to_le_bytes());
// Write null bitset
if with_nulls {
for i in (0..num_elements).step_by(10) {
let word_idx = i / 64;
let bit_idx = i % 64;
let word_offset = 8 + word_idx * 8;
let current_word =
i64::from_le_bytes(buffer[word_offset..word_offset + 8].try_into().unwrap());
let new_word = current_word | (1i64 << bit_idx);
buffer[word_offset..word_offset + 8].copy_from_slice(&new_word.to_le_bytes());
}
}
// Write element data
for i in 0..num_elements {
let offset = header_size + i * 8;
buffer[offset..offset + 8].copy_from_slice(&(i as f64).to_le_bytes());
}
buffer
}
fn benchmark_array_conversion(c: &mut Criterion) {
let mut group = c.benchmark_group("spark_unsafe_array_to_arrow");
// Benchmark i32 array conversion
for with_nulls in [false, true] {
let buffer = create_spark_unsafe_array_i32(NUM_ELEMENTS, with_nulls);
let array = SparkUnsafeArray::new(buffer.as_ptr() as i64);
let null_str = if with_nulls { "with_nulls" } else { "no_nulls" };
group.bench_with_input(
BenchmarkId::new("i32", null_str),
&(&array, &buffer),
|b, (array, _buffer)| {
b.iter(|| {
let mut builder = Int32Builder::with_capacity(NUM_ELEMENTS);
if with_nulls {
append_to_builder::<true>(&DataType::Int32, &mut builder, array).unwrap();
} else {
append_to_builder::<false>(&DataType::Int32, &mut builder, array).unwrap();
}
builder.finish()
});
},
);
}
// Benchmark i64 array conversion
for with_nulls in [false, true] {
let buffer = create_spark_unsafe_array_i64(NUM_ELEMENTS, with_nulls);
let array = SparkUnsafeArray::new(buffer.as_ptr() as i64);
let null_str = if with_nulls { "with_nulls" } else { "no_nulls" };
group.bench_with_input(
BenchmarkId::new("i64", null_str),
&(&array, &buffer),
|b, (array, _buffer)| {
b.iter(|| {
let mut builder = Int64Builder::with_capacity(NUM_ELEMENTS);
if with_nulls {
append_to_builder::<true>(&DataType::Int64, &mut builder, array).unwrap();
} else {
append_to_builder::<false>(&DataType::Int64, &mut builder, array).unwrap();
}
builder.finish()
});
},
);
}
// Benchmark f64 array conversion
for with_nulls in [false, true] {
let buffer = create_spark_unsafe_array_f64(NUM_ELEMENTS, with_nulls);
let array = SparkUnsafeArray::new(buffer.as_ptr() as i64);
let null_str = if with_nulls { "with_nulls" } else { "no_nulls" };
group.bench_with_input(
BenchmarkId::new("f64", null_str),
&(&array, &buffer),
|b, (array, _buffer)| {
b.iter(|| {
let mut builder = Float64Builder::with_capacity(NUM_ELEMENTS);
if with_nulls {
append_to_builder::<true>(&DataType::Float64, &mut builder, array).unwrap();
} else {
append_to_builder::<false>(&DataType::Float64, &mut builder, array)
.unwrap();
}
builder.finish()
});
},
);
}
// Benchmark date32 array conversion (same memory layout as i32)
for with_nulls in [false, true] {
let buffer = create_spark_unsafe_array_i32(NUM_ELEMENTS, with_nulls);
let array = SparkUnsafeArray::new(buffer.as_ptr() as i64);
let null_str = if with_nulls { "with_nulls" } else { "no_nulls" };
group.bench_with_input(
BenchmarkId::new("date32", null_str),
&(&array, &buffer),
|b, (array, _buffer)| {
b.iter(|| {
let mut builder = Date32Builder::with_capacity(NUM_ELEMENTS);
if with_nulls {
append_to_builder::<true>(&DataType::Date32, &mut builder, array).unwrap();
} else {
append_to_builder::<false>(&DataType::Date32, &mut builder, array).unwrap();
}
builder.finish()
});
},
);
}
// Benchmark timestamp array conversion (same memory layout as i64)
for with_nulls in [false, true] {
let buffer = create_spark_unsafe_array_i64(NUM_ELEMENTS, with_nulls);
let array = SparkUnsafeArray::new(buffer.as_ptr() as i64);
let null_str = if with_nulls { "with_nulls" } else { "no_nulls" };
group.bench_with_input(
BenchmarkId::new("timestamp", null_str),
&(&array, &buffer),
|b, (array, _buffer)| {
b.iter(|| {
let mut builder = TimestampMicrosecondBuilder::with_capacity(NUM_ELEMENTS);
let dt = DataType::Timestamp(TimeUnit::Microsecond, None);
if with_nulls {
append_to_builder::<true>(&dt, &mut builder, array).unwrap();
} else {
append_to_builder::<false>(&dt, &mut builder, array).unwrap();
}
builder.finish()
});
},
);
}
group.finish();
}
fn config() -> Criterion {
Criterion::default()
}
criterion_group! {
name = benches;
config = config();
targets = benchmark_array_conversion
}
criterion_main!(benches);