Skip to content

Commit

Permalink
Use Freeze for SourceFile.lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Sep 7, 2023
1 parent f8e4afc commit f96fe5b
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl DebugContext {

match tcx.sess.source_map().lookup_line(span.lo()) {
Ok(SourceFileAndLine { sf: file, line }) => {
let line_pos = file.lines(|lines| lines[line]);
let line_pos = file.lines()[line];
let col = file.relative_position(span.lo()) - line_pos;

(file, u64::try_from(line).unwrap() + 1, u64::from(col.to_u32()) + 1)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl CodegenCx<'_, '_> {
pub fn lookup_debug_loc(&self, pos: BytePos) -> DebugLoc {
let (file, line, col) = match self.sess().source_map().lookup_line(pos) {
Ok(SourceFileAndLine { sf: file, line }) => {
let line_pos = file.lines(|lines| lines[line]);
let line_pos = file.lines()[line];

// Use 1-based indexing.
let line = (line + 1) as u32;
Expand Down
14 changes: 13 additions & 1 deletion compiler/rustc_data_structures/src/sync/freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::sync::{AtomicBool, ReadGuard, RwLock, WriteGuard};
use crate::sync::{DynSend, DynSync};
use std::{
cell::UnsafeCell,
intrinsics::likely,
marker::PhantomData,
ops::{Deref, DerefMut},
sync::atomic::Ordering,
Expand Down Expand Up @@ -49,6 +50,17 @@ impl<T> FreezeLock<T> {
self.frozen.load(Ordering::Acquire)
}

/// Get the inner value if frozen.
#[inline]
pub fn get(&self) -> Option<&T> {
if likely(self.frozen.load(Ordering::Acquire)) {
// SAFETY: This is frozen so the data cannot be modified.
unsafe { Some(&*self.data.get()) }
} else {
None
}
}

#[inline]
pub fn read(&self) -> FreezeReadGuard<'_, T> {
FreezeReadGuard {
Expand All @@ -74,10 +86,10 @@ impl<T> FreezeLock<T> {

#[inline]
pub fn try_write(&self) -> Option<FreezeWriteGuard<'_, T>> {
let _lock_guard = self.lock.write();
if self.frozen.load(Ordering::Relaxed) {
None
} else {
let _lock_guard = self.lock.write();
Some(FreezeWriteGuard { _lock_guard, lock: self, marker: PhantomData })
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
let len = BytePos::decode(decoder);

let file_lo = decoder.file_index_to_file(file_lo_index);
let lo = file_lo.lines(|lines| lines[line_lo - 1] + col_lo);
let lo = file_lo.lines()[line_lo - 1] + col_lo;
let lo = file_lo.absolute_position(lo);
let hi = lo + len;

Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_query_system/src/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {

src_hash.hash_stable(hcx, hasher);

// We are always in `Lines` form by the time we reach here.
assert!(self.lines.borrow().is_lines());
self.lines(|lines| {
{
// We are always in `Lines` form by the time we reach here.
assert!(self.lines.read().is_lines());
let lines = self.lines();
// We only hash the relative position within this source_file
lines.len().hash_stable(hcx, hasher);
for &line in lines.iter() {
line.hash_stable(hcx, hasher);
}
});
}

// We only hash the relative position within this source_file
multibyte_chars.len().hash_stable(hcx, hasher);
Expand Down
Loading

0 comments on commit f96fe5b

Please sign in to comment.