Skip to content

Commit 4699632

Browse files
committed
Remove a lock in favor of an AppendOnlyVec
1 parent daee746 commit 4699632

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

compiler/rustc_data_structures/src/sync/vec.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::marker::PhantomData;
22

33
use rustc_index::vec::Idx;
44

5+
#[derive(Default)]
56
pub struct AppendOnlyIndexVec<I: Idx, T: Copy> {
67
#[cfg(not(parallel_compiler))]
78
vec: elsa::vec::FrozenVec<T>,
@@ -40,6 +41,7 @@ impl<I: Idx, T: Copy> AppendOnlyIndexVec<I, T> {
4041
}
4142
}
4243

44+
#[derive(Default)]
4345
pub struct AppendOnlyVec<T: Copy> {
4446
#[cfg(not(parallel_compiler))]
4547
vec: elsa::vec::FrozenVec<T>,
@@ -57,11 +59,14 @@ impl<T: Copy> AppendOnlyVec<T> {
5759
}
5860
}
5961

60-
pub fn push(&self, val: T) {
62+
pub fn push(&self, val: T) -> usize {
63+
#[cfg(not(parallel_compiler))]
64+
let i = self.vec.len();
6165
#[cfg(not(parallel_compiler))]
6266
self.vec.push(val);
6367
#[cfg(parallel_compiler)]
64-
self.vec.push(val)
68+
let i = self.vec.push(val);
69+
i
6570
}
6671

6772
pub fn get(&self, i: usize) -> Option<T> {

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,8 +1712,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
17121712
let stability = tcx.lookup_stability(CRATE_DEF_ID);
17131713
let macros =
17141714
self.lazy_array(tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index));
1715-
let spans = self.tcx.sess.parse_sess.proc_macro_quoted_spans();
1716-
for (i, span) in spans.into_iter().enumerate() {
1715+
for (i, span) in self.tcx.sess.parse_sess.proc_macro_quoted_spans() {
17171716
let span = self.lazy(span);
17181717
self.tables.proc_macro_quoted_spans.set_some(i, span);
17191718
}

compiler/rustc_session/src/parse.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::lint::{
88
};
99
use rustc_ast::node_id::NodeId;
1010
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
11-
use rustc_data_structures::sync::{AtomicBool, Lock, Lrc};
11+
use rustc_data_structures::sync::{AppendOnlyVec, AtomicBool, Lock, Lrc};
1212
use rustc_errors::{emitter::SilentEmitter, ColorConfig, Handler};
1313
use rustc_errors::{
1414
fallback_fluent_bundle, Diagnostic, DiagnosticBuilder, DiagnosticId, DiagnosticMessage,
@@ -219,7 +219,7 @@ pub struct ParseSess {
219219
pub assume_incomplete_release: bool,
220220
/// Spans passed to `proc_macro::quote_span`. Each span has a numerical
221221
/// identifier represented by its position in the vector.
222-
pub proc_macro_quoted_spans: Lock<Vec<Span>>,
222+
pub proc_macro_quoted_spans: AppendOnlyVec<Span>,
223223
/// Used to generate new `AttrId`s. Every `AttrId` is unique.
224224
pub attr_id_generator: AttrIdGenerator,
225225
}
@@ -324,13 +324,16 @@ impl ParseSess {
324324
}
325325

326326
pub fn save_proc_macro_span(&self, span: Span) -> usize {
327-
let mut spans = self.proc_macro_quoted_spans.lock();
328-
spans.push(span);
329-
return spans.len() - 1;
327+
self.proc_macro_quoted_spans.push(span)
330328
}
331329

332-
pub fn proc_macro_quoted_spans(&self) -> Vec<Span> {
333-
self.proc_macro_quoted_spans.lock().clone()
330+
pub fn proc_macro_quoted_spans(&self) -> impl Iterator<Item = (usize, Span)> + '_ {
331+
// This is equivalent to `.iter().copied().enumerate()`, but that isn't possible for
332+
// AppendOnlyVec, so we resort to this scheme.
333+
(0..)
334+
.map(|i| (i, self.proc_macro_quoted_spans.get(i)))
335+
.take_while(|(_, o)| o.is_some())
336+
.filter_map(|(i, o)| Some((i, o?)))
334337
}
335338

336339
#[track_caller]

0 commit comments

Comments
 (0)