{"id":21961751,"url":"https://github.com/daac-tools/trie-match","last_synced_at":"2025-10-13T15:04:42.860Z","repository":{"id":194226976,"uuid":"689386904","full_name":"daac-tools/trie-match","owner":"daac-tools","description":"Fast match expression optimized for string comparison","archived":false,"fork":false,"pushed_at":"2024-01-29T04:00:16.000Z","size":55,"stargazers_count":38,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-10-13T15:03:16.971Z","etag":null,"topics":["double-array","no-std","performance","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/daac-tools.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-09-09T16:36:09.000Z","updated_at":"2024-11-13T17:31:36.000Z","dependencies_parsed_at":"2024-01-29T05:21:50.441Z","dependency_job_id":"2ad73bd8-34f9-4dba-acf5-95cbaaf80600","html_url":"https://github.com/daac-tools/trie-match","commit_stats":null,"previous_names":["daac-tools/trie-match"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/daac-tools/trie-match","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daac-tools%2Ftrie-match","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daac-tools%2Ftrie-match/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daac-tools%2Ftrie-match/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daac-tools%2Ftrie-match/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daac-tools","download_url":"https://codeload.github.com/daac-tools/trie-match/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daac-tools%2Ftrie-match/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279015893,"owners_count":26085777,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["double-array","no-std","performance","rust"],"created_at":"2024-11-29T10:17:50.190Z","updated_at":"2025-10-13T15:04:42.843Z","avatar_url":"https://github.com/daac-tools.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `trie_match! {}`\n\n[![Crates.io](https://img.shields.io/crates/v/trie-match)](https://crates.io/crates/trie-match)\n[![Documentation](https://docs.rs/trie-match/badge.svg)](https://docs.rs/trie-match)\n[![Rust](https://img.shields.io/badge/rust-1.70%2B-blue.svg?maxAge=3600)](https://github.com/daac-tools/trie-match)\n[![Build Status](https://github.com/daac-tools/trie-match/actions/workflows/rust.yml/badge.svg)](https://github.com/daac-tools/trie-match/actions)\n[![Slack](https://img.shields.io/badge/join-chat-brightgreen?logo=slack)](https://join.slack.com/t/daac-tools/shared_invite/zt-1pwwqbcz4-KxL95Nam9VinpPlzUpEGyA)\n\nThis macro speeds up Rust's `match` expression for comparing strings by using a\ncompact double-array data structure.\n\n## Usage\n\nSimply wrap the existing `match` expression with the `trie_match! {}` macro as\nfollows:\n\n```rust\nuse trie_match::trie_match;\n\nlet x = \"abd\";\n\nlet result = trie_match! {\n    match x {\n        \"a\" =\u003e 0,\n        \"abc\" =\u003e 1,\n        pat @ (\"abd\" | \"bcc\") =\u003e pat.bytes()[0],\n        \"bc\" =\u003e 3,\n        _ =\u003e 4,\n    }\n};\n\nassert_eq!(result, 3);\n```\n\n## Why is it faster?\n\nIn a normal `match` expression, the string is compared for each pattern. It is\nequivalent to the following code:\n\n```rust\nif x == \"a\" {\n    0\n} else if x == \"abc\" {\n    1\n} else if x == \"abd\" || x == \"bcc\" {\n    x.bytes()[0]\n} else if x == \"bc\" {\n    3\n} else {\n    4\n}\n```\n\nThe above code requires that string comparisons be made from the beginning of\nthe string each time. The time complexity of the above code is *O(mn)*, where\n*m* is the average pattern length, and *n* is the number of patterns.\n\nIn contrast, this macro builds the following trie structure to retrieve the\nindex of the matched arm:\n\n![Trie](figures/graph.svg)\n\nFurthermore, this implementation uses the compact double-array data structure\nto achieve efficient state-to-state traversal, and the time complexity becomes\n*O(m)*.\n\n## `cfg` attribute\n\nOnly when using Nightly Rust, this macro supports conditional compilation with\nthe `cfg` attribute. To use this feature, enable `features = [\"cfg_attribute\"]`\nin your `Cargo.toml`.\n\n### Example\n\n```rust\ntrie_match! {\n    match x {\n        #[cfg(feature = \"foo\")]\n        \"a\" =\u003e { .. }\n        #[cfg(feature = \"bar\")]\n        \"b\" =\u003e { .. }\n        _ =\u003e { .. }\n    }\n}\n```\n\n## Limitations\n\nThe followings are different from the normal `match` expression:\n\n* Only supports strings, byte strings, and u8 slices as patterns.\n* The wildcard is evaluated last. (The normal `match` expression does not\n  match patterns after the wildcard.)\n* Guards are unavailable.\n\nSometimes the normal `match` expression is faster, depending on how\noptimization is performed, so it is better to choose based on your speed\nexperiments.\n\n## Benchmark\n\nRun the following command:\n\n```\ncargo bench\n```\n\nExperimental results are as follows [μs]:\n\n* AMD Ryzen 7 5700U with Radeon Graphics\n\n  | Bench name           | Normal match | *phf* crate | *trie-match* crate |\n  |----------------------|--------------|-------------|--------------------|\n  | 100 words random     |         1.94 |        2.02 |           **1.09** |\n  | HTML elements random |         2.32 |        2.43 |           **0.55** |\n\n* 12th Gen Intel(R) Core(TM) i7-1270P\n\n  | Bench name           | Normal match | *phf* crate | *trie-match* crate |\n  |----------------------|--------------|-------------|--------------------|\n  | 100 words random     |         1.13 |        1.29 |           **0.61** |\n  | HTML elements random |         1.24 |        1.51 |           **0.36** |\n\n[phf crate](https://github.com/rust-phf/rust-phf): Compile time static maps\nusing perfect hash functions.\n\n## License\n\nLicensed under either of\n\n * Apache License, Version 2.0\n   ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)\n * MIT license\n   ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)\n\nat your option.\n\n## Contribution\n\nSee [the guidelines](./CONTRIBUTING.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaac-tools%2Ftrie-match","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaac-tools%2Ftrie-match","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaac-tools%2Ftrie-match/lists"}