-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
lines.push(line_start); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean like this?
It's harder to read but could be faster if it avoids bounds checks. @martingms , want to try it out? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, like that 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!(), | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
+=
, here and below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BytePos
doesn't implementAddAssign
, I guess I could add that but seemed a bit out of scope, as thex = x + y
form was already used here in the old code.There was a problem hiding this comment.
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.