Qiskit / rustworkx

A high performance Python graph library implemented in Rust.
https://www.rustworkx.org
Apache License 2.0
1.05k stars 146 forks source link

Improve performance of custom-iterator `__getitem__` #1096

Closed jakelishman closed 6 months ago

jakelishman commented 6 months ago

Flipping the order of Slice and Int in SliceOrInt so that Int comes first means that the FromPyObject derivation will then try Int first, which is the correct variant in like 99.9% of uses of the struct. This has the impact of improving int __getitem__ times in the custom iterators by about 3x (from 205ns to 61ns on my machine), which has knock-on effects for the implicit iterators Python defines for these classes.

This implements the performance improvement I mentioned in https://github.com/Qiskit/rustworkx/issues/1090#issuecomment-1954836685. I can also look into defining manual Python-space iterators (rather than using the implicit one based on __getitem__), if that's an interesting path - I haven't tried it yet, so I don't know if there's more performance to be had.

coveralls commented 6 months ago

Pull Request Test Coverage Report for Build 7978469504

Details


Totals Coverage Status
Change from base Build 7967150733: 0.0%
Covered Lines: 16740
Relevant Lines: 17335

💛 - Coveralls
mtreinish commented 6 months ago

@Mergifyio backport stable/0.14

mergify[bot] commented 6 months ago

backport stable/0.14

✅ Backports have been created

* [#1100 Improve performance of custom-iterator `__getitem__` (backport #1096)](https://github.com/Qiskit/rustworkx/pull/1100) has been created for branch `stable/0.14`
IvanIsCoding commented 6 months ago

Thanks for writing this! I had a feeling we’d need to profile the function but I guess the fix was easier than we thought