mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 17:24:06 +00:00
0b81d7cdc6
`SourceFile::lines` is a big part of metadata. It's stored in a compressed form (a difference list) to save disk space. Decoding it is a big fraction of compile time for very small crates/programs. This commit introduces a new type `SourceFileLines` which has a `Lines` form and a `Diffs` form. The latter is used when the metadata is first read, and it is only decoded into the `Lines` form when line data is actually needed. This avoids the decoding cost for many files, especially in `std`. It's a performance win of up to 15% for tiny crates/programs where metadata decoding is a high part of compilation costs. A `Lock` is needed because the methods that access lines data (which can trigger decoding) take `&self` rather than `&mut self`. To allow for this, `SourceFile::lines` now takes a `FnMut` that operates on the lines slice rather than returning the lines slice.
44 lines
1.6 KiB
Rust
44 lines
1.6 KiB
Rust
use super::*;
|
|
|
|
#[test]
|
|
fn test_lookup_line() {
|
|
let source = "abcdefghijklm\nabcdefghij\n...".to_owned();
|
|
let sf =
|
|
SourceFile::new(FileName::Anon(0), source, BytePos(3), SourceFileHashAlgorithm::Sha256);
|
|
sf.lines(|lines| assert_eq!(lines, &[BytePos(3), BytePos(17), BytePos(28)]));
|
|
|
|
assert_eq!(sf.lookup_line(BytePos(0)), None);
|
|
assert_eq!(sf.lookup_line(BytePos(3)), Some(0));
|
|
assert_eq!(sf.lookup_line(BytePos(4)), Some(0));
|
|
|
|
assert_eq!(sf.lookup_line(BytePos(16)), Some(0));
|
|
assert_eq!(sf.lookup_line(BytePos(17)), Some(1));
|
|
assert_eq!(sf.lookup_line(BytePos(18)), Some(1));
|
|
|
|
assert_eq!(sf.lookup_line(BytePos(28)), Some(2));
|
|
assert_eq!(sf.lookup_line(BytePos(29)), Some(2));
|
|
}
|
|
|
|
#[test]
|
|
fn test_normalize_newlines() {
|
|
fn check(before: &str, after: &str, expected_positions: &[u32]) {
|
|
let mut actual = before.to_string();
|
|
let mut actual_positions = vec![];
|
|
normalize_newlines(&mut actual, &mut actual_positions);
|
|
let actual_positions: Vec<_> = actual_positions.into_iter().map(|nc| nc.pos.0).collect();
|
|
assert_eq!(actual.as_str(), after);
|
|
assert_eq!(actual_positions, expected_positions);
|
|
}
|
|
check("", "", &[]);
|
|
check("\n", "\n", &[]);
|
|
check("\r", "\r", &[]);
|
|
check("\r\r", "\r\r", &[]);
|
|
check("\r\n", "\n", &[1]);
|
|
check("hello world", "hello world", &[]);
|
|
check("hello\nworld", "hello\nworld", &[]);
|
|
check("hello\r\nworld", "hello\nworld", &[6]);
|
|
check("\r\nhello\r\nworld\r\n", "\nhello\nworld\n", &[1, 7, 13]);
|
|
check("\r\r\n", "\r\n", &[2]);
|
|
check("hello\rworld", "hello\rworld", &[]);
|
|
}
|