| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use icu_normalizer::{ComposingNormalizerBorrowed, DecomposingNormalizerBorrowed}; |
| 6 |
|
| 7 |
use criterion::{black_box, Criterion, Throughput}; |
| 8 |
|
| 9 |
use smallvec::SmallVec; |
| 10 |
|
| 11 |
//use detone::IterDecomposeVietnamese; |
| 12 |
|
| 13 |
// 2048 times size of u16 fits on one 4KB memory page, which maximizes |
| 14 |
// the run to take average over without introducing cross-page effects. |
| 15 |
const INPUT_SIZE: usize = 2048; |
| 16 |
|
| 17 |
fn generate_bmp_input_nfc(s: &str) -> Vec<u16> { |
| 18 |
ComposingNormalizerBorrowed::new_nfc() |
| 19 |
.normalize_iter(s.chars().cycle()) |
| 20 |
.take(INPUT_SIZE) |
| 21 |
.map(|c| { |
| 22 |
if c <= '\u{FFFF}' { |
| 23 |
c as u16 |
| 24 |
} else { |
| 25 |
unreachable!("Data should stay on the BMP!") |
| 26 |
} |
| 27 |
}) |
| 28 |
.collect() |
| 29 |
} |
| 30 |
|
| 31 |
fn generate_bmp_input_nfd(s: &str) -> Vec<u16> { |
| 32 |
DecomposingNormalizerBorrowed::new_nfd() |
| 33 |
.normalize_iter(s.chars().cycle()) |
| 34 |
.take(INPUT_SIZE) |
| 35 |
.map(|c| { |
| 36 |
if c <= '\u{FFFF}' { |
| 37 |
c as u16 |
| 38 |
} else { |
| 39 |
unreachable!("Data should stay on the BMP!") |
| 40 |
} |
| 41 |
}) |
| 42 |
.collect() |
| 43 |
} |
| 44 |
|
| 45 |
/// Removes headers and replaces line feed with space. |
| 46 |
/// Do not use for languages that don't use spaces! |
| 47 |
fn prepare_file_contents(content: &str) -> String { |
| 48 |
content |
| 49 |
.lines() |
| 50 |
.filter(|&s| !s.starts_with('#')) |
| 51 |
.map(|s| s.to_owned()) |
| 52 |
.collect::<Vec<String>>() |
| 53 |
.join(" ") |
| 54 |
} |
| 55 |
|
| 56 |
fn slice_as_slice(s: &[u16]) -> &[u16] { |
| 57 |
black_box(s) |
| 58 |
} |
| 59 |
|
| 60 |
fn bench_lang(name: &str, data: &str, c: &mut Criterion) { |
| 61 |
let input_nfc = generate_bmp_input_nfc(data); |
| 62 |
let input_nfd = generate_bmp_input_nfd(data); |
| 63 |
let nfc = ComposingNormalizerBorrowed::new_nfc(); |
| 64 |
let nfd = DecomposingNormalizerBorrowed::new_nfd(); |
| 65 |
|
| 66 |
// Appending to this output is infallible (does not return `Err`) and |
| 67 |
// this is sized to be large enough not to actually take the the heap |
| 68 |
// allocation path. |
| 69 |
let mut output: SmallVec<[u16; INPUT_SIZE * 2]> = SmallVec::new(); |
| 70 |
{ |
| 71 |
let mut group_name = "utf16_throughput_nfc_".to_string(); |
| 72 |
group_name.push_str(name); |
| 73 |
|
| 74 |
let mut group = c.benchmark_group(&group_name); |
| 75 |
group.throughput(Throughput::Elements(input_nfc.len() as u64)); |
| 76 |
|
| 77 |
group.bench_function("read", |b| { |
| 78 |
b.iter(|| { |
| 79 |
let _ = black_box( |
| 80 |
nfc.split_normalized_utf16(slice_as_slice(&input_nfc)) |
| 81 |
.0 |
| 82 |
.len(), |
| 83 |
); |
| 84 |
}) |
| 85 |
}); |
| 86 |
|
| 87 |
group.bench_function("writing_to_nfc", |b| { |
| 88 |
b.iter(|| { |
| 89 |
output.clear(); // Should be trivial and OK to do from within here. |
| 90 |
let _ = black_box( |
| 91 |
nfc.normalize_utf16_to(slice_as_slice(&input_nfc), black_box(&mut output)), |
| 92 |
); |
| 93 |
}) |
| 94 |
}); |
| 95 |
group.bench_function("writing_to_nfd", |b| { |
| 96 |
b.iter(|| { |
| 97 |
output.clear(); // Should be trivial and OK to do from within here. |
| 98 |
let _ = black_box( |
| 99 |
nfd.normalize_utf16_to(slice_as_slice(&input_nfc), black_box(&mut output)), |
| 100 |
); |
| 101 |
}) |
| 102 |
}); |
| 103 |
group.finish(); |
| 104 |
} |
| 105 |
{ |
| 106 |
let mut group_name = "utf16_throughput_nfd_".to_string(); |
| 107 |
group_name.push_str(name); |
| 108 |
|
| 109 |
let mut group = c.benchmark_group(&group_name); |
| 110 |
group.throughput(Throughput::Elements(input_nfd.len() as u64)); |
| 111 |
|
| 112 |
group.bench_function("read", |b| { |
| 113 |
b.iter(|| { |
| 114 |
let _ = black_box( |
| 115 |
nfd.split_normalized_utf16(slice_as_slice(&input_nfd)) |
| 116 |
.0 |
| 117 |
.len(), |
| 118 |
); |
| 119 |
}) |
| 120 |
}); |
| 121 |
|
| 122 |
group.bench_function("writing_to_nfd", |b| { |
| 123 |
b.iter(|| { |
| 124 |
output.clear(); // Should be trivial and OK to do from within here. |
| 125 |
let _ = black_box( |
| 126 |
nfd.normalize_utf16_to(slice_as_slice(&input_nfd), black_box(&mut output)), |
| 127 |
); |
| 128 |
}) |
| 129 |
}); |
| 130 |
group.bench_function("writing_to_nfc", |b| { |
| 131 |
b.iter(|| { |
| 132 |
output.clear(); // Should be trivial and OK to do from within here. |
| 133 |
let _ = black_box( |
| 134 |
nfc.normalize_utf16_to(slice_as_slice(&input_nfd), black_box(&mut output)), |
| 135 |
); |
| 136 |
}) |
| 137 |
}); |
| 138 |
group.finish(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
static EL: &str = include_str!("./data/TestRandomWordsUDHR_el.txt"); |
| 143 |
static EN: &str = "The ICU4X normalizer is an implementation of Unicode Normalization Forms. "; |
| 144 |
static FR: &str = include_str!("./data/TestRandomWordsUDHR_fr.txt"); |
| 145 |
static VI: &str = include_str!("./data/wotw.txt"); |
| 146 |
static ZH: &str = "單父人呂公善沛令,辟仇,從之客,因家焉。沛中豪傑吏聞令有重客,皆往賀。"; |
| 147 |
// zh text from https://www.gutenberg.org/cache/epub/23841/pg23841.txt |
| 148 |
// metadata at https://www.gutenberg.org/ebooks/23841 |
| 149 |
// If you replace this text, be sure not to include ASCII spaces and be sure |
| 150 |
// to include punctuation using code points actually used for punctuation in |
| 151 |
// Chinese. |
| 152 |
|
| 153 |
// TODO: Add: |
| 154 |
// * Japanese with realistic proportion of kana voicing marks |
| 155 |
// * Korean, since Hangul is special-cased in the normalizer |
| 156 |
// * Kannada or some other non-Korean BMP language that uses |
| 157 |
// backward-combining starters (with realistic proportion of such |
| 158 |
// characters). |
| 159 |
// * Chakma or some other living non-BMP language. |
| 160 |
// * Vietnamese in the orthographic form (i.e. as produced by |
| 161 |
// the official non-IME keyboard layout that's less common |
| 162 |
// than the NFC-producing IME.) |
| 163 |
|
| 164 |
pub fn criterion_benchmark(c: &mut Criterion) { |
| 165 |
bench_lang("el", prepare_file_contents(EL).as_str(), c); |
| 166 |
bench_lang("en", EN, c); |
| 167 |
bench_lang("fr", prepare_file_contents(FR).as_str(), c); |
| 168 |
bench_lang("vi", prepare_file_contents(VI).as_str(), c); |
| 169 |
bench_lang("zh", ZH, c); |
| 170 |
} |