Skip to content

Commit

Permalink
Avoid linear search for character in bench helper
Browse files Browse the repository at this point in the history
Get the char in O(1) instead of O(n).
  • Loading branch information
findepi committed Feb 20, 2025
1 parent e7af47d commit e23a4af
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions datafusion/functions/benches/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn gen_string_array(
let rng_ref = &mut rng;

let corpus = "DataFusionДатаФусион数据融合📊🔥"; // includes utf8 encoding with 1~4 bytes
let corpus_char_count = corpus.chars().count();
let corpus = corpus.chars().collect::<Vec<_>>();

let mut output_string_vec: Vec<Option<String>> = Vec::with_capacity(n_rows);
for _ in 0..n_rows {
Expand All @@ -46,8 +46,7 @@ pub fn gen_string_array(
// Generate random UTF8 string
let mut generated_string = String::with_capacity(str_len_chars);
for _ in 0..str_len_chars {
let idx = rng_ref.gen_range(0..corpus_char_count);
let char = corpus.chars().nth(idx).unwrap();
let char = corpus[rng_ref.gen_range(0..corpus.len())];
generated_string.push(char);
}
output_string_vec.push(Some(generated_string));
Expand Down

0 comments on commit e23a4af

Please sign in to comment.