Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/unicode-ident/benches/xid.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / unicode-ident / benches / xid.rs` 126 lines ยท 4097 bytes Raw Blame History 1 // To run: `cargo criterion` 2 // 3 // This benchmarks each of the different libraries at several ratios of ASCII to 4 // non-ASCII content. There is one additional benchmark labeled "baseline" which 5 // just iterates over characters in a string, converting UTF-8 to 32-bit chars. 6 // 7 // Criterion will show a time in milliseconds. The non-baseline bench functions 8 // each make one million function calls (2 calls per character, 500K characters 9 // in the strings created by gen_string). The "time per call" listed in our 10 // readme is computed by subtracting this baseline from the other bench 11 // functions' time, then dividing by one million (ms -> ns). 12 13 #![allow( 14 clippy::incompatible_msrv, // https://github.com/rust-lang/rust-clippy/issues/12257 15 clippy::needless_pass_by_value, 16 )] 17 18 #[path = "../tests/fst/mod.rs"] 19 mod fst; 20 #[path = "../tests/roaring/mod.rs"] 21 mod roaring; 22 #[path = "../tests/trie/mod.rs"] 23 mod trie; 24 25 use criterion::{criterion_group, criterion_main, Criterion}; 26 use rand::distr::{Bernoulli, Distribution, Uniform}; 27 use rand::rngs::SmallRng; 28 use rand::SeedableRng; 29 use std::hint::black_box; 30 use std::time::Duration; 31 32 fn gen_string(p_nonascii: u32) -> String { 33 let mut rng = SmallRng::from_seed([b'!'; 32]); 34 let pick_nonascii = Bernoulli::from_ratio(p_nonascii, 100).unwrap(); 35 let ascii = Uniform::new_inclusive('\0', '\x7f').unwrap(); 36 let nonascii = Uniform::new_inclusive(0x80 as char, char::MAX).unwrap(); 37 38 let mut string = String::new(); 39 for _ in 0..500_000 { 40 let distribution = if pick_nonascii.sample(&mut rng) { 41 nonascii 42 } else { 43 ascii 44 }; 45 string.push(distribution.sample(&mut rng)); 46 } 47 48 string 49 } 50 51 fn bench(c: &mut Criterion, group_name: &str, string: String) { 52 let mut group = c.benchmark_group(group_name); 53 group.measurement_time(Duration::from_secs(10)); 54 group.bench_function("baseline", |b| { 55 b.iter(|| { 56 for ch in string.chars() { 57 black_box(ch); 58 } 59 }); 60 }); 61 group.bench_function("unicode-ident", |b| { 62 b.iter(|| { 63 for ch in string.chars() { 64 black_box(unicode_ident::is_xid_start(ch)); 65 black_box(unicode_ident::is_xid_continue(ch)); 66 } 67 }); 68 }); 69 group.bench_function("unicode-xid", |b| { 70 b.iter(|| { 71 for ch in string.chars() { 72 black_box(unicode_xid::UnicodeXID::is_xid_start(ch)); 73 black_box(unicode_xid::UnicodeXID::is_xid_continue(ch)); 74 } 75 }); 76 }); 77 group.bench_function("ucd-trie", |b| { 78 b.iter(|| { 79 for ch in string.chars() { 80 black_box(trie::XID_START.contains_char(ch)); 81 black_box(trie::XID_CONTINUE.contains_char(ch)); 82 } 83 }); 84 }); 85 group.bench_function("fst", |b| { 86 let xid_start_fst = fst::xid_start_fst(); 87 let xid_continue_fst = fst::xid_continue_fst(); 88 b.iter(|| { 89 for ch in string.chars() { 90 let ch_bytes = (ch as u32).to_be_bytes(); 91 black_box(xid_start_fst.contains(ch_bytes)); 92 black_box(xid_continue_fst.contains(ch_bytes)); 93 } 94 }); 95 }); 96 group.bench_function("roaring", |b| { 97 let xid_start_bitmap = roaring::xid_start_bitmap(); 98 let xid_continue_bitmap = roaring::xid_continue_bitmap(); 99 b.iter(|| { 100 for ch in string.chars() { 101 black_box(xid_start_bitmap.contains(ch as u32)); 102 black_box(xid_continue_bitmap.contains(ch as u32)); 103 } 104 }); 105 }); 106 group.finish(); 107 } 108 109 fn bench0(c: &mut Criterion) { 110 bench(c, "0%-nonascii", gen_string(0)); 111 } 112 113 fn bench1(c: &mut Criterion) { 114 bench(c, "1%-nonascii", gen_string(1)); 115 } 116 117 fn bench10(c: &mut Criterion) { 118 bench(c, "10%-nonascii", gen_string(10)); 119 } 120 121 fn bench100(c: &mut Criterion) { 122 bench(c, "100%-nonascii", gen_string(100)); 123 } 124 125 criterion_group!(benches, bench0, bench1, bench10, bench100); 126 criterion_main!(benches);