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/README.md # 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 / README.md` 274 lines · 12365 bytes Raw Blame History Rendered 1 Unicode ident 2 ============= 3 4 [github](https://github.com/dtolnay/unicode-ident) 5 [crates.io](https://crates.io/crates/unicode-ident) 6 [docs.rs](https://docs.rs/unicode-ident) 7 [build status](https://github.com/dtolnay/unicode-ident/actions?query=branch%3Amaster) 8 9 Implementation of [Unicode Standard Annex #31][tr31] for determining which 10 `char` values are valid in programming language identifiers. 11 12 [tr31]: https://www.unicode.org/reports/tr31/ 13 14 This crate is a better optimized implementation of the older `unicode-xid` 15 crate. This crate uses less static storage, and is able to classify both ASCII 16 and non-ASCII codepoints with better performance, 6× faster than 17 `unicode-xid`. 18 19
20 21 ## Comparison of performance 22 23 The following table shows a comparison between five Unicode identifier 24 implementations. 25 26 - `unicode-ident` is this crate; 27 - [`unicode-xid`] is a widely used crate run by the "unicode-rs" org; 28 - `ucd-trie` and `fst` are two data structures supported by the [`ucd-generate`] tool; 29 - [`roaring`] is a Rust implementation of Roaring bitmap. 30 31 The *static storage* column shows the total size of `static` tables that the 32 crate bakes into your binary, measured in 1000s of bytes. 33 34 The remaining columns show the **cost per call** to evaluate whether a single 35 `char` has the XID\_Start or XID\_Continue Unicode property, comparing across 36 different ratios of ASCII to non-ASCII codepoints in the input data. 37 38 [`unicode-xid`]: https://github.com/unicode-rs/unicode-xid 39 [`ucd-generate`]: https://github.com/BurntSushi/ucd-generate 40 [`roaring`]: https://github.com/RoaringBitmap/roaring-rs 41 42 | | static storage | 0% nonascii | 1% | 10% | 100% nonascii | 43 |---|---|---|---|---|---| 44 | **`unicode-ident`** | 10.3 K | 0.36 ns | 0.37 ns | 0.37 ns | 0.43 ns | 45 | **`unicode-xid`** | 12.2 K | 1.63 ns | 1.70 ns | 1.82 ns | 4.56 ns | 46 | **`ucd-trie`** | 10.8 K | 1.01 ns | 0.73 ns | 0.97 ns | 1.09 ns | 47 | **`fst`** | 149 K | 22.0 ns | 21.9 ns | 20.9 ns | 10.5 ns | 48 | **`roaring`** | 66.1 K | 1.91 ns | 1.90 ns | 1.94 ns | 2.67 ns | 49 50 Source code for the benchmark is provided in the *bench* directory of this repo 51 and may be repeated by running `cargo criterion`. 52 53
54 55 ## Comparison of data structures 56 57 #### unicode-xid 58 59 They use a sorted array of character ranges, and do a binary search to look up 60 whether a given character lands inside one of those ranges. 61 62 ```rust 63 static XID_Continue_table: [(char, char); 763] = [ 64 ('\u{30}', '\u{39}'), // 0-9 65 ('\u{41}', '\u{5a}'), // A-Z 66 … 67 ('\u{e0100}', '\u{e01ef}'), 68 ]; 69 ``` 70 71 The static storage used by this data structure scales with the number of 72 contiguous ranges of identifier codepoints in Unicode. Every table entry 73 consumes 8 bytes, because it consists of a pair of 32-bit `char` values. 74 75 In some ranges of the Unicode codepoint space, this is quite a sparse 76 representation – there are some ranges where tens of thousands of adjacent 77 codepoints are all valid identifier characters. In other places, the 78 representation is quite inefficient. A characater like `µ` (U+00B5) which is 79 surrounded by non-identifier codepoints consumes 64 bits in the table, while it 80 would be just 1 bit in a dense bitmap. 81 82 On a system with 64-byte cache lines, binary searching the table touches 7 cache 83 lines on average. Each cache line fits only 8 table entries. Additionally, the 84 branching performed during the binary search is probably mostly unpredictable to 85 the branch predictor. 86 87 Overall, the crate ends up being about 6× slower on non-ASCII input 88 compared to the fastest crate. 89 90 A potential improvement would be to pack the table entries more compactly. 91 Rust's `char` type is a 21-bit integer padded to 32 bits, which means every 92 table entry is holding 22 bits of wasted space, adding up to 3.9 K. They could 93 instead fit every table entry into 6 bytes, leaving out some of the padding, for 94 a 25% improvement in space used. With some cleverness it may be possible to fit 95 in 5 bytes or even 4 bytes by storing a low char and an extent, instead of low 96 char and high char. I don't expect that performance would improve much but this 97 could be the most efficient for space across all the libraries, needing only 98 about 7 K to store. 99 100 #### ucd-trie 101 102 Their data structure is a compressed trie set specifically tailored for Unicode 103 codepoints. The design is credited to Raph Levien in [rust-lang/rust#33098]. 104 105 [rust-lang/rust#33098]: https://github.com/rust-lang/rust/pull/33098 106 107 ```rust 108 pub struct TrieSet { 109 tree1_level1: &'static [u64; 32], 110 tree2_level1: &'static [u8; 992], 111 tree2_level2: &'static [u64], 112 tree3_level1: &'static [u8; 256], 113 tree3_level2: &'static [u8], 114 tree3_level3: &'static [u64], 115 } 116 ``` 117 118 It represents codepoint sets using a trie to achieve prefix compression. The 119 final states of the trie are embedded in leaves or "chunks", where each chunk is 120 a 64-bit integer. Each bit position of the integer corresponds to whether a 121 particular codepoint is in the set or not. These chunks are not just a compact 122 representation of the final states of the trie, but are also a form of suffix 123 compression. In particular, if multiple ranges of 64 contiguous codepoints have 124 the same Unicode properties, then they all map to the same chunk in the final 125 level of the trie. 126 127 Being tailored for Unicode codepoints, this trie is partitioned into three 128 disjoint sets: tree1, tree2, tree3. The first set corresponds to codepoints \[0, 129 0x800), the second \[0x800, 0x10000) and the third \[0x10000, 0x110000). These 130 partitions conveniently correspond to the space of 1 or 2 byte UTF-8 encoded 131 codepoints, 3 byte UTF-8 encoded codepoints and 4 byte UTF-8 encoded codepoints, 132 respectively. 133 134 Lookups in this data structure are significantly more efficient than binary 135 search. A lookup touches either 1, 2, or 3 cache lines based on which of the 136 trie partitions is being accessed. 137 138 One possible performance improvement would be for this crate to expose a way to 139 query based on a UTF-8 encoded string, returning the Unicode property 140 corresponding to the first character in the string. Without such an API, the 141 caller is required to tokenize their UTF-8 encoded input data into `char`, hand 142 the `char` into `ucd-trie`, only for `ucd-trie` to undo that work by converting 143 back into the variable-length representation for trie traversal. 144 145 #### fst 146 147 Uses a [finite state transducer][fst]. This representation is built into 148 [ucd-generate] but I am not aware of any advantage over the `ucd-trie` 149 representation. In particular `ucd-trie` is optimized for storing Unicode 150 properties while `fst` is not. 151 152 [fst]: https://github.com/BurntSushi/fst 153 [ucd-generate]: https://github.com/BurntSushi/ucd-generate 154 155 As far as I can tell, the main thing that causes `fst` to have large size and 156 slow lookups for this use case relative to `ucd-trie` is that it does not 157 specialize for the fact that only 21 of the 32 bits in a `char` are meaningful. 158 There are some dense arrays in the structure with large ranges that could never 159 possibly be used. 160 161 #### roaring 162 163 This crate is a pure-Rust implementation of [Roaring Bitmap], a data structure 164 designed for storing sets of 32-bit unsigned integers. 165 166 [Roaring Bitmap]: https://roaringbitmap.org/about/ 167 168 Roaring bitmaps are compressed bitmaps which tend to outperform conventional 169 compressed bitmaps such as WAH, EWAH or Concise. In some instances, they can be 170 hundreds of times faster and they often offer significantly better compression. 171 172 In this use case the performance was reasonably competitive but still 173 substantially slower than the Unicode-optimized crates. Meanwhile the 174 compression was significantly worse, requiring 6× as much storage for the 175 data structure. 176 177 I also benchmarked the [`croaring`] crate which is an FFI wrapper around the C 178 reference implementation of Roaring Bitmap. This crate was consistently about 179 15% slower than pure-Rust `roaring`, which could just be FFI overhead. I did not 180 investigate further. 181 182 [`croaring`]: https://crates.io/crates/croaring 183 184 #### unicode-ident 185 186 This crate is most similar to the `ucd-trie` library, in that it's based on 187 bitmaps stored in the leafs of a trie representation, achieving both prefix 188 compression and suffix compression. 189 190 The key differences are: 191 192 - Uses a single 2-level trie, rather than 3 disjoint partitions of different 193 depth each. 194 - Uses significantly larger chunks: 512 bits rather than 64 bits. 195 - Compresses the XID\_Start and XID\_Continue properties together 196 simultaneously, rather than duplicating identical trie leaf chunks across the 197 two. 198 199 The following diagram show the XID\_Start and XID\_Continue Unicode boolean 200 properties in uncompressed form, in row-major order: 201 202 203 204 205 206 207 208
XID_StartXID_Continue
XID_Start bitmapXID_Continue bitmap
209 210 Uncompressed, these would take 140 K to store, which is beyond what would be 211 reasonable. However, as you can see there is a large degree of similarity 212 between the two bitmaps and across the rows, which lends well to compression. 213 214 This crate stores one 512-bit "row" of the above bitmaps in the leaf level of a 215 trie, and a single additional level to index into the leafs. There are 134 216 unique 512-bit chunks across the two bitmaps. 217 218 The chunk size of 512 bits is selected as the size that minimizes the total size 219 of the data structure. A smaller chunk, like 256 or 128 bits, would achieve 220 better deduplication but require a larger index. A larger chunk would increase 221 redundancy in the leaf bitmaps. 512 bit chunks are the optimum for total size of 222 the index plus leaf bitmaps. 223 224 The chunk data is compressed using the Kuhn–Munkres algorithm for bipartite 225 matching to eliminate redundancies between the second half of any chunk and the 226 first half of any other chunk. This achieves an additional 9% compression of the 227 leaf level, leaving 122 chunks that can be indexed at the half-chunk level using 228 an 8-bit index. Note that this is not the same as using chunks which are half 229 the size, because it does not necessitate raising the size of the trie's first 230 level. 231 232 In contrast to binary search or the `ucd-trie` crate, performing lookups in this 233 data structure is straight-line code with no need for branching. 234 235 ```asm 236 is_xid_start: 237 mov eax, edi 238 shr eax, 9 239 cmp edi, 261632 240 mov ecx, offset unicode_ident::tables::TRIE_START+17 241 lea rax, [rax + unicode_ident::tables::TRIE_START] 242 cmovb rcx, rax 243 movzx eax, byte ptr [rcx] 244 mov ecx, 1539 245 bextr ecx, edi, ecx 246 and edi, 7 247 shl eax, 5 248 movzx eax, byte ptr [rax + rcx + unicode_ident::tables::LEAF] 249 bt eax, edi 250 setb al 251 ret 252 ``` 253 254
255 256 ## License 257 258 Use of the Unicode Character Database, as this crate does, is governed by the Unicode license. 260 261 All intellectual property within this crate that is **not generated** using the 262 Unicode Character Database as input is licensed under either of Apache License, Version 2.0 or MIT license at your option. 265 266 The **generated** files incorporate tabular data derived from the Unicode 267 Character Database, together with intellectual property from the original source 268 code content of the crate. One must comply with the terms of both the Unicode 269 License Agreement and either of the Apache license or MIT license when those 270 generated files are involved. 271 272 Unless you explicitly state otherwise, any contribution intentionally submitted 273 for inclusion in this crate by you, as defined in the Apache-2.0 license, shall 274 be licensed as just described, without any additional terms or conditions.