Skip to content

std: Add performance warnings to HashMap::get_disjoint_mut #139307

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

xizheyin
Copy link
Contributor

@xizheyin xizheyin commented Apr 3, 2025

Closes #139296

The get_disjoint_mut in HashMap also performs a complexity O(n^2) check. So we need to be reminded of that as well.

https://github.com./rust-lang/hashbrown/blob/b5b0655a37e156f9798ac8dd7e970d4adba9bf90/src/raw/mod.rs#L1216-L1220

@rustbot
Copy link
Collaborator

rustbot commented Apr 3, 2025

r? @thomcc

rustbot has assigned @thomcc.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 3, 2025
@mattfbacon
Copy link
Contributor

To be honest I don't understand why, why not just sort the list of hashes and check neighbours (n log n)?

@xizheyin
Copy link
Contributor Author

xizheyin commented Apr 3, 2025

I'm not sure. I think what you said seems reasonable.We can wait for the members of libs to discuss it. Maybe we can change the implementation.

@mattfbacon
Copy link
Contributor

mattfbacon commented Apr 3, 2025

So, we can't do it with the hashes due to collisions, and T doesn't have PartialOrd.

However, I think it would be possible to do everything up to getting the raw pointer of each element to return, then use this technique there. The performance is worse in the case of an overlap, but I think that is irrelevant since it panics in that case.

But honestly it's probably not worth it, most use cases are probably 2 or 3 keys where the perf would probably even be worse. So I would go ahead with documenting it.

@zirconium-n
Copy link
Contributor

So, we can't do it with the hashes due to collisions, and T doesn't have PartialOrd.

However, I think it would be possible to do everything up to getting the raw pointer of each element to return, then use this technique there. The performance is worse in the case of an overlap, but I think that is irrelevant since it panics in that case.

But honestly it's probably not worth it, most use cases are probably 2 or 3 keys where the perf would probably even be worse. So I would go ahead with documenting it.

Actually std delegates to hashbrown which is indeed already comparing the pointers, so you can sort it. However just like what you said, it's not like we are really going to query 100 keys, and there's unsafe version of API for that anyway.

@xizheyin
Copy link
Contributor Author

r? libs

@rustbot rustbot assigned joboet and unassigned thomcc Apr 14, 2025
@xizheyin xizheyin requested a review from joboet April 20, 2025 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent warning on get_disjoint_mut
7 participants