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

Consider use of IndexMap::(swap_)remove and IndexSet::(swap_)remove #120456

Open
clubby789 opened this issue Jan 28, 2024 · 5 comments
Open

Consider use of IndexMap::(swap_)remove and IndexSet::(swap_)remove #120456

clubby789 opened this issue Jan 28, 2024 · 5 comments
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@clubby789
Copy link
Contributor

#120454 (comment)

indexmap 2.2.0 made the remove methods deprecated, as they forward directly to swap_remove. #120454 will update these callsites to use swap_remove, but we should consider if this is the correct behaviour. Alternatives are shift_remove (invalidating existing indexes) or using a different map.

@clubby789 clubby789 added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-discussion Category: Discussion or questions that doesn't represent real issues. labels Jan 28, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 28, 2024
@RalfJung
Copy link
Member

For the map that appears in the interpreter, this used to be an FxHashMap but someone made it an IndexMap... it is a map we need to remove from though. I don't think we care about iteration order as long as it is deterministic, so why do we care about swap_remove vs shift_remove?

@clubby789 clubby789 removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 28, 2024
@cuviper
Copy link
Member

cuviper commented Jan 29, 2024

If you only care about determinism, then either method will work, and swap is faster.

But some people were surprised by the change in order -- see: indexmap-rs/indexmap#241

@cuviper
Copy link
Member

cuviper commented Jan 29, 2024

Another idea: if there are cases that do care about order but don't want to pay the price for shift_remove, or they need unperturbed indices, we could consider adding a wrapper to rustc_data_structures that removes by replacing with tombstones. I'd be happy to prototype that if there's a use case for it.

@Mark-Simulacrum
Copy link
Member

If you only care about determinism, then either method will work, and swap is faster.

But some people were surprised by the change in order -- see: indexmap-rs/indexmap#241

I think shift has fewer requirements on determinism, right? Removal order doesn't need to be deterministic to preserve order amongst remaining elements in particular.

@cuviper
Copy link
Member

cuviper commented Jan 30, 2024

Yeah, I suppose that's true -- shift_remove can happen in any order and still get the same remaining order.
(Assuming you're not mixing those removals with the otherwise deterministic inserts.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants