Debian Codegraph @

README.md

raw symbols SCIP
1 [![CI](https://github.com/rusticstuff/simdutf8/actions/workflows/ci.yml/badge.svg)](https://github.com/rusticstuff/simdutf8/actions/workflows/ci.yml)
2 [![crates.io](https://img.shields.io/crates/v/simdutf8.svg)](https://crates.io/crates/simdutf8)
3 [![docs.rs](https://docs.rs/simdutf8/badge.svg)](https://docs.rs/simdutf8)
4
5 # simdutf8 – High-speed UTF-8 validation
6
7 Blazingly fast API-compatible UTF-8 validation for Rust using SIMD extensions, based on the implementation from
8 [simdjson](https://github.com/simdjson/simdjson). Originally ported to Rust by the developers of [simd-json.rs](https://simd-json.rs), but now heavily improved.
9
10 ## Status
11 This library has been thoroughly tested with sample data as well as fuzzing and there are no known bugs.
12
13 ## Features
14 * `basic` API for the fastest validation, optimized for valid UTF-8
15 * `compat` API as a fully compatible replacement for `std::str::from_utf8()`
16 * Supports AVX 2 and SSE 4.2 implementations on x86 and x86-64
17 * 🆕 ARM64 (aarch64) SIMD is supported since Rust 1.61
18 * 🆕 WASM (wasm32) SIMD is supported
19 * x86-64: Up to 23 times faster than the std library on valid non-ASCII, up to four times faster on ASCII
20 * aarch64: Up to eleven times faster than the std library on valid non-ASCII, up to four times faster on ASCII (Apple Silicon)
21 * Faster than the original simdjson implementation
22 * Selects the fastest implementation at runtime based on CPU support (on x86)
23 * Falls back to the excellent std implementation if SIMD extensions are not supported
24 * Written in pure Rust
25 * No dependencies
26 * No-std support
27
28 ## Quick start
29 Add the dependency to your Cargo.toml file:
30 ```toml
31 [dependencies]
32 simdutf8 = "0.1.5"
33 ```
34
35 Use `simdutf8::basic::from_utf8()` as a drop-in replacement for `std::str::from_utf8()`.
36
37 ```rust
38 use simdutf8::basic::from_utf8;
39
40 println!("{}", from_utf8(b"I \xE2\x9D\xA4\xEF\xB8\x8F UTF-8!").unwrap());
41 ```
42
43 If you need detailed information on validation failures, use `simdutf8::compat::from_utf8()`
44 instead.
45
46 ```rust
47 use simdutf8::compat::from_utf8;
48
49 let err = from_utf8(b"I \xE2\x9D\xA4\xEF\xB8 UTF-8!").unwrap_err();
50 assert_eq!(err.valid_up_to(), 5);
51 assert_eq!(err.error_len(), Some(2));
52 ```
53
54 ## APIs
55
56 ### Basic flavor
57 Use the `basic` API flavor for maximum speed. It is fastest on valid UTF-8, but only checks
58 for errors after processing the whole byte sequence and does not provide detailed information if the data
59 is not valid UTF-8. `simdutf8::basic::Utf8Error` is a zero-sized error struct.
60
61 ### Compat flavor
62 The `compat` flavor is fully API-compatible with `std::str::from_utf8()`. In particular, `simdutf8::compat::from_utf8()`
63 returns a `simdutf8::compat::Utf8Error`, which has `valid_up_to()` and `error_len()` methods. The first is useful for
64 verification of streamed data. The second is useful e.g. for replacing invalid byte sequences with a replacement character.
65
66 It also fails early: errors are checked on the fly as the string is processed and once
67 an invalid UTF-8 sequence is encountered, it returns without processing the rest of the data.
68 This comes at a slight performance penalty compared to the `basic` API even if the input is valid UTF-8.
69
70 ## Implementation selection
71
72 ### X86
73 The fastest implementation is selected at runtime using the `std::is_x86_feature_detected!` macro, unless the CPU
74 targeted by the compiler supports the fastest available implementation.
75 So if you compile with `RUSTFLAGS="-C target-cpu=native"` on a recent x86-64 machine, the AVX 2 implementation is selected at
76 compile-time and runtime selection is disabled.
77
78 For no-std support (compiled with `--no-default-features`) the implementation is always selected at compile time based on
79 the targeted CPU. Use `RUSTFLAGS="-C target-feature=+avx2"` for the AVX 2 implementation or `RUSTFLAGS="-C target-feature=+sse4.2"`
80 for the SSE 4.2 implementation.
81
82 ### ARM64
83 The SIMD implementation is used automatically since Rust 1.61.
84
85 ### WASM32
86 For wasm32 support, the implementation is selected at compile time based on the presence of the `simd128` target feature.
87 Use `RUSTFLAGS="-C target-feature=+simd128"` to enable the WASM SIMD implementation. WASM, at
88 the time of this writing, doesn't have a way to detect SIMD through WASM itself. Although this capability
89 is available in various WASM host environments (e.g., [wasm-feature-detect] in the web browser), there is no portable
90 way from within the library to detect this.
91
92 [wasm-feature-detect]: https://github.com/GoogleChromeLabs/wasm-feature-detect
93
94 #### Building/Targeting WASM
95 See [this document](./wasm32-development.md) for more details.
96
97 ### Access to low-level functionality
98
99 If you want to be able to call a SIMD implementation directly, use the `public_imp` feature flag. The validation implementations are then accessible in the `simdutf8::{basic, compat}::imp` hierarchy. Traits
100 facilitating streaming validation are available there as well.
101
102 ## Optimisation flags
103 Do not use [`opt-level = "z"`](https://doc.rust-lang.org/cargo/reference/profiles.html), which prevents inlining and makes
104 the code quite slow.
105
106 ## Minimum Supported Rust Version (MSRV)
107 This crate's minimum supported Rust version is 1.38.0.
108
109 ## Benchmarks
110 The benchmarks have been done with [criterion](https://bheisler.github.io/criterion.rs/book/index.html), the tables
111 are created with [critcmp](https://github.com/BurntSushi/critcmp). Source code and data are in the
112 [bench directory](https://github.com/rusticstuff/simdutf8/tree/main/bench).
113
114 The naming schema is id-charset/size. _0-empty_ is the empty byte slice, _x-error/66536_ is a 64KiB slice where the very
115 first character is invalid UTF-8. Library versions are simdutf8 v0.1.2 and simdjson v0.9.2. When comparing
116 with simdjson simdutf8 is compiled with `#inline(never)`.
117
118 Configurations:
119 * X86-64: PC with an AMD Ryzen 7 PRO 3700 CPU (Zen2) on Linux with Rust 1.52.0
120 * Aarch64: Macbook Air with an Apple M1 CPU (Apple Silicon) on macOS with Rust rustc 1.54.0-nightly (881c1ac40 2021-05-08).
121
122 ### simdutf8 basic vs std library on x86-64 (AMD Zen2)
123 ![image](https://user-images.githubusercontent.com/3736990/117568104-1c00f900-b0bf-11eb-938f-4c253d192480.png)
124 Simdutf8 is up to 23 times faster than the std library on valid non-ASCII, up to four times on pure ASCII.
125
126 ### simdutf8 basic vs std library on aarch64 (Apple Silicon)
127 ![image](https://user-images.githubusercontent.com/3736990/117568160-42bf2f80-b0bf-11eb-86a4-9aeee4cee87d.png)
128 Simdutf8 is up to to eleven times faster than the std library on valid non-ASCII, up to four times faster on
129 pure ASCII.
130
131 ### simdutf8 basic vs simdjson on x86-64
132 ![image](https://user-images.githubusercontent.com/3736990/117568231-80bc5380-b0bf-11eb-8e90-1dcc6d966ebd.png)
133 Simdutf8 is faster than simdjson on almost all inputs.
134
135 ### simdutf8 basic vs simdutf8 compat UTF-8 on x86-64
136 ![image](https://user-images.githubusercontent.com/3736990/117568270-af3a2e80-b0bf-11eb-8ec4-e5a0a4ad7210.png)
137 There is a small performance penalty to continuously checking the error status while processing data, but detecting
138 errors early provides a huge benefit for the _x-error/66536_ benchmark.
139
140 ## Technical details
141 For inputs shorter than 64 bytes validation is delegated to `core::str::from_utf8()` except for the direct-access
142 functions in `simdutf8::{basic, compat}::imp`.
143
144 The SIMD implementation is mostly similar to the one in simdjson except that it is has additional optimizations
145 for the pure ASCII case. Also it uses prefetch with AVX 2 on x86 which leads to slightly better performance with
146 some Intel CPUs on synthetic benchmarks.
147
148 For the compat API, we need to check the error status vector on each 64-byte block instead of just aggregating it. If an
149 error is found, the last bytes of the previous block are checked for a cross-block continuation and then
150 `std::str::from_utf8()` is run to find the exact location of the error.
151
152 Care is taken that all functions are properly inlined up to the public interface.
153
154 ## Thanks
155 * to the authors of simdjson for coming up with the high-performance SIMD implementation and in particular to Daniel Lemire
156 for his feedback. It was very helpful.
157 * to the authors of the simdjson Rust port who did most of the heavy lifting of porting the C++ code to Rust.
158
159
160 ## License
161 This code is dual-licensed under the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html) and the [MIT License](https://opensource.org/licenses/MIT).
162
163 It is based on code distributed with simd-json.rs, the Rust port of simdjson, which is dual-licensed under
164 the MIT license and Apache 2.0 license as well.
165
166 simdjson itself is distributed under the Apache License 2.0.
167
168 ## References
169 John Keiser, Daniel Lemire, [Validating UTF-8 In Less Than One Instruction Per Byte](https://arxiv.org/abs/2010.03090), Software: Practice and Experience 51 (5), 2021