Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize <SourceFile as Decodable>::decode #95981

Merged
merged 2 commits into from
Apr 14, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,17 +1318,26 @@ impl<D: Decoder> Decodable<D> for SourceFile {
let mut line_start: BytePos = Decodable::decode(d);
lines.push(line_start);

for _ in 1..num_lines {
let diff = match bytes_per_diff {
1 => d.read_u8() as u32,
2 => d.read_u16() as u32,
4 => d.read_u32(),
_ => unreachable!(),
};

line_start = line_start + BytePos(diff);

lines.push(line_start);
match bytes_per_diff {
1 => {
for _ in 1..num_lines {
line_start = line_start + BytePos(d.read_u8() as u32);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use +=, here and below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BytePos doesn't implement AddAssign, I guess I could add that but seemed a bit out of scope, as the x = x + y form was already used here in the old code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, yeah, don't worry about it.

lines.push(line_start);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using extend with an iterator instead of push?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean like this?

lines.extend((1..num_lines).map(|_| {
    line_start += BytePos(d.read_u8());
    line_start
}));

It's harder to read but could be faster if it avoids bounds checks. @martingms , want to try it out?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, like that 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided against it for readability, but I didn't benchmark it to see if it's faster, will do that now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed the commit here, if you wanted to do a CI perf run @nnethercote

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks promising locally, so worth a run! Thanks for the suggestion @Dandandan, I've still got a lot to learn about convincing the compiler to elide bounds checks.

}
}
2 => {
for _ in 1..num_lines {
line_start = line_start + BytePos(d.read_u16() as u32);
lines.push(line_start);
}
}
4 => {
for _ in 1..num_lines {
line_start = line_start + BytePos(d.read_u32());
lines.push(line_start);
}
}
_ => unreachable!(),
}
}

Expand Down