Skip to content

Commit 04c9f7a

Browse files
committed
perf: keep ASCII and fullwidth digits off the table lookup
Normalizing every char through the Nd table slowed down already supported fullwidth and mixed ASCII/fullwidth input. Scan bytes instead, copying ASCII directly and mapping the fullwidth UTF-8 encoding in place as before, and only decode a char for the table lookup otherwise. The output buffer is preallocated, since it is never longer than the input. Add fullwidth, mixed ASCII/fullwidth and Arabic-Indic cases to the cast_from_string benchmark.
1 parent fb5f351 commit 04c9f7a

2 files changed

Lines changed: 80 additions & 6 deletions

File tree

‎native/spark-expr/benches/cast_from_string.rs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,29 @@ fn criterion_benchmark(c: &mut Criterion) {
136136
group.finish();
137137
}
138138

139+
// str -> decimal with non-ASCII digits, which take the normalization path in
140+
// `parse_string_to_decimal` instead of the pure-ASCII fast path
141+
let spark_cast_options = SparkCastOptions::new(EvalMode::Legacy, "", false);
142+
let cast = Cast::new(
143+
expr.clone(),
144+
DataType::Decimal128(38, 10),
145+
spark_cast_options,
146+
None,
147+
None,
148+
);
149+
let mut group = c.benchmark_group("cast_string_to_decimal_unicode/legacy");
150+
for (name, zero, every_other) in [
151+
("fullwidth", '\u{FF10}', false),
152+
("mixed_ascii_fullwidth", '\u{FF10}', true),
153+
("arabic_indic", '\u{0660}', false),
154+
] {
155+
let batch = create_unicode_decimal_cast_string_batch(zero, every_other);
156+
group.bench_function(name, |b| {
157+
b.iter(|| cast.evaluate(&batch).unwrap());
158+
});
159+
}
160+
group.finish();
161+
139162
// str -> boolean and str -> float benchmarks, with and without the leading/trailing
140163
// whitespace that exercises the trim helpers in `conversion_funcs::trim`
141164
let bool_batch = create_boolean_string_batch(false);
@@ -252,6 +275,29 @@ fn create_decimal_cast_string_batch() -> RecordBatch {
252275
})
253276
}
254277

278+
/// Create batch of decimal strings whose digits are re-spelled in the script whose zero is
279+
/// `zero`, either all of them or every other one (mixing with ASCII)
280+
fn create_unicode_decimal_cast_string_batch(zero: char, every_other: bool) -> RecordBatch {
281+
let mut rng = StdRng::seed_from_u64(42);
282+
string_batch(8192, 10, move |_| {
283+
let ascii = format!(
284+
"-{}.{}",
285+
rng.random_range(0..1_000_000_000u32),
286+
rng.random_range(0..1_000_000_000u32)
287+
);
288+
ascii
289+
.chars()
290+
.enumerate()
291+
.map(|(i, c)| match c.to_digit(10) {
292+
Some(d) if !every_other || i % 2 == 0 => {
293+
char::from_u32(u32::from(zero) + d).unwrap()
294+
}
295+
_ => c,
296+
})
297+
.collect()
298+
})
299+
}
300+
255301
fn config() -> Criterion {
256302
Criterion::default()
257303
}

‎native/spark-expr/src/conversion_funcs/string.rs‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,40 @@ fn non_ascii_digit_value(c: char) -> Option<u8> {
532532
/// can be mixed freely ("1٢e३" is 12000). All other characters are kept unchanged and are
533533
/// rejected later by [`parse_decimal_str`].
534534
fn normalize_unicode_digits(s: &str) -> String {
535-
s.chars()
536-
.map(|c| match non_ascii_digit_value(c) {
537-
Some(d) => char::from(b'0' + d),
538-
None => c,
539-
})
540-
.collect()
535+
// Every non-ASCII digit is at least two UTF-8 bytes and becomes one, so the output is
536+
// never longer than the input.
537+
let mut out = Vec::with_capacity(s.len());
538+
let bytes = s.as_bytes();
539+
let mut i = 0;
540+
while i < bytes.len() {
541+
let b = bytes[i];
542+
// ASCII (including mixed ASCII/fullwidth input) and fullwidth digits, encoded as
543+
// [0xEF, 0xBC, 0x90 + n], are the common cases, so they skip the table lookup.
544+
if b.is_ascii() {
545+
out.push(b);
546+
i += 1;
547+
} else if b == 0xEF
548+
&& bytes.get(i + 1) == Some(&0xBC)
549+
&& matches!(bytes.get(i + 2), Some(0x90..=0x99))
550+
{
551+
// e.g. 0x91 - 0x60 = 0x31 = b'1'
552+
out.push(bytes[i + 2] - 0x60);
553+
i += 3;
554+
} else {
555+
// `i` is on a char boundary: every branch above advances by a whole char.
556+
let mut rest = s[i..].chars();
557+
let c = rest.next().unwrap();
558+
let next = bytes.len() - rest.as_str().len();
559+
match non_ascii_digit_value(c) {
560+
Some(d) => out.push(b'0' + d),
561+
None => out.extend_from_slice(&bytes[i..next]),
562+
}
563+
i = next;
564+
}
565+
}
566+
// SAFETY: every branch either copies a whole UTF-8 encoded char unchanged or replaces it
567+
// with a single ASCII byte, so the output is valid UTF-8 because the input is.
568+
unsafe { String::from_utf8_unchecked(out) }
541569
}
542570

543571
/// Powers of ten that fit in an `i128` (`10^0` through `10^38`).

0 commit comments

Comments
 (0)