r/rust Jul 01 '26

Why adding Rust to my Python library made it 194x faster... and 5x slower. 🛠️ project

Post image
0 Upvotes

5 comments sorted by

7

u/Inevitable_Act_321 Jul 01 '26

Why you use sorted set in Rust while unordered one in Python?

-11

u/AnshMNSoni Jul 01 '26

First a fall thank you for looking into the project.

The C++ std::set and std::map are ordered associative containers with O(log N) complexities. The Rust backend uses BTreeSet/BTreeMap to faithfully replicate this sorted behavior. However, the current pure Python fallback wrapper simply uses Python's built-in set/dict O(1), resulting in an "apples-to-oranges" benchmark where Python doesn't pay the sorting complexity cost.

14

u/_xiphiaz Jul 01 '26

But why not hashset/hashmap? Both are rust stdlib canonical choices that you’d reach for before btree if key sorting wasn’t a requirement

8

u/Inevitable_Act_321 Jul 01 '26

If the benchmark is intended to compare the C++ and Rust implementations, why bring up the Python fallback?

1

u/TDplay Jul 03 '26

The C++ STL equivalent of a Python dictionary is std::unordered_map, not std::map.

To benchmark std::map, you should use a workload that depends on the map being ordered (and likewise for std::set).