-
Notifications
You must be signed in to change notification settings - Fork 111
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
Abbreviation parsing performance improvements #451
Conversation
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.
LGTM with a small comment about choosing the number of inline elements.
Thanks!
src/read/abbrev.rs
Outdated
@@ -173,14 +174,16 @@ impl Abbreviations { | |||
} | |||
} | |||
|
|||
type Attributes = SmallVec<[AttributeSpecification; 6]>; |
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.
Because SmallVec
always has a capacity: usize
regardless of if the elements are inline or on the heap, I think that 4 u16
s will be the same size as a regular Vec
on 32-bit but we can go up to 8 u16
s on 64-bit. Did you choose 6 based on some profiling or anything like that? If not, I think it makes sense to use the most inline elements we can without bloating the structure.
FWIW, we can assert the sizes at compile like this:
const SIZE_IS_SAME: bool = mem::size_of::<Attributes>() == mem::size_of::<Vec<AttributeSpecification>>();
const _ASSERT_SIZE_IS_SAME: () = [()][!SIZE_IS_SAME as usize];
As a practicality, I'd suggest only doing this for 64-bit.
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.
I chose 6 based on the benchmark results. However, after repeating the benchmarks for i686, I think 5 is a better number that works for both x86-64 and i686.
Regarding the rest of your comment:
AttributeSpecification
is 12 bytes with an 8 byte alignment, so effectively 16 bytes (it seems like you thought it was onlyu16
?) Theimplicit_const_value
is taking up a lot of space here for something that is rarely used.- The goal isn't to use the same inline size as a
Vec
. That would be true if we are simply trying to minimize memory usage, but in this case we are trying to find the tradeoff that is best for performance. Since most abbreviations are relatively small, we can make the inline size larger while still using the same average memory as theVec
, and benefiting from avoiding the heap indirection.
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.
(Note, I'm just clarifying my original comment, not arguing against the chosen number here. As I already said, my comments regarding number of inline elements were qualified on not having done extensive profiling of number of inline elements yet.)
* The goal isn't to use the same inline size as a `Vec`.
I should have said "same size as the smallvec's heap variant's data" which, unless I am missing something, is the same size as a regular Vec
once you add the capacity. I was thinking, "how many inline elements can we fit without making the SmallVec
bigger than it needs to be to represent the elements-in-the-heap variant?"
Anyways, it seems the question is moot!
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.
* (it seems like you thought it was only `u16`?)
Yes, also true; I misread!
The standard does not allow larger values, even for user defined values.
Reduce the size of abbreviations, and store them in a
SmallVec
.