laysakura / trie-rs

Memory efficient trie (prefix tree) library based on LOUDS
https://crates.io/crates/trie-rs
Apache License 2.0
90 stars 10 forks source link

Add common_prefix_match #15

Closed DavidVentura closed 4 months ago

DavidVentura commented 3 years ago

Hey! Running this code instead of common_prefix_search.is_empty() is ~18% faster for my use case (which is matching a trie with \~3500 items, of 5\~40 chars each

Not sure if it's worth adding to the crate though..

Running the benchmark on my pc,

Benchmarking [fab7d8c] Trie::common_prefix_search() 100 times: Analyzing
[fab7d8c] Trie::common_prefix_search() 100 times
                        time:   [3.1533 ms 3.1587 ms 3.1684 ms]

[fab7d8c] Trie::common_prefix_match() 100 times
                        time:   [732.60 us 734.58 us 736.95 us]
shanecelis commented 4 months ago

Hi, thank you for your pull request. I had a similar need and added is_prefix() which I now regret because it can be achieved with the iterator-based searches available in v0.3.0 of trie-rs. I added your benchmark here. Basically, we can now just check to see if the iterator is empty or not.

    assert!(trie.common_prefix_search::<Vec<u8>, _>("すしをにぎる").next().is_some());

Here are the results using the above code. They are thankfully comparable to yours.

Benchmarking [1d4d686] Trie::common_prefix_match() 100 times
Benchmarking [1d4d686] Trie::common_prefix_match() 100 times: Warming up for 1.0000 s
Benchmarking [1d4d686] Trie::common_prefix_match() 100 times: Collecting 10 samples in estimated 5.0414 s (5995 iterations)
Benchmarking [1d4d686] Trie::common_prefix_match() 100 times: Analyzing
[1d4d686] Trie::common_prefix_match() 100 times
                        time:   [823.13 us 828.41 us 837.42 us]

I hope that's satisfactory for you.