about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2025-04-11 21:20:59 +0200
committerGitHub <noreply@github.com>2025-04-11 21:20:59 +0200
commitbc05aaeeaac650f5b7f7cfdb609f6dc2175ee7f5 (patch)
treeabd49aa8c58deb57adbc875d621e575bab4043d9 /compiler/rustc_data_structures/src
parenta6608294a93cdf57c2e4c7bc6239ddeb59642473 (diff)
parent98d51fb44f4944b8bdad93a529d0625b9998bc93 (diff)
downloadrust-bc05aaeeaac650f5b7f7cfdb609f6dc2175ee7f5.tar.gz
rust-bc05aaeeaac650f5b7f7cfdb609f6dc2175ee7f5.zip
Rollup merge of #139584 - oli-obk:horrible-experiment-1, r=petrochenkov
Avoid a reverse map that is only used in diagnostics paths

r? `@petrochenkov`

iterating a map until a value matches and returning the key is bad obviously, but it happens very rarely and only on diagnostics paths. It would also be a lot cheaper with https://github.com/rust-lang/rust/pull/138995. Which is actually why I'm trying this out, that PR adds a new entry in `create_def`, which makes `create_def` show up in cachegrind. So I'm trying out if removing adding an entry in `create_def` is a perf improvement
Diffstat (limited to 'compiler/rustc_data_structures/src')
-rw-r--r--compiler/rustc_data_structures/src/unord.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/unord.rs b/compiler/rustc_data_structures/src/unord.rs
index baa66cd7c85..3d44fb1fd48 100644
--- a/compiler/rustc_data_structures/src/unord.rs
+++ b/compiler/rustc_data_structures/src/unord.rs
@@ -109,6 +109,16 @@ impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
     pub fn collect<C: From<UnordItems<T, I>>>(self) -> C {
         self.into()
     }
+
+    /// If the iterator has only one element, returns it, otherwise returns `None`.
+    #[track_caller]
+    pub fn get_only(mut self) -> Option<T> {
+        let item = self.0.next();
+        if self.0.next().is_some() {
+            return None;
+        }
+        item
+    }
 }
 
 impl<T> UnordItems<T, std::iter::Empty<T>> {