-
Notifications
You must be signed in to change notification settings - Fork 783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize nth and nth_back for BoundListIterator #4810
base: main
Are you sure you want to change the base?
Optimize nth and nth_back for BoundListIterator #4810
Conversation
Awesome, thanks! Will seek to review soon. I guess we can do the same thing for tuple iteration? |
Yup - I'll file a PR to optimise tuple iterator after the holiday =) |
let length = self.length.min(self.list.len()); | ||
let target_index = self.index + n; | ||
if self.index + n < length { | ||
let item = unsafe { self.get_item(target_index) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder, is there a time-of-check to time-of-use bug here on the length? Not one for this PR, but a follow up I will try not to forget...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup I think that's a potential TOCTOU bug here. This particular implementation assumes the user ensures proper synchronization if they intend to use the iterator in a multi-threaded or mutable environment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the current implementation of next
also has a potential TOCTOU bug:
https://github.com/PyO3/pyo3/blob/main/src/types/list.rs#L495-L498
Thanks for working on this! Hopefully my PR should be merged in the next few days and then you can rebase this. |
It might also make sense to implement advance_by on nightly. |
See #4787
This PR optimizes
nth
andnth_back
forBoundListIterator
, and added unittest & benchmarks for these 2 APIs. Here are the benchmark of the optimizednth
andnth_back
:With Optimization
The default
nth
andnth_back
implementation