Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Owen-CH-Leung
Copy link
Contributor

See #4787

This PR optimizes nth and nth_back for BoundListIterator, and added unittest & benchmarks for these 2 APIs. Here are the benchmark of the optimized nth and nth_back:

With Optimization

list_nth                time:   [293.43 ns 294.83 ns 296.46 ns]
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high severe

list_nth_back           time:   [303.82 ns 307.94 ns 313.12 ns]
Found 9 outliers among 100 measurements (9.00%)

The default nth and nth_back implementation

list_nth                time:   [2.9155 µs 2.9410 µs 2.9742 µs]
                        change: [+903.75% +913.27% +921.36%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 5 outliers among 100 measurements (5.00%)
  2 (2.00%) high mild
  3 (3.00%) high severe

list_nth_back           time:   [3.6843 µs 3.6950 µs 3.7053 µs]
                        change: [+1106.5% +1121.1% +1133.2%] (p = 0.00 < 0.05)
                        Performance has regressed.

@davidhewitt
Copy link
Member

Awesome, thanks! Will seek to review soon. I guess we can do the same thing for tuple iteration?

@Owen-CH-Leung Owen-CH-Leung marked this pull request as ready for review December 21, 2024 15:47
@Owen-CH-Leung
Copy link
Contributor Author

Owen-CH-Leung commented Dec 21, 2024

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) };
Copy link
Member

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...

Copy link
Contributor Author

@Owen-CH-Leung Owen-CH-Leung Dec 29, 2024

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.

Copy link
Contributor Author

@Owen-CH-Leung Owen-CH-Leung Dec 29, 2024

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

@ngoldbaum
Copy link
Contributor

Thanks for working on this! Hopefully my PR should be merged in the next few days and then you can rebase this.

@ngoldbaum
Copy link
Contributor

It might also make sense to implement advance_by on nightly. nth is implemented in terms of advance_by, so this improvement would fall out "for free" on nightly if you implement that function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants