| 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_collections::codepointtrie::CodePointTrie; |
| 6 |
|
| 7 |
#[path = "tries/mod.rs"] |
| 8 |
mod tries; |
| 9 |
|
| 10 |
// The string has 41 chars. |
| 11 |
const SAMPLE_STRING_LATIN1: &str = "Declaration loremips umdolo loremipsompi"; |
| 12 |
const SAMPLE_STRING_MIXED: &str = "Dèclaråcion ЗАГАЛЬНА 世界人权宣言 𑄟𑄚𑄬𑄭𑄃𑄇𑄴𑄇𑄥𑄧𑄁𑄢𑄴"; |
| 13 |
|
| 14 |
fn get_trie_small() -> CodePointTrie<'static, u8> { |
| 15 |
CodePointTrie::try_new( |
| 16 |
tries::gc_small::HEADER, |
| 17 |
tries::gc_small::INDEX, |
| 18 |
tries::gc_small::DATA, |
| 19 |
) |
| 20 |
.unwrap() |
| 21 |
} |
| 22 |
|
| 23 |
fn get_trie_fast() -> CodePointTrie<'static, u8> { |
| 24 |
CodePointTrie::try_new( |
| 25 |
tries::gc_fast::HEADER, |
| 26 |
tries::gc_fast::INDEX, |
| 27 |
tries::gc_fast::DATA, |
| 28 |
) |
| 29 |
.unwrap() |
| 30 |
} |
| 31 |
|
| 32 |
fn bench_iai_cpt_overview(fast: bool, mixed: bool) { |
| 33 |
// Tests the instructions required to get CPT for 100,000 chars. |
| 34 |
|
| 35 |
let cpt = if fast { |
| 36 |
get_trie_fast() |
| 37 |
} else { |
| 38 |
get_trie_small() |
| 39 |
}; |
| 40 |
let sample = if mixed { |
| 41 |
SAMPLE_STRING_MIXED |
| 42 |
} else { |
| 43 |
SAMPLE_STRING_LATIN1 |
| 44 |
}; |
| 45 |
|
| 46 |
let mut i: u8 = 0; |
| 47 |
for c in sample.chars() { |
| 48 |
i = i.wrapping_add(cpt.get32(c as u32)) |
| 49 |
//i = i.wrapping_add(1); |
| 50 |
} |
| 51 |
|
| 52 |
// Ensure the loop is not DCEd |
| 53 |
assert!(i < 255); |
| 54 |
} |
| 55 |
|
| 56 |
fn bench_iai_cpt_latin_fast() { |
| 57 |
bench_iai_cpt_overview(true, false); |
| 58 |
} |
| 59 |
|
| 60 |
fn bench_iai_cpt_latin_small() { |
| 61 |
bench_iai_cpt_overview(false, false); |
| 62 |
} |
| 63 |
|
| 64 |
fn bench_iai_cpt_mixed_fast() { |
| 65 |
bench_iai_cpt_overview(true, true); |
| 66 |
} |
| 67 |
|
| 68 |
fn bench_iai_cpt_mixed_small() { |
| 69 |
bench_iai_cpt_overview(false, true); |
| 70 |
} |
| 71 |
|
| 72 |
iai::main!( |
| 73 |
bench_iai_cpt_latin_fast, |
| 74 |
bench_iai_cpt_latin_small, |
| 75 |
bench_iai_cpt_mixed_fast, |
| 76 |
bench_iai_cpt_mixed_small, |
| 77 |
); |