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

feat: faster VisibilityTracker::from_crate #404

Merged

Conversation

jalil-salame
Copy link
Contributor

@jalil-salame jalil-salame commented Aug 26, 2024

We reduce the number of times &Ids get hashed and get a nice perf boost thanks to that.

Numbers on my machine:

IndexedCrate/new(aws-sdk-ec2)
                        time:   [1.3597 s 1.3602 s 1.3607 s]
                        change: [-13.397% -12.945% -12.503%] (p = 0.00 < 0.05)
                        Performance has improved.

Split off from #402 (perf numbers come from the commits there)

Copy link
Owner

@obi1kenobi obi1kenobi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great optimization, nice find!

I'd love to make the new code a bit more maintenance-friendly before we merge it. As Rust evolves, we'll need to keep updating this code to cover new item kinds and it'll be important that we always understand how the code works and why. Otherwise, we'll write bugs and performance regressions, which will be frustrating to everyone.

let visible_parent_ids = compute_parent_ids_for_public_items(crate_)
.into_iter()
.map(|(key, values)| {
// Ensure a consistent order, since queries can observe this order directly.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to preserve this comment, since I believe it's still relevant and accurate. Adding to my suggestion in the other comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

Comment on lines 20 to 25
let iter = visible_parent_ids.iter_mut();

// Sort and deduplicate parent ids
iter.for_each(|(_id, parent_ids)| {
parent_ids.sort_unstable_by_key(|id| id.0.as_str());
parent_ids.dedup_by_key(|id| id.0.as_str());
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit of personal preference, if you don't mind indulging me: I find it easier to understand iterator .for_each() when the base iterator is constructed inline instead of constructed separately and assigned to a variable.

Suggested change
let iter = visible_parent_ids.iter_mut();
// Sort and deduplicate parent ids
iter.for_each(|(_id, parent_ids)| {
parent_ids.sort_unstable_by_key(|id| id.0.as_str());
parent_ids.dedup_by_key(|id| id.0.as_str());
});
// Sort and deduplicate parent ids.
// This ensures a consistent order, since queries can observe the order directly.
visible_parent_ids.iter_mut().for_each(|(_id, parent_ids)| {
parent_ids.sort_unstable_by_key(|id| id.0.as_str());
parent_ids.dedup_by_key(|id| id.0.as_str());
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

}

let next_parent_id = Some(&item.id);
// queue up removing this from the visited list
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to match the style of the surrounding comments.

Suggested change
// queue up removing this from the visited list
// Queue up removing this from the visited list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed (by removing the refactor)

Comment on lines 663 to 664
stack.reserve(m.items.len());
stack.extend(m.items.iter().filter_map(|id| {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting that this does .reserve() then .extend().

The iterator length of m.items.iter().filter_map() is known to be <= m.items.len(). Is this not something that the stack's .extend() will already account for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed (by removing the refactor)

Comment on lines 758 to 764
stack.reserve(trait_.items.len());
stack.extend(trait_.items.iter().filter_map(|id| {
crate_.index.get(id).map(|inner| Action::Process {
item: inner,
parent_id: next_parent_id,
})
}));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it'd be feasible to make some kind of refactor to deduplicate this reserve() + extend() + crate_.index.get(id).map(Action::Process {...}) pattern?

This method was pretty complex to start with, and now includes even more complexity with a fair bit of extra repetition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed (by removing the refactor)

item_parents.insert(parent_id);
) -> HashMap<&'a Id, Vec<&'a Id>> {
#[derive(Debug)]
enum Action<'a> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to leave a doc comment explaining the purpose of this type.

It took me a non-trivial amount of time to figure out why it's present and how it works. I'm not confident that in 6 months you or I will still remember the details, so as a favor to our future selves, I'd like to err on the side of explaining in too much detail rather than too little.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed (by removing the refactor)

Comment on lines 616 to 619
let mut stack = vec![Action::Process {
item,
parent_id: None,
}];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here, it would be good to leave an explanatory comment for what's happening: we're simulating recursion with a stack, the different actions mean A and B and correspond to equivalent operations X and Y in the recursive model. The stack-based implementation is faster because of reasons XYZ etc.

All this will be useful context for the next person who might try to update or optimize this code further.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed (by removing the refactor)

@jalil-salame
Copy link
Contributor Author

This is a great optimization, nice find!

I'd love to make the new code a bit more maintenance-friendly before we merge it. As Rust evolves, we'll need to keep updating this code to cover new item kinds and it'll be important that we always understand how the code works and why. Otherwise, we'll write bugs and performance regressions, which will be frustrating to everyone.

Lets push this PR back a bit. I'll split it into the manual dedup and the rewrite, because the more i look at the rewrite, the less i like it.

@obi1kenobi
Copy link
Owner

Sounds good to me 👍

@jalil-salame jalil-salame force-pushed the improve-visibility-tracker branch from 019f146 to 5859ef7 Compare August 27, 2024 18:46
We reduce the number of times `&Id`s get hashed and get a nice perf
boost thanks to that.

Numbers on my machine:

```console
$ cargo criterion --bench indexed_crate
IndexedCrate/new(aws-sdk-ec2)
                        time:   [1.5595 s 1.5713 s 1.5827 s]
                        change: [-8.3651% -7.5024% -6.6525%] (p = 0.00 < 0.05)
                        Performance has improved.
```
@jalil-salame jalil-salame force-pushed the improve-visibility-tracker branch from 5859ef7 to d658aba Compare August 27, 2024 19:25
@obi1kenobi obi1kenobi enabled auto-merge (squash) August 28, 2024 03:05
@obi1kenobi obi1kenobi merged commit c5bb145 into obi1kenobi:rustdoc-v33 Aug 28, 2024
4 checks passed
obi1kenobi added a commit that referenced this pull request Aug 28, 2024
* feat: faster VisibilityTracker::from_crate

We reduce the number of times `&Id`s get hashed and get a nice perf
boost thanks to that.

Numbers on my machine:

```console
$ cargo criterion --bench indexed_crate
IndexedCrate/new(aws-sdk-ec2)
                        time:   [1.5595 s 1.5713 s 1.5827 s]
                        change: [-8.3651% -7.5024% -6.6525%] (p = 0.00 < 0.05)
                        Performance has improved.
```

* Update src/visibility_tracker.rs

---------

Co-authored-by: Predrag Gruevski <[email protected]>
obi1kenobi added a commit that referenced this pull request Aug 28, 2024
* feat: faster VisibilityTracker::from_crate

We reduce the number of times `&Id`s get hashed and get a nice perf
boost thanks to that.

Numbers on my machine:

```console
$ cargo criterion --bench indexed_crate
IndexedCrate/new(aws-sdk-ec2)
                        time:   [1.5595 s 1.5713 s 1.5827 s]
                        change: [-8.3651% -7.5024% -6.6525%] (p = 0.00 < 0.05)
                        Performance has improved.
```

* Update src/visibility_tracker.rs

---------

Co-authored-by: Predrag Gruevski <[email protected]>
obi1kenobi added a commit that referenced this pull request Aug 28, 2024
* feat: faster VisibilityTracker::from_crate

We reduce the number of times `&Id`s get hashed and get a nice perf
boost thanks to that.

Numbers on my machine:

```console
$ cargo criterion --bench indexed_crate
IndexedCrate/new(aws-sdk-ec2)
                        time:   [1.5595 s 1.5713 s 1.5827 s]
                        change: [-8.3651% -7.5024% -6.6525%] (p = 0.00 < 0.05)
                        Performance has improved.
```

* Update src/visibility_tracker.rs

---------

Co-authored-by: Predrag Gruevski <[email protected]>
obi1kenobi added a commit that referenced this pull request Aug 28, 2024
* feat: faster VisibilityTracker::from_crate

We reduce the number of times `&Id`s get hashed and get a nice perf
boost thanks to that.

Numbers on my machine:

```console
$ cargo criterion --bench indexed_crate
IndexedCrate/new(aws-sdk-ec2)
                        time:   [1.5595 s 1.5713 s 1.5827 s]
                        change: [-8.3651% -7.5024% -6.6525%] (p = 0.00 < 0.05)
                        Performance has improved.
```

* Update src/visibility_tracker.rs

---------

Co-authored-by: Predrag Gruevski <[email protected]>
obi1kenobi added a commit that referenced this pull request Aug 28, 2024
* feat: faster VisibilityTracker::from_crate

We reduce the number of times `&Id`s get hashed and get a nice perf
boost thanks to that.

Numbers on my machine:

```console
$ cargo criterion --bench indexed_crate
IndexedCrate/new(aws-sdk-ec2)
                        time:   [1.5595 s 1.5713 s 1.5827 s]
                        change: [-8.3651% -7.5024% -6.6525%] (p = 0.00 < 0.05)
                        Performance has improved.
```

* Update src/visibility_tracker.rs

---------

Co-authored-by: Jalil David Salamé Messina <[email protected]>
obi1kenobi added a commit that referenced this pull request Aug 28, 2024
* feat: faster VisibilityTracker::from_crate

We reduce the number of times `&Id`s get hashed and get a nice perf
boost thanks to that.

Numbers on my machine:

```console
$ cargo criterion --bench indexed_crate
IndexedCrate/new(aws-sdk-ec2)
                        time:   [1.5595 s 1.5713 s 1.5827 s]
                        change: [-8.3651% -7.5024% -6.6525%] (p = 0.00 < 0.05)
                        Performance has improved.
```

* Update src/visibility_tracker.rs

---------

Co-authored-by: Jalil David Salamé Messina <[email protected]>
obi1kenobi added a commit that referenced this pull request Aug 28, 2024
* feat: faster VisibilityTracker::from_crate

We reduce the number of times `&Id`s get hashed and get a nice perf
boost thanks to that.

Numbers on my machine:

```console
$ cargo criterion --bench indexed_crate
IndexedCrate/new(aws-sdk-ec2)
                        time:   [1.5595 s 1.5713 s 1.5827 s]
                        change: [-8.3651% -7.5024% -6.6525%] (p = 0.00 < 0.05)
                        Performance has improved.
```

* Update src/visibility_tracker.rs

---------

Co-authored-by: Jalil David Salamé Messina <[email protected]>
obi1kenobi added a commit that referenced this pull request Aug 28, 2024
* feat: faster VisibilityTracker::from_crate

We reduce the number of times `&Id`s get hashed and get a nice perf
boost thanks to that.

Numbers on my machine:

```console
$ cargo criterion --bench indexed_crate
IndexedCrate/new(aws-sdk-ec2)
                        time:   [1.5595 s 1.5713 s 1.5827 s]
                        change: [-8.3651% -7.5024% -6.6525%] (p = 0.00 < 0.05)
                        Performance has improved.
```

* Update src/visibility_tracker.rs

---------

Co-authored-by: Jalil David Salamé Messina <[email protected]>
@jalil-salame jalil-salame deleted the improve-visibility-tracker branch August 28, 2024 05:49
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.

2 participants