| 1 | # bytecount |
| 2 | |
| 3 | Counting bytes really fast |
| 4 | |
| 5 | [](https://github.com/llogiq/bytecount/actions/workflows/ci.yml) |
| 6 | [](https://ci.appveyor.com/project/llogiq/bytecount) |
| 7 | [](https://crates.io/crates/bytecount) |
| 8 | [](#license) |
| 9 | |
| 10 | This uses the "hyperscreamingcount" algorithm by Joshua Landau to count bytes faster than anything else. |
| 11 | The [newlinebench](https://github.com/llogiq/newlinebench) repository has further benchmarks for old versions of this repository. |
| 12 | |
| 13 | To use bytecount in your crate, if you have [cargo-edit](https://github.com/killercup/cargo-edit), just type |
| 14 | `cargo add bytecount` in a terminal with the crate root as the current path. Otherwise you can manually edit your |
| 15 | `Cargo.toml` to add `bytecount = 0.6.9` to your `[dependencies]` section. |
| 16 | |
| 17 | In your crate root (`lib.rs` or `main.rs`, depending on if you are writing a |
| 18 | library or application), add `extern crate bytecount;`. Now you can simply use |
| 19 | `bytecount::count` as follows: |
| 20 | |
| 21 | ```Rust |
| 22 | extern crate bytecount; |
| 23 | |
| 24 | fn main() { |
| 25 | let mytext = "some potentially large text, perhaps read from disk?"; |
| 26 | let spaces = bytecount::count(mytext.as_bytes(), b' '); |
| 27 | .. |
| 28 | } |
| 29 | ``` |
| 30 | |
| 31 | bytecount supports two features to make use of modern CPU's features to speed up counting considerably. To allow your |
| 32 | users to use them, add the following to your `Cargo.toml`: |
| 33 | |
| 34 | ``` |
| 35 | [features] |
| 36 | runtime-dispatch-simd = ["bytecount/runtime-dispatch-simd"] |
| 37 | generic-simd = ["bytecount/generic-simd"] |
| 38 | ``` |
| 39 | |
| 40 | The first, `runtime-dispatch-simd`, enables detection of SIMD capabilities at runtime, which allows using the SSE2 and |
| 41 | AVX2 codepaths, but cannot be used with `no_std`. |
| 42 | |
| 43 | Your users can then compile with runtime dispatch using: |
| 44 | |
| 45 | ``` |
| 46 | cargo build --release --features runtime-dispatch-simd |
| 47 | ``` |
| 48 | |
| 49 | The second, `generic-simd`, uses [`std::simd`](https://doc.rust-lang.org/std/simd/index.html) and [`#![feature(portable_simd)]`](https://github.com/rust-lang/rust/issues/86656) to provide a fast |
| 50 | architecture-agnostic SIMD codepath, but requires running on nightly. |
| 51 | |
| 52 | Your users can compile with this codepath using: |
| 53 | |
| 54 | ``` |
| 55 | cargo build --release --features generic-simd |
| 56 | ``` |
| 57 | |
| 58 | Building for a more specific architecture will also improve performance. |
| 59 | You can do this with |
| 60 | |
| 61 | ``` |
| 62 | RUSTFLAGS="-C target-cpu=native" cargo build --release |
| 63 | ``` |
| 64 | |
| 65 | The scalar algorithm is explained in depth [here](https://llogiq.github.io/2016/09/27/count.html). |
| 66 | |
| 67 | **Note: Versions until 0.4.0 worked with Rust as of 1.20.0. Version 0.5.0 until 0.6.0 requires Rust 1.26 or later, |
| 68 | and at least 1.27.2 to use SIMD. Versions from 0.6.0 require Rust 1.32.0 or later.** |
| 69 | |
| 70 | ## License |
| 71 | |
| 72 | Licensed under either of at your discretion: |
| 73 | |
| 74 | - [Apache 2.0](LICENSE.Apache2) |
| 75 | - [MIT](LICENSE.MIT) |