no-std bytecount

count occurrences of a given byte, or the number of UTF-8 code points, in a byte slice, fast

10 releases

0.3.2 Aug 3, 2018
0.3.1 Jan 30, 2018
0.2.0 Aug 25, 2017
0.1.7 Jul 19, 2017
0.1.2 Sep 27, 2016

#6 in Algorithms

Download history 2883/week @ 2018-09-14 3896/week @ 2018-09-21 3585/week @ 2018-09-28 4023/week @ 2018-10-05 3287/week @ 2018-10-12 3883/week @ 2018-10-19 7986/week @ 2018-10-26 9304/week @ 2018-11-02 9622/week @ 2018-11-09 8705/week @ 2018-11-16 9121/week @ 2018-11-23 9260/week @ 2018-11-30 9804/week @ 2018-12-07

19,283 downloads per month
Used in 182 crates (14 directly)

Apache-2.0/MIT

27KB
555 lines

bytecount

Counting bytes really fast

Build Status Windows build status Current Version License: Apache 2.0/MIT

This uses the "hyperscreamingcount" algorithm by Joshua Landau to count bytes faster than anything else. The newlinebench repository has further benchmarks for old versions of this repository.

To use bytecount in your crate, if you have cargo-edit, just type cargo add bytecount in a terminal with the crate root as the current path. Otherwise you can manually edit your Cargo.toml to add bytecount = 0.4.0 to your [dependencies] section.

In your crate root (lib.rs or main.rs, depending on if you are writing a library or application), add extern crate bytecount;. Now you can simply use bytecount::count as follows:

extern crate bytecount;

fn main() {
    let mytext = "some potentially large text, perhaps read from disk?";
    let spaces = bytecount::count(mytext.as_bytes(), b' ');
    ..
}

bytecount supports two features to make use of modern CPU's features to speed up counting considerably. To allow your users to use them, add the following to your Cargo.toml:

[features]
runtime-dispatch-simd = ["bytecount/runtime-dispatch-simd"]
generic-simd = ["bytecount/generic-simd"]

The first, runtime-dispatch-simd, enables detection of SIMD capabilities at runtime, which allows using the SSE2 and AVX2 codepaths, but cannot be used with no_std.

Your users can then compile with runtime dispacth using:

cargo build --release --features runtime-dispatch-simd

The second, generic-simd, uses packed_simd to provide a fast architecture-agnostic SIMD codepath, but requires running on nightly.

Your users can compile with this codepath using:

cargo build --release --features generic-simd

Building for a more specific architecture will also improve performance. You can do this with

RUSTFLAGS="-C target-cpu=native" cargo build --release

The scalar algorithm is explained in depth here.

License

Licensed under either of at your discretion:

Dependencies

~136KB