about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src/sync/vec.rs
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2023-02-21 08:37:10 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2023-02-21 08:38:24 +0000
commitdecfb4d123651a0673db47542e18ca60d1c501ac (patch)
treecdfb83b31154c07755eaf21b97fa51ddfe01e64f /compiler/rustc_data_structures/src/sync/vec.rs
parenta04e78730e501de7147ba8c09372348529b00c17 (diff)
downloadrust-decfb4d123651a0673db47542e18ca60d1c501ac.tar.gz
rust-decfb4d123651a0673db47542e18ca60d1c501ac.zip
Use a lock-free datastructure for `source_span`
Diffstat (limited to 'compiler/rustc_data_structures/src/sync/vec.rs')
-rw-r--r--compiler/rustc_data_structures/src/sync/vec.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/sync/vec.rs b/compiler/rustc_data_structures/src/sync/vec.rs
new file mode 100644
index 00000000000..cbea4f05999
--- /dev/null
+++ b/compiler/rustc_data_structures/src/sync/vec.rs
@@ -0,0 +1,41 @@
+use std::marker::PhantomData;
+
+use rustc_index::vec::Idx;
+
+pub struct AppendOnlyVec<I: Idx, T: Copy> {
+    #[cfg(not(parallel_compiler))]
+    vec: elsa::vec::FrozenVec<T>,
+    #[cfg(parallel_compiler)]
+    vec: elsa::sync::LockFreeFrozenVec<T>,
+    _marker: PhantomData<fn(&I)>,
+}
+
+impl<I: Idx, T: Copy> AppendOnlyVec<I, T> {
+    pub fn new() -> Self {
+        Self {
+            #[cfg(not(parallel_compiler))]
+            vec: elsa::vec::FrozenVec::new(),
+            #[cfg(parallel_compiler)]
+            vec: elsa::sync::LockFreeFrozenVec::new(),
+            _marker: PhantomData,
+        }
+    }
+
+    pub fn push(&self, val: T) -> I {
+        #[cfg(not(parallel_compiler))]
+        let i = self.vec.len();
+        #[cfg(not(parallel_compiler))]
+        self.vec.push(val);
+        #[cfg(parallel_compiler)]
+        let i = self.vec.push(val);
+        I::new(i)
+    }
+
+    pub fn get(&self, i: I) -> Option<T> {
+        let i = i.index();
+        #[cfg(not(parallel_compiler))]
+        return self.vec.get_copy(i);
+        #[cfg(parallel_compiler)]
+        return self.vec.get(i);
+    }
+}