Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Fast match expression optimized for string comparison

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
NotificationsYou must be signed in to change notification settings

daac-tools/trie-match

Repository files navigation

Crates.ioDocumentationRustBuild StatusSlack

This macro speeds up Rust'smatch expression for comparing strings by using acompact double-array data structure.

Usage

Simply wrap the existingmatch expression with thetrie_match! {} macro asfollows:

use trie_match::trie_match;let x ="abd";let result =trie_match!{match x{"a" =>0,"abc" =>1,        pat @("abd" |"bcc") => pat.bytes()[0],"bc" =>3,        _ =>4,}};assert_eq!(result,3);

Why is it faster?

In a normalmatch expression, the string is compared for each pattern. It isequivalent to the following code:

if x =="a"{0}elseif x =="abc"{1}elseif x =="abd" || x =="bcc"{    x.bytes()[0]}elseif x =="bc"{3}else{4}

The above code requires that string comparisons be made from the beginning ofthe string each time. The time complexity of the above code isO(mn), wherem is the average pattern length, andn is the number of patterns.

In contrast, this macro builds the following trie structure to retrieve theindex of the matched arm:

Trie

Furthermore, this implementation uses the compact double-array data structureto achieve efficient state-to-state traversal, and the time complexity becomesO(m).

cfg attribute

Only when using Nightly Rust, this macro supports conditional compilation withthecfg attribute. To use this feature, enablefeatures = ["cfg_attribute"]in yourCargo.toml.

Example

trie_match!{match x{        #[cfg(feature ="foo")]"a" =>{ ..}        #[cfg(feature ="bar")]"b" =>{ ..}        _ =>{ ..}}}

Limitations

The followings are different from the normalmatch expression:

  • Only supports strings, byte strings, and u8 slices as patterns.
  • The wildcard is evaluated last. (The normalmatch expression does notmatch patterns after the wildcard.)
  • Guards are unavailable.

Sometimes the normalmatch expression is faster, depending on howoptimization is performed, so it is better to choose based on your speedexperiments.

Benchmark

Run the following command:

cargo bench

Experimental results are as follows [μs]:

  • AMD Ryzen 7 5700U with Radeon Graphics

    Bench nameNormal matchphf cratetrie-match crate
    100 words random1.942.021.09
    HTML elements random2.322.430.55
  • 12th Gen Intel(R) Core(TM) i7-1270P

    Bench nameNormal matchphf cratetrie-match crate
    100 words random1.131.290.61
    HTML elements random1.241.510.36

phf crate: Compile time static mapsusing perfect hash functions.

License

Licensed under either of

at your option.

Contribution

Seethe guidelines.

About

Fast match expression optimized for string comparison

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp