Skip to content

Commit 985c87f

Browse files
committed
with lock
1 parent 97facd0 commit 985c87f

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

compiler/rustc_data_structures/src/sharded.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ impl<T: Default> Default for Sharded<T> {
3434
impl<T: Default> Sharded<T> {
3535
#[inline]
3636
pub fn new(mut value: impl FnMut() -> T) -> Self {
37+
Sharded {
38+
single_thread: !active(),
39+
shard: Lock::new(value()),
40+
shards: [(); SHARDS].map(|()| CacheAligned(Lock::new(value()))),
41+
}
42+
}
43+
44+
pub fn with_new(mut value: impl FnMut() -> T) -> Self {
3745
if likely(!active()) {
3846
Sharded {
3947
single_thread: !active(),
@@ -114,7 +122,7 @@ pub type ShardedHashMap<K, V> = Sharded<FxHashMap<K, V>>;
114122

115123
impl<K: Eq, V> ShardedHashMap<K, V> {
116124
pub fn len(&self) -> usize {
117-
self.lock_shards().iter().map(|shard| shard.len()).sum()
125+
self.lock_shards().iter().map(|shard| shard.deref().len()).sum()
118126
}
119127
}
120128

compiler/rustc_data_structures/src/sync.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ cfg_if! {
437437
let panic: Lock<Option<_>> = Lock::new(None);
438438
t.into_par_iter().for_each(|i| if let Err(p) = catch_unwind(AssertUnwindSafe(|| for_each(i))) {
439439
let mut l = panic.lock();
440+
let l = l .deref_mut();
440441
if l.is_none() {
441442
*l = Some(p)
442443
}
@@ -480,6 +481,7 @@ cfg_if! {
480481
Ok(r) => Some(r),
481482
Err(p) => {
482483
let mut l = panic.lock();
484+
let l = l.deref_mut();
483485
if l.is_none() {
484486
*l = Some(p);
485487
}
@@ -599,7 +601,7 @@ pub struct Lock<T> {
599601
impl<T: Debug> Debug for Lock<T> {
600602
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
601603
match self.try_lock() {
602-
Some(guard) => f.debug_struct("Lock").field("data", &&*guard).finish(),
604+
Some(guard) => f.debug_struct("Lock").field("data", guard.deref()).finish(),
603605
None => {
604606
struct LockedPlaceholder;
605607
impl Debug for LockedPlaceholder {
@@ -740,7 +742,7 @@ impl<T: Default> Default for Lock<T> {
740742
impl<T: Clone> Clone for Lock<T> {
741743
#[inline]
742744
fn clone(&self) -> Self {
743-
Lock::new(self.borrow().clone())
745+
Lock::new(self.borrow().deref().clone())
744746
}
745747
}
746748

@@ -753,16 +755,12 @@ pub struct LockGuard<'a, T> {
753755
marker: PhantomData<&'a mut T>,
754756
}
755757

756-
impl<T> const Deref for LockGuard<'_, T> {
757-
type Target = T;
758-
759-
fn deref(&self) -> &T {
758+
impl<T> LockGuard<'_, T> {
759+
pub const fn deref(&self) -> &T {
760760
unsafe { &*self.lock.data.get() }
761761
}
762-
}
763762

764-
impl<T> const DerefMut for LockGuard<'_, T> {
765-
fn deref_mut(&mut self) -> &mut T {
763+
pub const fn deref_mut(&mut self) -> &mut T {
766764
unsafe { &mut *self.lock.data.get() }
767765
}
768766
}

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
11431143
record_graph,
11441144
record_stats,
11451145
)),
1146-
new_node_to_index: Sharded::new(|| {
1146+
new_node_to_index: Sharded::with_new(|| {
11471147
FxHashMap::with_capacity_and_hasher(capacity, Default::default())
11481148
}),
11491149
prev_index_to_index: Lock::new(IndexVec::from_elem_n(None, prev_graph_node_count)),

compiler/rustc_span/src/lib.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ impl<S: Encoder> Encodable<S> for SourceFile {
13541354
self.end_pos.encode(s);
13551355

13561356
// We are always in `Lines` form by the time we reach here.
1357-
assert!(self.lines.borrow().is_lines());
1357+
assert!(self.lines.with_borrow(|lines| lines.is_lines()));
13581358
self.lines(|lines| {
13591359
// Store the length.
13601360
s.emit_u32(lines.len() as u32);
@@ -1521,8 +1521,7 @@ impl SourceFile {
15211521
where
15221522
F: FnOnce(&[BytePos]) -> R,
15231523
{
1524-
let mut guard = self.lines.borrow_mut();
1525-
match &*guard {
1524+
let f = |guard: &mut SourceFileLines| match guard {
15261525
SourceFileLines::Lines(lines) => f(lines),
15271526
SourceFileLines::Diffs(SourceFileDiffs {
15281527
mut line_start,
@@ -1531,21 +1530,21 @@ impl SourceFile {
15311530
raw_diffs,
15321531
}) => {
15331532
// Convert from "diffs" form to "lines" form.
1534-
let num_lines = num_diffs + 1;
1533+
let num_lines = *num_diffs + 1;
15351534
let mut lines = Vec::with_capacity(num_lines);
15361535
lines.push(line_start);
15371536

1538-
assert_eq!(*num_diffs, raw_diffs.len() / bytes_per_diff);
1539-
match bytes_per_diff {
1537+
assert_eq!(*num_diffs, raw_diffs.len() / *bytes_per_diff);
1538+
match *bytes_per_diff {
15401539
1 => {
1541-
lines.extend(raw_diffs.into_iter().map(|&diff| {
1540+
lines.extend(*raw_diffs.into_iter().map(|diff| {
15421541
line_start = line_start + BytePos(diff as u32);
15431542
line_start
15441543
}));
15451544
}
15461545
2 => {
15471546
lines.extend((0..*num_diffs).map(|i| {
1548-
let pos = bytes_per_diff * i;
1547+
let pos = *bytes_per_diff * i;
15491548
let bytes = [raw_diffs[pos], raw_diffs[pos + 1]];
15501549
let diff = u16::from_le_bytes(bytes);
15511550
line_start = line_start + BytePos(diff as u32);
@@ -1554,7 +1553,7 @@ impl SourceFile {
15541553
}
15551554
4 => {
15561555
lines.extend((0..*num_diffs).map(|i| {
1557-
let pos = bytes_per_diff * i;
1556+
let pos = *bytes_per_diff * i;
15581557
let bytes = [
15591558
raw_diffs[pos],
15601559
raw_diffs[pos + 1],
@@ -1572,7 +1571,8 @@ impl SourceFile {
15721571
*guard = SourceFileLines::Lines(lines);
15731572
res
15741573
}
1575-
}
1574+
};
1575+
self.lines.with_lock(f)
15761576
}
15771577

15781578
/// Returns the `BytePos` of the beginning of the current line.
@@ -1590,15 +1590,15 @@ impl SourceFile {
15901590
F: FnOnce() -> Option<String>,
15911591
{
15921592
if matches!(
1593-
*self.external_src.borrow(),
1593+
*self.external_src.borrow().deref(),
15941594
ExternalSource::Foreign { kind: ExternalSourceKind::AbsentOk, .. }
15951595
) {
15961596
let src = get_src();
15971597
let mut external_src = self.external_src.borrow_mut();
15981598
// Check that no-one else have provided the source while we were getting it
15991599
if let ExternalSource::Foreign {
16001600
kind: src_kind @ ExternalSourceKind::AbsentOk, ..
1601-
} = &mut *external_src
1601+
} = external_src.deref_mut()
16021602
{
16031603
if let Some(mut src) = src {
16041604
// The src_hash needs to be computed on the pre-normalized src.
@@ -1613,10 +1613,10 @@ impl SourceFile {
16131613

16141614
false
16151615
} else {
1616-
self.src.is_some() || external_src.get_source().is_some()
1616+
self.src.is_some() || external_src.deref().get_source().is_some()
16171617
}
16181618
} else {
1619-
self.src.is_some() || self.external_src.borrow().get_source().is_some()
1619+
self.src.is_some() || self.external_src.with_borrow(|src| src.get_source().is_some())
16201620
}
16211621
}
16221622

@@ -1642,7 +1642,7 @@ impl SourceFile {
16421642

16431643
if let Some(ref src) = self.src {
16441644
Some(Cow::from(get_until_newline(src, begin)))
1645-
} else if let Some(src) = self.external_src.borrow().get_source() {
1645+
} else if let Some(src) = self.external_src.with_borrow(|src| src.get_source()) {
16461646
Some(Cow::Owned(String::from(get_until_newline(src, begin))))
16471647
} else {
16481648
None

0 commit comments

Comments
 (0)